<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Randy Patterson &#187; Unit Tests</title>
	<atom:link href="http://RandyPatterson.com/index.php/category/unit-tests/feed/" rel="self" type="application/rss+xml" />
	<link>http://RandyPatterson.com</link>
	<description>Code To Live, Live To Code</description>
	<lastBuildDate>Sat, 24 Apr 2010 01:18:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Lazy Instantiation with Unity Application Block</title>
		<link>http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/</link>
		<comments>http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 05:48:37 +0000</pubDate>
		<dc:creator>randypatterson</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Unit Tests]]></category>
		<category><![CDATA[Unity Application Block]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://localhost/2008/03/30/LazyInstantiationWithUnityApplicationBlock.aspx</guid>
		<description><![CDATA[Configuring the Unity Application Block for just-in-time Instantiation.&#160; 
Out of the box, Unity does not provide a way to configure your dependencies so that they are resolved only if needed.&#160; 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 [...]]]></description>
			<content:encoded><![CDATA[<p>Configuring the <a href="http://codeplex.com/unity" target="_blank">Unity Application Block</a> for just-in-time Instantiation.&nbsp; </p>
<p>Out of the box, Unity does not provide a way to configure your dependencies so that they are resolved only if needed.&nbsp; 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.</p>
<p>For <a href="http://en.wikipedia.org/wiki/Lazy_initialization_pattern" target="_blank">Lazy Instantiation</a>, the first obstacle to overcome when using <a href="http://martinfowler.com/articles/injection.html" target="_blank">IoC Containers</a> like <a href="http://codeplex.com/unity" target="_blank">Unity</a>, is to have your components get a reference to the container so the dependency creation can be delegated to it when the need arises.&nbsp; My first inclination was to wrap Unity in a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" target="_blank">Singleton</a> and have the component reference this in order to resolve the dependency.&nbsp; </p>
<p><font size="3">Container.Instance.Resolve&lt;<font color="#07c7fe">IProductService</font>&gt;();</font></p>
<p>Singleton&#8217;s are, however, notoriously difficult to test and very rigid in design.&nbsp; Used in this manner,&nbsp; a Singleton is nothing more than a global variable wrapped in a buzzword.&nbsp; so I wanted a different approach.&nbsp; Since we are using a <a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">Dependency Injection</a> Framework, why not have the container inject a reference to itself into our components.</p>
<p>1.&nbsp; First, configure the Unity container so that it can return a reference to itself.</p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="65" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image1_thumb.png" width="526" border="0"></a> </p>
<p>2.&nbsp; Next, create a property on your class to get a reference to the Unity container that created it.</p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image10.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="119" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image10_thumb.png" width="341" border="0"></a> </p>
<p>3.&nbsp; Finally, have the container resolve the dependency the first time it&#8217;s referenced.&nbsp; </p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image16.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="181" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image16_thumb.png" width="548" border="0"></a> </p>
<p><font size="3"></font><font size="3">Unit Testing</font></p>
<p>Testing is straight forward as well.&nbsp;&nbsp; You could have your unit test use reflection to reach in and set the private field (_productService) to your mock object.&nbsp; However,&nbsp; giving your Unit Tests access to private members violates the &#8220;<a href="http://xunitpatterns.com/Principles%20of%20Test%20Automation.html#Use%20the%20Front%20Door%20First" target="_blank">Use the Front Door First</a>&#8221; Test Automation Principle and should be avoided if possible.&nbsp; Alternatively, you could create a mock object for IUnityContainer and have it return a mock object for IProductService when the Resolve method is called.</p>
<p>Using <a href="http://www.ayende.com/projects/rhino-mocks/downloads.aspx" target="_blank">RhinoMocks</a>, the Unit Test would look something like this:</p>
<p>&nbsp;</p>
<p><a href="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="424" alt="image" src="http://www.randypatterson.com/images/Gettingareferencetothecurrentunitycontai_B90C/image_thumb.png" width="698" border="0"></a> </p>
<p><font size="3"></font>&nbsp;</p>
<p>&nbsp;<a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2frandypatterson.com%2f2008%2f03%2f30%2fLazyInstantiationWithUnityApplicationBlock.aspx"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2frandypatterson.com%2f2008%2f03%2f30%2fLazyInstantiationWithUnityApplicationBlock.aspx" border="0"></a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dotnetkicks.com/kick/?url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;DotNetKicks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dotnetkicks.png" title="Add to&nbsp;DotNetKicks" alt="Add to&nbsp;DotNetKicks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Lazy+Instantiation+with+Unity+Application+Block&amp;url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;title=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Lazy+Instantiation+with+Unity+Application+Block+@+http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/&amp;t=Lazy+Instantiation+with+Unity+Application+Block" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://RandyPatterson.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://RandyPatterson.com/index.php/2008/03/30/lazy-instantiation-with-unity-application-block/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
