Resolve Service Implementation from Autofac based on Runtime Session Value
<p>Need some help trying to solve a problem resolving an implementation of a service at runtime based on a parameter. In other words use a factory pattern with DI.  </p>

<p>We have Autofac wired in to our MVC application.  I am trying to figure out how we can use a user session variable (Call it Ordering Type) to be used for the Dependency Resolver to resolve the correct implementation of a service.  </p>

<p>An example of what we are trying to do.</p>

<p>The application has two "types" of ordering - real eCommerce type of ordering (add stuff to a shopping cart, checkout etc).  </p>

<p>The other is called Forecast ordering.  Users create orders - but they do not get fulfilled right away.  They go through an approval process and then fulfilled.  </p>

<p>The bottom line is the data schema and back end systems the application talks to changes based on the order type.  </p>

<p>What I want to do is:</p>

<ol>
<li><p>I have IOrderManagerService</p>

<pre><code>public interface IOrderManagerService
{
      Order GetOrder(int orderNumber);
      int CreateOrder(Order order);
}
</code></pre></li>
<li><p>Because we have two ordering "types" - I have two implementations of the the IOrderManagerService:</p>

<pre><code>public class ShelfOrderManager : IOrderManagerService
{
    public Order GetOrder(int orderMumber)
    {
         ...code
    }

    public int CreateOrder(Order order)
    {
        ...code
    }
}
</code></pre></li>
</ol>

<p>and </p>

<pre><code>    public class ForecastOrderManager: IOrderManagerService
    {
        public Order GetOrder(int orderMumber)
        {
             ...code
        }

        public int CreateOrder(Order order)
        {
            ...code
        }
    }
</code></pre>

<ol start="3">
<li><p>My First question is - in my MVC application - do I register these implementations as?</p>

<pre><code>builder.RegisterType&lt;ShelfOrderManager&gt;().As&lt;IOrderManagerService&gt;();
builder.RegisterType&lt;ForecastOrderManager&gt;().As&lt;IOrderManagerService&gt;();
</code></pre></li>
<li><p>What we are planning on doing is sticking the user selected ordering type in a users session.  When a user wants to view order status - depending on their selected ordering "type" - I need the resolver to give the controller the correct implementation.</p>

<pre><code>public class OrderStatusController : Controller
{
      private readonly IOrderManagerService _orderManagerService;

      public OrderStatusController(IOrderManagerService orderManagerService)
      {
           //This needs to be the correct implementation based on the users "type".  
           _orderManagerService = orderManagerService; 
      }

      public ActionResult GetOrder(int orderNumber)
      {
           var model = _orderManagerService.GetOrder(orderNumber);
           return View(model);
      }
}
</code></pre></li>
</ol>

<p>I've ready about the <a href="http://docs.autofac.org/en/latest/advanced/delegate-factories.html" rel="nofollow noreferrer">the delegate factory</a> and <a href="https://stackoverflow.com/questions/25253381/autofac-runtime-parameters">this answer</a> explains the concept well.  </p>

<p>The problem is the runtime parameters are being used to construct the service and resolve at runtime.  i.e. </p>

<pre><code>     var service = resolvedServiceClass.Factory("runtime parameter")
</code></pre>

<p>All this would do is give me "service" that used the "runtime parameter" in the constructor.</p>

<p>I've looked at <a href="http://docs.autofac.org/en/latest/advanced/keyed-services.html" rel="nofollow noreferrer">Keyed or Named</a> resolution too. </p>

<p>At first I thought I could combine these two techniques - but the controller has the dependency on the interface - not the concrete implementation.  (as it should)</p>

<p>Any ideas on how to get around this would be <strong>MUCH</strong> appreciated. </p>