2

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?

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
  • 2
    I would applaud you for it! When you think about it a complete singleton is the most easiest of all. You would not need a container for it, for example. So no IMHO it is as good as it gets with almost all singletons! – Ric .Net May 23 '19 at 17:58

1 Answers1

2

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.

Steven
  • 166,672
  • 24
  • 332
  • 435