Randy Patterson

Code To Live, Live To Code

  • Home
  • About

30

Mar

Lazy Instantiation with Unity Application Block

Posted by randypatterson  Published in Patterns, Unit Tests, Unity Application Block

Configuring the Unity Application Block for just-in-time Instantiation. 

Out of the box, Unity does not provide a way to configure your dependencies so that they are resolved only if needed.  Lazy Instantiation would be desirable if your dependency is and required only under certain circumstances or you would like to delay object creation of a resource intensive dependency until needed.

For Lazy Instantiation, the first obstacle to overcome when using IoC Containers like Unity, is to have your components get a reference to the container so the dependency creation can be delegated to it when the need arises.  My first inclination was to wrap Unity in a Singleton and have the component reference this in order to resolve the dependency. 

Container.Instance.Resolve<IProductService>();

Singleton’s are, however, notoriously difficult to test and very rigid in design.  Used in this manner,  a Singleton is nothing more than a global variable wrapped in a buzzword.  so I wanted a different approach.  Since we are using a Dependency Injection Framework, why not have the container inject a reference to itself into our components.

1.  First, configure the Unity container so that it can return a reference to itself.

image

2.  Next, create a property on your class to get a reference to the Unity container that created it.

image

3.  Finally, have the container resolve the dependency the first time it’s referenced. 

image

Unit Testing

Testing is straight forward as well.   You could have your unit test use reflection to reach in and set the private field (_productService) to your mock object.  However,  giving your Unit Tests access to private members violates the “Use the Front Door First” Test Automation Principle and should be avoided if possible.  Alternatively, you could create a mock object for IUnityContainer and have it return a mock object for IProductService when the Resolve method is called.

Using RhinoMocks, the Unit Test would look something like this:

 

image

 

 kick it on DotNetKicks.com

Bookmark It

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone
Add to Google Bookmarks Add to reddit Add to Slashdot Add to Stumble Upon
Add to Technorati Add to Twitter Add to Yahoo My Web

Tags: IoC, Unity

1 comment

RSS

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Tags

Azure CodeCamp IoC Podcast Presentation ReSharper TFS Unit Test Unity VS2010

Categories

  • C# (8)
  • Deals (2)
  • Events (11)
  • Patterns (4)
  • Podcast (5)
  • ReSharper (3)
  • TFS (1)
  • Uncategorized (7)
  • Unit Tests (1)
  • Unity Application Block (7)
  • What Burns Me (1)

Recent Posts

  • Support The National Day of Prayer
  • ReSharper and the Method Group Refactor
  • Moved my Blog
  • Installing VS 2010 in XP Mode
  • TFS Basic Version

Twitter

  • RT @dotnetblogs: #shanselman: A New Podcast for Developers - This Developer's Life http://ow.ly/18RfJQ-2 hours ago
  • going to go see vampires suck with my daughter. I'll have to laugh when she does because I wont understand a thing-3 days ago
  • WIndows Live Sync is the new Live Mesh http://tcrn.ch/ceh2oR-4 days ago
  • got a signed copy of @sdorman new C# book!!-1 week ago
  • RT @devfish: tampa/orlando web firestarters - http://bit.ly/9opxrF #fldev-1 week ago
  • I am constantly impressed with the quality and content of the RadioLab Science poscast http://bit.ly/Od4Ek-1 week ago
  • Reality is frequently inaccurate - Douglas Adams-1 week ago
  • Reminder: IASA Meeting tonight: @absolutdeno presenting Delivering SOA with Silverlight and WCF RIA Services http://bit.ly/aRCSuc-1 week ago

Comments

  • olegsych on Moved my Blog
  • Ed Holloway on TFS Basic Version
  • Jeff P on The Power of the Predicate<T>
  • Jeff Odell on Beginning ReSharper
  • Jeff Odell on POCO and Unity Application Block Part I

Recent Entries

  • Support The National Day of Prayer
  • ReSharper and the Method Group Refactor
  • Moved my Blog
  • Installing VS 2010 in XP Mode
  • TFS Basic Version
  • Windows Azure Commercial Release this year!
  • Orlando Code Camp 2009 – Unity Presentation Slides and Code
  • Speaking at the South Florida Code Camp
  • Call for Speakers
  • Beginning ReSharper
  • Random Selection of Posts

    • Useless Unit Test
    • Get SnagIt and Camtasia for FREE!
    • I am Speaking at the Next IASA Meeting
    • ]InBetween[ Microsoft Community Summit 2008 – Free Weekend of Training
    • Mix08 Videos Posted Online
    • Martin Fowler has a Podcast!
    • Tampa Bay International Association of Software Architects Newsletter – January 2008
© 2008 Randy Patterson. WordPress. Free email sender software
Theme designing by Mark Hoodia