title: What's the best way to inject different implementations of the same interface in to a controller using StructureMap?
body: <p>I am fairly new to StructureMap, but my understanding is that there are two ways of getting an instance from the <code>ObjectFactory</code>:</p>
  
  <ol>
  <li>By type (i.e. <code>ObjectFactory.GetInstance&lt;IFoo&gt;()</code>)</li>
  <li>By type and name (i.e. <code>ObjectFactory.GetNamedInstance&lt;IFoo&gt;("foo.bar")</code>)</li>
  </ol>
  
  <p>I have seen a multitude of examples out there demonstrating how to create an MVC Controller Factory that will provide controller instances using StructureMap, and in most cases I have seen, they are using the method from option #1 above.</p>
  
  <p>Let's say we have some controller that accepts a repository/DAO interface as a constructor arg...</p>
  
  <pre><code>public class FooController : Controller
  {
    public FooController (IFooRepository fooRepository)
    {
       // constructor code goes here
    }
  }
  </code></pre>
  
  <p>Using the interface class gives me the ability to "inject" any implementation of that interface in to my controller. What if for two different actions, I want to inject different implementations? Perhaps in one case, I need an implementation of my repository that will query a SQL Server database, and in another case, I need an implementation that will get data from an XML file or through a Web Service call.</p>
  
  <p>It seems that if you use the ObjectFactory.GetInstance() method, you would be restricted to only providing your controller with a single implementation of your repository interface.</p>
  
  <p>However, if you go with named instances, then you will end up having to create the instances based on the name of the controller that the MVC framework provides. In most cases, this will typically be the controller name coming from the URL, but it's really up to the route configuration. This seems like it could be very confusing, and would only get more confusing as the number of routes increased.</p>
  
  <p>Is there any alternative method that would allow for different controller instantiation strategies without using named instances? Would it be a better idea to just create separate controllers, and make sure that all the actions in any given controller will be valid for the same concrete instance of a repository/DAO class?</p>