Do I need one Unity container instance?

Sorry for this noob question. I am starting to use the Unity 2.0 container with Silverlight and put a few single into it. In order for singletones to work, do I only need to have one container instance in my application? I assume that instantiating a container of a new unity, when I want to see my singleton, will result in separate containers with separate singletones.

Thanks, -Jon

+4
source share
2 answers

To achieve single-tone behavior, you need one instance of the container. You also need to register your singleton classes with the ContainerControlledLifetimeManager.

+10
source

This is a bit confusing because when we refer to the Singletons in DI terminology , we do not mean the same thing as the Singleton design template .

Basically, Singleton Lifestyle behaves like a (template) Singleton inside the same container , but if you create and use it from a new container instance, you will get another instance.

We can say that Singleton Lifetime is a locally bounded Singleton.

To generalize the use of the container, you do not need a Singleton container, but to use Singleton Lifestyle you need an instance with one container .

To make things more confusing, in Unity, Singleton's lifestyle is called ContainerControlledLifetimeManager ...

+9
source

Source: https://habr.com/ru/post/1338652/


All Articles