<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Gaute's .net weblog - Patterns</title>
    <link>http://gaute.amende.no/</link>
    <description>thoughts on programming and computer related stuff</description>
    <language>en-us</language>
    <copyright>gaute</copyright>
    <lastBuildDate>Fri, 12 Jun 2009 11:18:17 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>gaute@amende.no</managingEditor>
    <webMaster>gaute@amende.no</webMaster>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=2a040d40-6e4d-4411-b3c4-c57abfaec18a</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=2a040d40-6e4d-4411-b3c4-c57abfaec18a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">This blog post is about changing the DefaultControllerFactory
to a custom one to allow constructor dependency injection. In this post I use <a href="http://structuremap.sourceforge.net/Default.htm">Structuremap</a>, <a href="http://commonservicelocator.codeplex.com/">CommonServiceLocator</a> and
I use a slightly changed version of the <a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=StructureMap%20Adapter">CommonServiceLocator.StructureMapAdatpter</a>. 
<br /><br />
The DefaultControllerFactory needs a constructor without any input parameters. I want
to get rid of dependencies in my code, so I put all my interface dependencies in the
constructor and want to use structuremap to handle this for me. I have used this in
a semi-large project, but in this blog post I will use an example with only one dependency
and only one page.<br /><br />
Lets get started:<br /><br />
I start by creating a new ASP.NET MVC project. I then delete all controllers and views
except for my home controller and my index view.<br /><br />
Then I add a Tools folder and a PersonRepository class in my new folder. This only
has a default constructor and a get method that returns a Person, a simple object
with only a name Property. I also extract an interface from PersonRepository called
IPersonRepository.<br /><br />
In my homecontroller I create a constructor that takes a Ipersonrepository as a parameter.
This is my HomeController now:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Web.Mvc; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> DIControllerFactoryExample.Tools; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Controllers
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> HomeController
: Controller { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">readonly</span> IPersonRepository
personRepository; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> HomeController(IPersonRepository
personRepository) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.personRepository <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> personRepository;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> ActionResult
Index() { var personid <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> 1;
ViewData.Model <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> personRepository.Get(personid); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> View();
} } }</span><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /></span></pre>I compile, run and get a: "No parameterless constructor defined for this
object." exception. Just as planned.<br />
Now, the fun begins. Getting this to work.<br /><br />
I start by adding a solution folder called Lib, and adding StructureMap.dll and Microsoft.Practices.ServiceLocation.dll
and reference them in the solution. These can be found at the locations in the top
of this blog post.<br /><br />
Then I add 3 new files to my Tools folder.<br />
StructureMapServiceLocator.cs, DefaultStructureMapRegistry.cs, CommonServiceLocatorControllerFactory.cs<br /><br />
These files look like this:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> StructureMap.Configuration.DSL; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> DefaultStructureMapRegistry
: Registry { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> DefaultStructureMapRegistry()
{ ForRequestedType&lt;IPersonRepository&gt;().TheDefaultIsConcreteType &lt;PersonRepository&gt;();
} } }<pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Collections.Generic; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.Practices.ServiceLocation; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> StructureMap; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> StructureMapServiceLocator
: ServiceLocatorImplBase { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">private</span> IContainer
container; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> StructureMapServiceLocator(IContainer
container) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">this</span>.container <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> container;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> DoGetInstance(Type
serviceType, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> key)
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>.IsNullOrEmpty(key)
? container.GetInstance(serviceType) : container.GetInstance(serviceType, key); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span> IEnumerable&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span>&gt;
DoGetAllInstances(Type serviceType) { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">foreach</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> obj <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">in</span> container.GetAllInstances(serviceType))
{ yield <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> obj;
} } } } </span></pre></span></pre><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System.Web.Mvc; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> Microsoft.Practices.ServiceLocation; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> DIControllerFactoryExample.Tools
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> StructureMapServiceLocatorControllerFactory
: DefaultControllerFactory { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">protected</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">override</span> IController
GetControllerInstance(Type controllerType) { var controller <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> ServiceLocator.Current.GetInstance(controllerType) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">as</span> Controller; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> controller;
} } } </span></pre><br />
I also add the following to the Application_Start method in my Global.asax.cs:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">var
registry <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> DefaultStructureMapRegistry();
var container <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> Container(registry);
ServiceLocator.SetLocatorProvider(() =&gt; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StructureMapServiceLocator(container));
var locator <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StructureMapServiceLocatorControllerFactory();
ControllerBuilder.Current.SetControllerFactory(locator);</span></pre><br />
I compile, reload and everything works. That's is. Good luck;) 
<br /><br />
EDIT: Did a small fix in the code i global.asax, check out this <a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx">post</a><br /><br />
The full Visual Studio solution can be downloaded here:<br /><p></p><a href="http://gaute.amende.no/content/binary/DIControllerFactoryExample.zip">DIControllerFactoryExample.zip
(463.5 KB)</a><img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2a040d40-6e4d-4411-b3c4-c57abfaec18a" /></body>
      <title>Changing the ASP.NET MVC DefaultControllerFactory with a custom using StructureMap and Common Service Locator.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</guid>
      <link>http://gaute.amende.no/ChangingTheASPNETMVCDefaultControllerFactoryWithACustomUsingStructureMapAndCommonServiceLocator.aspx</link>
      <pubDate>Fri, 12 Jun 2009 11:18:17 GMT</pubDate>
      <description>This blog post is about changing the DefaultControllerFactory to a custom one to allow constructor dependency injection. In this post I use &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;Structuremap&lt;/a&gt;, &lt;a href="http://commonservicelocator.codeplex.com/"&gt;CommonServiceLocator&lt;/a&gt; and
