In this blog, we will discuss about
lifetime scope of instance of register type created by Unity Container and how
to manage it
Container.RegisterInstance(typeof(ProjectEngine), new ProjectEngine ())
By default, the RegisterInstance method
registers existing object instances with a container-controlled lifetime [ContainerControlledLifetimeManager]
it means that unity registers an object as a singleton instance.
By default lifetime for RegisterType method
is TransientLifetimeManager, which creates new object of request type for each
call.
Container.RegisterType<IProjectEngine, ProjectEngine>();
OR
Container.RegisterType<IProjectEngine, ProjectEngine>(new TransientLifetimeManager());
Unity Container have other lifetime managers so
you can able to manage lifetime of created object differently.
Lifetime Manager
|
Description
|
TransientLifetimeManager
|
Unity Creates a new instance of request type
for each call, when you call Resolve method
|
ContainerControlledLifetimeManager
|
Unity creates a singleton object of request
type on first call and it returns same object for subsequent Resolve call
|
HierarchicalLifetimeManager
|
Its same as
ContainerControllerLifeTimeManager, only difference is that child and parent
container creates its own singleton object and but both don’t share its
singleton object
|
PerResolveLifetimeManager
|
It is same as TransientLifeTimeManager but it
reuse same instanced object in the recursive object graph
|
PerThreadLifetimeManager
|
Unity Creates a singleton object per thread
basis
|
ExternallyControlledLifetimeManager
|
It delegate to external source to manage the
object and but for first resolve call, it creates object of requested type.
|
other dependency injection and unity container related posts :
No comments:
Post a Comment