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# (7)
  • Deals (2)
  • Events (11)
  • Patterns (4)
  • Podcast (5)
  • ReSharper (2)
  • TFS (1)
  • Uncategorized (6)
  • Unit Tests (1)
  • Unity Application Block (7)
  • What Burns Me (1)

Recent Posts

  • 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

Twitter

  • any of your books preloaded on it? RT @ambroselittle: RT Win a Kindle at #MIX10. Follow @Infragistics to learn how.-59 minutes ago
  • RT @esawdust: RT @HacknMod: 137 Years of Popular Science now Available for Free http://bit.ly/bRNNip-5 hours ago
  • Running Win 7 64-bit? Fix the broken preview thumbnails for PDF files. http://bit.ly/dAVVnH-5 hours ago
  • Don't forget to follow @OrlandoCC for the latest news on the Orlando Code Camp later this month-10 hours ago
  • Lots of buzz about Amazon releasing a better browser for the #Kindle, I'd rather see better EBook oranization via Folders.-10 hours ago
  • A PDF of the #OrlandoCC Agenda is out http://bit.ly/bwiZlP-11 hours ago
  • RT @ByteMaster: Introducing a new CodePlex project WinMo Twitter Widget Starter Kit -> http://tinyurl.com/ydzxv5v #wmdev #windowsphone-1 day ago
  • RT @CalebJenkins: Pelosi on Obamacare "we have to pass the bill so that you can find out what is in it" http://bit.ly/aciX3F-1 day ago

Comments

  • 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
  • Jeff Odell on Resharper Tip #1 – Highlight Usages

Recent Entries

  • 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
  • POCO and Unity Application Block Part II
  • POCO and Unity Application Block Part I
  • Random Selection of Posts

    • 10 Podcasts Every Developer Should Listen To
    • Mix08 Videos Posted Online
    • Unit Testing Principles and Goals
    • Moved my Blog
    • Installing VS 2010 in XP Mode
    • Speaking at the South Florida Code Camp
    • Orlando Code Camp slides and sample code
© 2008 Randy Patterson. WordPress. Free email sender software
Theme designing by Mark Hoodia