DEV Community

Discussion on: Dependency Injection in NetCore

Collapse
 
jcalixte profile image
Julien Calixte

Hi! Why not only use Singleton classes? What are the benefits using Scoped and Transient?

Collapse
 
jpblancodb profile image
JPBlancoDB

Hi Julien! So singleton classes only instantiate once, I would not use singleton as default because could cause you troubles. Don't think that it is going to be better for performance if you use your whole app as a singleton, on the contrary, I would recommend you if you are in doubt, just use transient as default.

One use case for singleton that I could think of right now is, for example, you have a configuration file and this does not change then you want to read it once at startup and then inject these values where is needed. Or for the service locator pattern, you could read more about this: Using a service locator.

My advice is, don't ever use singletons for classes that contain business logic.

I hope this gives you more insights into singletons!