title: How to dynamically register a type (eg. a different implementation) for a Unity container based on web application URL?
body: <p>Currently I am stuck on a what I assume to be a simple architecture problem.</p>
  
  <p>I have a controller (these are just examples as I can't share my real code, but the principle holds):</p>
  
  <pre><code>public class StackOverflowController : Controller
      {
          private readonly IStackOverflowService stackOverflowService;
  
          public StackOverflowService (IStackOverflowService stackOverflowService)
          {
              this.stackOverflowService = stackOverflowService;
          }
  
          // GET: StackOverflow
          public ActionResult Index()
          {
  
              var foo = stackOverflowService.Get(bar);
  
              return View(foo);
          }
      }
  </code></pre>
  
  <p>I am using the Unity MVC4 Bootstrapper to manage the injections:</p>
  
  <pre><code>  public static class Bootstrapper
      {
  
          public static IUnityContainer Initialise()
          {
              var container = BuildUnityContainer();
  
              DependencyResolver.SetResolver(new UnityDependencyResolver(container));
  
              return container;
          }
  
          private static IUnityContainer BuildUnityContainer()
          {
              var container = new UnityContainer();
  
              container.RegisterType&lt;IStackOverflowService, StackOverflowService&gt;("DefaultStackOverflowService");
  
  
              return container;
          }
  
  
      }
  </code></pre>
  
  <p>So far so good, the injections work. Where I get stuck is to now easily based on either configuration or some variable I want to be able to change the implementation of the <code>StackOverflowService</code>.</p>
  
  <p>So for instance if the url = "somethingelse.com" I want to be able to automatically inject the CustomStackOverflowService which inherits from IStackOverflowService. But this is where I get lost. I tried using Named Registration but I can't seem to work out how I would manually resolve the correct service to implement based on some alternate criteria.</p>
  
  <p>If anyone could please help that would be epic :)</p>
  
  <p><strong>EDIT:</strong> The criteria for the change of implementation would ideally be figured out in realtime, so say based on the actual url that is currently being visited. It's a multi-tennanted environment so it could be something like <code>domain.com/stackoverflow</code> vs <code>domain2.com/stackoverflow.</code> The implementation needs to be different per site. I don't even know if this is possible to be honest or how to go about that. I am fairly new to the whole IoC subject.</p>
  
  <p><strong>EDIT 2:</strong> I have gotten a little bit further with this and managed to manually invoke a particular implementation. I am now looking at using a Custom Controller Factory to resolve what implementation to use, but I am not sure if this is the right way to go, or if there is an easier way of doing this.</p>
  
  <p>I need to keep in mind that as more clients come on board I need to be able to quickly add more implementations for each new clients. So ideally I would have some easy way to manage the methods for a particular client.</p>
  
  <p><strong>EDIT 3:</strong> Further update, I have rewritten my controller to allow for a constructor to be injected to determine the dependency. However my initial issue is still valid, as this fires when the controller is constructed, I don't have access to the request, and can therefore not determine the URL to resolve the dependency. Any ideas would be appreciated.</p>