In C#, there are several way to
maintain the single instance of object by using singleton pattern, static classor static property with private constructor.
Recently I explored one more way to
implement singleton pattern by using unity and in unity container, we can
define the lifetime scope of object instance by passing Lifetime Manager option in
RegisterType methods.
UnityContainer
container = new UnityContainer();
container.RegisterType<IHTTPClientManager, HTTPClientManager>( new ContainerControlledLifetimeManager());
In above example, ContainerControlledLifetimeManager as Lifetimemanager
option is being passed in registerType method for IHTTPClientManager type.
Unity will take care of instance creation of IHTTPClientManager type
and it will make sure that there will be a single instance of
IHTTPClientManager type.it will not create any new instance after first resolve
method call and it will share same instance.
other dependency injection and unity container related posts :
No comments:
Post a Comment