Friday, January 4, 2019

Unity Container Register Singleton Class

In C#, there are several way to maintain the single instance of object by using singleton pattern, static classor static property with private constructor.

Singleton Pattern HTTPCLient

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.

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...