I use a slightly changed version of the &lt;a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=StructureMap%20Adapter"&gt;CommonServiceLocator.StructureMapAdatpter&lt;/a&gt;. 
&lt;br&gt;
&lt;br&gt;
The DefaultControllerFactory needs a constructor without any input parameters. I want
to get rid of dependencies in my code, so I put all my interface dependencies in the
constructor and want to use structuremap to handle this for me. I have used this in
a semi-large project, but in this blog post I will use an example with only one dependency
and only one page.&lt;br&gt;
&lt;br&gt;
Lets get started:&lt;br&gt;
&lt;br&gt;
I start by creating a new ASP.NET MVC project. I then delete all controllers and views
except for my home controller and my index view.&lt;br&gt;
&lt;br&gt;
Then I add a Tools folder and a PersonRepository class in my new folder. This only
has a default constructor and a get method that returns a Person, a simple object
with only a name Property. I also extract an interface from PersonRepository called
IPersonRepository.&lt;br&gt;
&lt;br&gt;
In my homecontroller I create a constructor that takes a Ipersonrepository as a parameter.
This is my HomeController now:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Web.Mvc; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; DIControllerFactoryExample.Tools; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Controllers
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; HomeController
: Controller { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;readonly&lt;/span&gt; IPersonRepository
personRepository; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; HomeController(IPersonRepository
personRepository) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.personRepository &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; personRepository;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; ActionResult
Index() { var personid &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; 1;
ViewData.Model &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; personRepository.Get(personid); &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; View();
} } }&lt;/span&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;
&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;I compile, run and get a: "No parameterless constructor defined for this
object." exception. Just as planned.&lt;br&gt;
Now, the fun begins. Getting this to work.&lt;br&gt;
&lt;br&gt;
I start by adding a solution folder called Lib, and adding StructureMap.dll and Microsoft.Practices.ServiceLocation.dll
and reference them in the solution. These can be found at the locations in the top
of this blog post.&lt;br&gt;
&lt;br&gt;
Then I add 3 new files to my Tools folder.&lt;br&gt;
StructureMapServiceLocator.cs, DefaultStructureMapRegistry.cs, CommonServiceLocatorControllerFactory.cs&lt;br&gt;
&lt;br&gt;
These files look like this:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; StructureMap.Configuration.DSL; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; DefaultStructureMapRegistry
: Registry { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; DefaultStructureMapRegistry()
{ ForRequestedType&amp;lt;IPersonRepository&amp;gt;().TheDefaultIsConcreteType &amp;lt;PersonRepository&amp;gt;();
} } }&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Collections.Generic; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.Practices.ServiceLocation; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; StructureMap; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; StructureMapServiceLocator
: ServiceLocatorImplBase { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;private&lt;/span&gt; IContainer
container; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; StructureMapServiceLocator(IContainer
container) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;this&lt;/span&gt;.container &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; container;
} &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; DoGetInstance(Type
serviceType, &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; key)
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;.IsNullOrEmpty(key)
? container.GetInstance(serviceType) : container.GetInstance(serviceType, key); } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt;&amp;gt;
DoGetAllInstances(Type serviceType) { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;foreach&lt;/span&gt; (&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;object&lt;/span&gt; obj &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;in&lt;/span&gt; container.GetAllInstances(serviceType))
{ yield &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; obj;
} } } } &lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System.Web.Mvc; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; Microsoft.Practices.ServiceLocation; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; DIControllerFactoryExample.Tools
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; StructureMapServiceLocatorControllerFactory
: DefaultControllerFactory { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;protected&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;override&lt;/span&gt; IController
GetControllerInstance(Type controllerType) { var controller &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; ServiceLocator.Current.GetInstance(controllerType) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;as&lt;/span&gt; Controller; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; controller;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
I also add the following to the Application_Start method in my Global.asax.cs:&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;var
registry &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; DefaultStructureMapRegistry();
var container &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; Container(registry);
ServiceLocator.SetLocatorProvider(() =&amp;gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; StructureMapServiceLocator(container));
var locator &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; StructureMapServiceLocatorControllerFactory();
ControllerBuilder.Current.SetControllerFactory(locator);&lt;/span&gt;&lt;/pre&gt;
&lt;br&gt;
I compile, reload and everything works. That's is. Good luck;) 
&lt;br&gt;
&lt;br&gt;
EDIT: Did a small fix in the code i global.asax, check out this &lt;a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx"&gt;post&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
The full Visual Studio solution can be downloaded here:&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;a href="http://gaute.amende.no/content/binary/DIControllerFactoryExample.zip"&gt;DIControllerFactoryExample.zip
(463.5 KB)&lt;/a&gt;&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=2a040d40-6e4d-4411-b3c4-c57abfaec18a" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,2a040d40-6e4d-4411-b3c4-c57abfaec18a.aspx</comments>
      <category>.NET</category>
      <category>ASP.NET</category>
      <category>ASP.NET MVC</category>
      <category>Patterns</category>
    </item>
    <item>
      <trackback:ping>http://gaute.amende.no/Trackback.aspx?guid=dfa52b04-e2df-4982-8fa4-27c7ff01f603</trackback:ping>
      <pingback:server>http://gaute.amende.no/pingback.aspx</pingback:server>
      <pingback:target>http://gaute.amende.no/PermaLink,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</pingback:target>
      <dc:creator>Gaute Magnussen</dc:creator>
      <wfw:comment>http://gaute.amende.no/CommentView,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</wfw:comment>
      <wfw:commentRss>http://gaute.amende.no/SyndicationService.asmx/GetEntryCommentsRss?guid=dfa52b04-e2df-4982-8fa4-27c7ff01f603</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Third party applications are great, and its very often smart to use open source or
buy modules you need before making your own. However a lot of third party applications
do create some problems. One is that prices and functionality can change and new,
better and cheaper products come to surface.
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
 
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Therefore, you do not want to be to dependent on these applications, so if you find
another tool that does the same thing even better, you should be able to switch it
out easy. A good way to do this is to create a interface and a factory pattern, and
refer to the interface in your code and let the factory class decide what tool to
use.<span style="">  </span>You can also use this pattern to ease mocking of
objects. And let the factory return a mock object if you are unit testing.
</p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
          <br />
        </p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
Example in C#:<br /></p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
        </p>
        <pre>
          <span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">
            <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> System; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">namespace</span> Logger
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
An interface is created</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">interface</span> iLog
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> Log(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> s);
} <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//An
implementation using enterprise libraries logging</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> EntLog
: iLog { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> Log(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> s)
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Implement
logic to save to log</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">false</span>;
} } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//An
implementation using log4net to save logs.</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> log4net
: iLog { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">bool</span> Log(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> s)
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Implement
logic to save to log</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">false</span>;
} } <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
A factory class</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> LogFactory
{ <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
Creates an object, of the right type.</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span> iLog
CreateLog(){ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> log4net();
} } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Program
{ <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Main(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>[]
args) { <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
Get the log object to use.</span> iLog logger <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> LogFactory.CreateLog(); <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//Print
the type of logger used</span> Console.Out.WriteLine(logger.GetType()); <span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">//
Log a message and write the result</span> Console.Out.WriteLine(logger.Log(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Something
is happening"</span>)); Console.ReadKey(); } } } </span>
        </pre>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
To change this to use entLog instead of log4net, all you have to do is change the
object created in the logfactory.<span style="">  </span>To add a new logger,
all you have to do is create a new class to implement Ilog and change the creator
to return the new logger. The creator could also have a constructor that takes an
input, so that you can decide in the class using the log what kind of log you want
to use.
</p>
        <p>
        </p>
        <p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US">
 
</p>
        <p>
        </p>
        <img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=dfa52b04-e2df-4982-8fa4-27c7ff01f603" />
      </body>
      <title>Creating a Factory class and an Interface for your third party applications.</title>
      <guid isPermaLink="false">http://gaute.amende.no/PermaLink,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</guid>
      <link>http://gaute.amende.no/CreatingAFactoryClassAndAnInterfaceForYourThirdPartyApplications.aspx</link>
      <pubDate>Mon, 01 Sep 2008 11:33:16 GMT</pubDate>
      <description>

&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Third party applications are great, and its very often smart to use open source or
buy modules you need before making your own. However a lot of third party applications
do create some problems. One is that prices and functionality can change and new,
better and cheaper products come to surface.
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Therefore, you do not want to be to dependent on these applications, so if you find
another tool that does the same thing even better, you should be able to switch it
out easy. A good way to do this is to create a interface and a factory pattern, and
refer to the interface in your code and let the factory class decide what tool to
use.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;You can also use this pattern to ease mocking of
objects. And let the factory return a mock object if you are unit testing.
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
Example in C#:&lt;br&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&lt;/p&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;using&lt;/span&gt; System; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;namespace&lt;/span&gt; Logger
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
An interface is created&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;interface&lt;/span&gt; iLog
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; Log(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; s);
} &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//An
implementation using enterprise libraries logging&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; EntLog
: iLog { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; Log(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; s)
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Implement
logic to save to log&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;;
} } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//An
implementation using log4net to save logs.&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; log4net
: iLog { &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;bool&lt;/span&gt; Log(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt; s)
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Implement
logic to save to log&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;false&lt;/span&gt;;
} } &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
A factory class&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; LogFactory
{ &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
Creates an object, of the right type.&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;public&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; iLog
CreateLog(){ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;return&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;new&lt;/span&gt; log4net();
} } &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;class&lt;/span&gt; Program
{ &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;static&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;void&lt;/span&gt; Main(&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;string&lt;/span&gt;[]
args) { &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
Get the log object to use.&lt;/span&gt; iLog logger &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;=&lt;/span&gt; LogFactory.CreateLog(); &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//Print
the type of logger used&lt;/span&gt; Console.Out.WriteLine(logger.GetType()); &lt;span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;//
Log a message and write the result&lt;/span&gt; Console.Out.WriteLine(logger.Log(&lt;span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;"&gt;"Something
is happening"&lt;/span&gt;)); Console.ReadKey(); } } } &lt;/span&gt;&lt;/pre&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
To change this to use entLog instead of log4net, all you have to do is change the
object created in the logfactory.&lt;span style=""&gt;&amp;nbsp; &lt;/span&gt;To add a new logger,
all you have to do is create a new class to implement Ilog and change the creator
to return the new logger. The creator could also have a constructor that takes an
input, so that you can decide in the class using the log what kind of log you want
to use.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p style="margin: 0in; font-family: Calibri; font-size: 11pt;" lang="en-US"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://gaute.amende.no/aggbug.ashx?id=dfa52b04-e2df-4982-8fa4-27c7ff01f603" /&gt;</description>
      <comments>http://gaute.amende.no/CommentView,guid,dfa52b04-e2df-4982-8fa4-27c7ff01f603.aspx</comments>
      <category>.NET</category>
      <category>Patterns</category>
    </item>
  </channel>
</rss>