-
Notifications
You must be signed in to change notification settings - Fork 318
[Discussion]: AddInstance was renamed to AddSingleton #332
Comments
👍 |
https://docs.asp.net/en/latest/fundamentals/dependency-injection.html
So if you "merge" or rename, how would the new functionality fit? How would I know where the instance is created? |
That doesn't change. You're still passing in an instance so it's instantiated wherever you're create it. |
Duh, good point 😄 |
Should probably call is AddSingleInstance() instead? |
@matlus, we don't differentiate between the factory method and the type overload (i.e. we don't have a |
@pranavkm I don't see a Singleton needs a Factory (since there is only one). AddInstance is clearer (to me) than AddSingleton in my honest opinion. I guess the confusion is that "Singleton" is an overloaded term here. I assumed (initially) my class needed to be designed as a Singleton, but we're say "give me an instance and I'll just reuse that instance each time". so it's effectively a singleton but it is not designed to be. Thinking back on it, I'd stick to AddInstance() :). code documentation in these methods will make the intent/operation clearer. |
A singleton could need a factory if it is expensive or complex to construct, yet might not be needed. |
The factory lets you get at other services via the service container that is passed to it. We use this in a couple of places as a substitute for creating instance factories. |
Right, that's exactly what I use the AddInstance() for now. But I create the instance and hand it off (obviously). So you're saying the overload that takes a Factory method, will use the Factory method just once? Or do I have to pass you a method that creates an instance where the class is designed to be a singleton? Ok, looking at Eilon's answer makes sense from a lazy loading need point of view. I see the use for this functionality (obviously - since I'm using it 😃). I do still have a confusion with the name. |
Yes it will be called just once, since it's a singleton.
Yes, if you add a singleton the class should be designed to be a singleton.
The delegate is not just for lazy loading, it's for getting at other registered services at time of construction: services.AddSingleton<IFoo>(sp => new Foo(sp.GetRequiredService<IBar>()); At the time you call |
I get the need/purpose of this method and its overloads. By the way does the DI container call Dispose for any instances it is managing and id the type implements IDisposable?
Ok, I see why you've answered "yes" to both. However, I don't think I was clear when I said, "Designed to be a singleton". Of course when you're sharing one instance across multiple requests you have to be careful about state and have to be thread-safe since multiple simultaneous request could be partying on your class at the same instant in time. But does the class truly need to be designed to allow only one instance of you call the factory method exactly once even when multiple threads may be requesting this instance at the exact same time? |
@matlus only one instance will be created. The class must be thread-safe or otherwise provide some sort of thread safety guarantee (e.g. a very poor solution would be some kind of external synchronization, but that's an extreme anti-pattern). The class does not need to require allowing only one instance. In fact, that is also an anti-pattern. E.g. in a unit test you might want to instantiate many instance of the class to test. |
|
We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have. |
Discussion PR for #319
The rename makes the 3 APIs line up
The text was updated successfully, but these errors were encountered: