Search code examples
dependency-injectioninversion-of-controlsimple-injector

Should I use Singleton registrations in Simple Injector?


In my registry, I'm registering a lot of things as Singleton.

I'm doing this because they're all thread safe and have no state. Therefore, one instance in memory is fine.

I'm also using AsyncScoped lifestyles for types that need context/state, but they are few and far-between compared to the amount of singleton registrations that I have.

Is there anything wrong with registering thread safe, stateless types as singleton?


Solution

  • This is actually a hard question to answer. There are two general DI Composition Models you can apply to construct dependency graphs:

    Both models have their pros and cons, but it is only when you apply the Ambient Composition Model holistically that you're able to register your components as Singleton.

    I started blogging about these two models here.