Giter Club home page Giter Club logo

Comments (9)

AAverin avatar AAverin commented on July 28, 2024

Also, ToSingleInstance is not mentioned in the documentation.
Is it still supported? What should I use instead?

from zenject-2019.

svermeulen avatar svermeulen commented on July 28, 2024

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.

AAverin avatar AAverin commented on July 28, 2024

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.

extrawurst avatar extrawurst commented on July 28, 2024

@AAverin if you want to construct it yourself you can still use ToInstance(new foo(params)) right ?

from zenject-2019.

extrawurst avatar extrawurst commented on July 28, 2024

@svermeulen documentation of ToSingleInstance would be great ;)

from zenject-2019.

AAverin avatar AAverin commented on July 28, 2024

@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.

extrawurst avatar extrawurst commented on July 28, 2024

@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.

svermeulen avatar svermeulen commented on July 28, 2024

@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.

svermeulen avatar svermeulen commented on July 28, 2024

Ok added both ToSingleInstance and ToSingleMethod to the documentation page

from zenject-2019.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.