Sunday, March 15, 2015

Lifetime Management

When you resolve an object that you registered using the RegisterType method, the container instantiates a new object when you call the Resolve method: the container does not hold a reference to the object. When you create a new instance using the RegisterInstance method, the container manages the object and holds a reference to it for the lifetime of the container.
Lifetime Managers manage the lifetimes of objects instantiated by the container.

The default lifetime manager for the RegisterType method is the Transient-LifetimeManager and the default lifetime manager for the RegisterInstance method is the ContainerControlledLifetimeManager.

If you want the container to create or return a singleton instance of a type when you call the
Resolve method, you can use the ContainerControlledLifetimeManager type when you register your type or instance. The following example shows how you could tell the container to create a singleton instance of the TenantStore.
         container.RegisterType<ITenantStore, TenantStore>(new ContainerControlledLifetimeManager());

The first time that you resolve the ITenantStore type the container creates a new TenantStore object and keeps a reference to it. On subsequent times when you resolve the ITenantStore type, the container returns a reference to the TenantStore object that it created previously. Some lifetime managers, such as the ContainerControlledLifetimeManager, are used to dispose the created objects when the container is disposed.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More