<?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>amptools &#187; Code</title>
	<atom:link href="http://www.amptools.net/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://www.amptools.net</link>
	<description>Simplify your life.</description>
	<lastBuildDate>Sat, 08 May 2010 07:40:52 +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>Switching between Ioc libraries in Net.</title>
		<link>http://www.amptools.net/blog/switching-between-ioc-libraries-in-net</link>
		<comments>http://www.amptools.net/blog/switching-between-ioc-libraries-in-net#comments</comments>
		<pubDate>Fri, 23 Oct 2009 01:04:55 +0000</pubDate>
		<dc:creator>Michael Herndon</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[common-service-locator]]></category>
		<category><![CDATA[dependency-injection]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[inversion-of-control]]></category>
		<category><![CDATA[ioc]]></category>

		<guid isPermaLink="false">http://www.amptools.net/?p=239</guid>
		<description><![CDATA[Lets face it. Some developers are religiously attached to their beloved languages, frameworks, and libraries.
This becomes a problem when building software with hopes of gaining a developer base.  If it is not built on Unity, Castle Windsor, Structure Map or [insert beloved Ioc library here], they refuse to work/use the software.
Evidently others have had [...]]]></description>
			<content:encoded><![CDATA[<img class='gravatar' style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=899a978b0f02774a965aec11eeb0b8f4&amp;default=http://amppotls.net' alt='No Gravatar' width=48 height=48/><p>Lets face it. Some developers are religiously attached to their beloved languages, frameworks, and libraries.</p>
<p>This becomes a problem when building software with hopes of gaining a developer base.  If it is not built on Unity, Castle Windsor, Structure Map or [insert beloved Ioc library here], they refuse to work/use the software.</p>
<p>Evidently others have had this issue and have created a solution, the <a href="http://www.codeplex.com/CommonServiceLocator">common service locator</a>. Â I ran across while searching for asp.net Â mvc and DI (dependency injection) for models. More on DI with models inside controllers in a future post.</p>
<h2>How to use</h2>
<p>Download the library from <a href="http://www.codeplex.com/commonservicelocator">codeplex.com/CommonServiceLocator</a></p>
<p>Its pretty straight forward. Â Download the service locator adapter for the Ioc library of your code on the <a href="http://www.codeplex.com/commonservicelocator">codeplex.com/CommonServiceLocator</a></p>
<div class="wp-caption alignnone" style="width: 439px"><img title="common service locator adapters" src="http://farm3.static.flickr.com/2447/4035421289_eb10bffd3b_o.png" alt="choose the adapter for your Ioc library. " width="429" height="265" /><p class="wp-caption-text">choose the adapter for your Ioc library. </p></div>
<p>Configure your Ioc library in the usual manner. Then pass the container into the adapter.</p>
<pre class="brush: csharp;">
        private IServiceLocator CreateLocator()
        {
            var container = new UnityContainer();
            container.RegisterType&lt;IUser, User&gt;();
            container.RegisterType&lt;IMembership, Membership&gt;();
            container.RegisterType&lt;IUser, Loser&gt;(&quot;loser&quot;);

            return new UnityServiceLocatorAdapter(container);
        }
</pre>
<p>Then tell the service locator which adapter you are using.</p>
<pre class="brush: csharp;">
        private void SetupService()
        {
            // CreateLocator returns IServiceLocator in the above method.
            ServiceLocator.SetLocatorProvider(() =&gt; CreateLocator());
        }
</pre>
<p>Then use the locator to return the adapter, which will allow you to use a common interface to resolve your dependencies.</p>
<pre class="brush: csharp;">
        [It, Should(&quot;resolve the type to the default&quot;)]
        public void ResolveType()
        {
            this.SetupService();
           Â              // gets the current adapter for the Ioc Library.
            var resolver = ServiceLocator.Current;             

            // resolves the type.
            var result = resolver.GetInstance(typeof(IUser));

            // verify the result is the right type for the contract.
            result.IsInstanceOf(typeof(User));

            // this user should not have a membership.
            var user = (User)result;
            user.Membership.ShouldBeNull();
        }
</pre>
<h2>Some Caveats.</h2>
<p>Someone named the base service locator class in a java like manner using Impl.</p>
<p>The assemblies for the Unity Ioc are not strong named assemblies.  I had to create a key, sign the assign the assemblies for Unity.<a href="http://github.com/amptools-net/midori-project/zipball/master"> You can download the midori-project</a> which has the signed assemblies for Unity under the vendor folder.</p>
<h2>Further Usage and Reading</h2>
<p>If you would like to check out the spec and see how it might be used, <a href="http://github.com/amptools-net/midori-project/blob/master/specs/midori.core.specs/Services/ServiceLocatorApi_UnityServiceLocatorAdapter.cs">check out the spec in the midori core</a>. This will give you some context.</p>
<p>You can also read more about it on <a href="http://codebetter.com/blogs/glenn.block/archive/2008/10/02/iservicelocator-a-step-toward-ioc-container-service-locator-detente.aspx">codebetter.com</a> as well as the<a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/08/16/it-s-time-for-ioc-container-detente.aspx"> initial post from Jeremy Miller</a> that spawned someone to write this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.amptools.net/blog/switching-between-ioc-libraries-in-net/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->