title: How to properly decouple Structure Map dependency resolver from ASP.NET MVC web project?
body: <p>While developing web project using ASP.NET MVC, I came up against a coupling problem.
  When I build custom controller factory (or dependency resolver if using MVC 3), I need this factory to know somehow where to get dependencies from. Here's my code:</p>
  
  <pre><code>//from Global.asax.cs
  
  DependencyResolver.SetResolver(new StructureMapControllerFactory());
  class StructureMapControllerFactory: IDependencyResolver {
      Container repositories;
  
      public StructureMapControllerFactory()
      {
          repositories = new RepositoriesContainer();
      }
      //... rest of the implementation
  }
  class RepositoriesContainer: Container
  {
     public RepositoriesContainer()
     {
        For&lt;IAccountRepository&gt;().Use&lt;SqlAccountRepository&gt;();
        //...
     }
  }
  </code></pre>
  
  <p><code>StructureMapControllerFactory</code> class is responsible for injecting dependencies into a controller. As I said, it needs to know where to find these dependencies (I mean concrete classes, like services and repositories implementations).</p>
  
  <p>I have a separate class library called <code>MySite.Data</code>, where all the implementation details live. Contracts, like <code>IAccountRepository</code>, live in library <code>MySite.Contracts</code>. Now, if I reference this <code>MySite.Data</code> library directly from MVC project, there will be a dependency between my site and implementation of its data retrieval. The question is how can I remove it? What are best practices in this situation?</p>
  
  <p>I'm sure it does have a bunch of workarounds, just I haven't found any yet.</p>