skip to the main content area of this page
Patterns and Practices

 

Unity March 4, 2008 Weekly Drop - RegisterType Resolve ContainerControlledLifetimeManager


The March 4, 2008 Weekly Drop of Unity is out and includes a number of API Changes on how you register types, resolve instances, and control the lifetime of registered types. In general, SetSingleton is gone, Get has been replaced by Resolve, Register has been replaced by RegisterType, and RegisterType now contains overloads to add a LifeTimeManager Instance.

 

Unity Sample Code

Here is the sample code from the following post on Unity Nested Containers, which has been updated with the new API Changes:

 

UnityContainer parentContainer = new UnityContainer();
IUnityContainer childContainer1 = parentContainer.CreateChildContainer();
IUnityContainer childContainer2 = parentContainer.CreateChildContainer();

parentContainer.RegisterType<ILogger, NullLogger>
(new ContainerControlledLifetimeManager())
; childContainer1.RegisterType<ILogger, CustomLogger>
(new ContainerControlledLifetimeManager())
; // Should be NullLogger // From ParentContainer ILogger logger = childContainer2.Resolve<ILogger>(); logger.Log("Test"); // Should be CustomLogger // From LocalContainer ILogger logger2 = childContainer1.Resolve<ILogger>(); logger2.Log("Test");

 

Hope this helps.

David Hayden


Tags: Unity


Topics



 

Popular Tags



Recent Links