Unity IoC API Changes Affect Code Samples – March 4, 2008 Weekly Drop

The Unity IoC / Dependency Injection Tool from Microsoft Patterns & Practices has a number of API Changes in the just released March 4, 2008 Weekly Drop.


If you watched my Unity IoC Screencast or have been using my sample code for using Unity with the ASP.NET MVC Framework or Unity with ASP.NET Model-View-Presenter, you will have to update the code to reflect the new API in the March 4th Weekly Drop.


The big API Changes are mentioned by Chris on the Unity CodePlex Site as:




  • Register has been replaced by RegisterType


  • Get has been replaced by Resolve


  • SetSingleton is history


  • RegisterType now has overloads for providing a LifeTimeManager Instance

and maybe others I haven’t come acrossed yet.


I was playing with Unity Nested Containers functionality today and updated the code to reflect the new API. Maybe seeing some code will help clarify some of the API Changes. Here is some sample code using the recent API Changes mentioned above:


 

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.

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>