Comments (9)
Also, ToSingleInstance is not mentioned in the documentation.
Is it still supported? What should I use instead?
from zenject-2019.
No, Zenject only injects into objects that it creates itself. So when it's provided with an instance, it doesn't inject on that. If you want to force-inject on it though, you can do that by calling _container.Inject(myObject)
.
Oh you're right, ToSingleInstance
isn't documented. I'll fix that, thanks
I prefer to use ToInstance
instead of ToSingleInstance
most of the time but ToSingleInstance
can still be useful in some cases. You need it if you want to bind an instance but also use it as ToSingle<>
for other interfaces. For example:
Container.Bind<IFoo>().ToSingleInstance(new Foo());
Container.Bind<IBar>().ToSingle<Foo>();
Container.Bind<Foo>().ToSingle();
from zenject-2019.
I had to use ToSingleInstance because I had some parameters that I needed to pass in from Editor.
I tried doing Container.Resolve() but that didn't work.
I'll try with Container.Inject(MyObject), thanks
from zenject-2019.
@AAverin if you want to construct it yourself you can still use ToInstance(new foo(params)) right ?
from zenject-2019.
@svermeulen documentation of ToSingleInstance would be great ;)
from zenject-2019.
@extrawurst I need my object to be a Singleton and be available in multiple classes around the project, so I'm doing ToSingleInstnace(new Foo(params)).
from zenject-2019.
@AAverin the documentation for ToInstance says In this case the given instance will be used for every dependency with the given type
... sounds like a singleton to me
from zenject-2019.
@AAverin Yeah, it sounds like using ToInstance
would work fine in your case. The difference is kinda subtle. Another difference is that you can do the following
Container.BindInstance(new Foo());
Container.BindInstance(new Foo());
Container.BindInstance(new Foo());
(Note that BindInstance
is the same as Bind<Foo>().ToInstance
)
And then have a class that takes all of them as a list like this:
public class Bar
{
public Bar(List<Foo> foos)
{
}
}
So really ToInstance
doesn't necessarily result in a singleton. On the other hand ToSingleInstance
will always require that there only be one unique instance, and also allows for binding to multiple interfaces using ToSingle<>
as I mentioned above.
Although, even in the case where you want to bind to multiple interfaces you can still do that just by re-using the instance like this:
var foo = new Foo();
Container.Bind<IFoo>().ToInstance(foo);
Container.Bind<IBar>().ToInstance(foo);
So ToSingleInstance
really isn't all that useful but does serve some purposes in some rare edge cases. I personally always use ToInstance
. ToInstance
is also nice because DiContainer has a convenience method that doesn't require generic arguments (_container.BindInstance(foo)
).
There is also ToSingleMethod
which works similarly. The given method will only be called once and the result returned from the method will be re-used for every dependency that it is bound to (whereas ToMethod
will be called each time).
from zenject-2019.
Ok added both ToSingleInstance
and ToSingleMethod
to the documentation page
from zenject-2019.
Related Issues (20)
- User Error -> StackOverflow AssertHit! (999+ times) HOT 2
- Why did you stop publishing in the asset store? HOT 4
- Interfaces and FromComponentInNewPrefab
- Declaring Factory for Object which doesn't have all its dependencies yet
- NullReferenceException in the MonoKernel.Start ()
- Error at project Upgrade HOT 4
- Cant add Zenject-TestFramework to Solution HOT 1
- Injection is happning twice
- Inject reference of a monobehaviour to an instantiated gameobject HOT 1
- "Commitment to open source" and lawsuit HOT 4
- Zenject causes gameObjects destroying just after start
- Typo in readme
- Automocking with Moq internal constructor not visible with Reflection HOT 4
- DiContainer.Resolve exception handling
- Why so many projects and files are added? HOT 1
- ZenjectException: Assert Hit! Expected null pointer but instead found 'Zenject.DiContainer' HOT 1
- DI on base class?
- Objects which class with interface implemented couldn't be inject correctly
- Support for IAsyncDisposable?
- Object that has Reference to the SceneContext stay in memory after scene unloaded HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zenject-2019.