title: Unity named mapping issue
body: <p>I have two implementations of <code>IEmailService</code>, one for testing and one for live (is-A). And I have a <code>BusinessService</code> that has-A <code>IEmailService</code> reference. </p>
  
  <pre><code>BusinessService
      IEmailService (has-A)
  
  IEmailService
      TestEmailService (is-A)
      LiveEmailService (is-A)
  </code></pre>
  
  <p>In unity config, I register the two <code>IEmailService</code> implementations as follows. </p>
  
  <pre><code>&lt;unity xmlns="http://schemas.microsoft.com/practices/2010/unity"&gt;
  &lt;container&gt;
    &lt;register type="DataAccess.IEmailService, DataAccess"
              mapTo="DataAccess.LiveEmailService, DataAccess"
              name="Live"&gt;
      &lt;lifetime type="singleton" /&gt;
    &lt;/register&gt;
    &lt;register type="DataAccess.IEmailService, DataAccess"
              mapTo="DataAccess.TestEmailService, DataAccess"
              name="Test"&gt;
      &lt;lifetime type="singleton" /&gt;
    &lt;/register&gt;
  &lt;container&gt;
  &lt;/unity&gt;
  </code></pre>
  
  <p>Based on the <code>appSetting</code> for <code>IEmailService</code> I want Unity to pick the correct implementation. This will help while testing. </p>
  
  <pre><code>&lt;appSettings&gt;
      &lt;add key="IEmailService" value="Test"/&gt;
  &lt;/appSettings&gt;
  </code></pre>
  
  <p>The issue is when unity resolves <code>BusinessService</code>, it tries to resolve <code>(none)</code> named mapping of <code>IEmailService</code> instead of <code>Live</code> or <code>Test</code> and throws an <code>ResolutionFailedException</code>.</p>
  
  <p><code>container.Resolve&lt;BusinessService&gt;();</code> throws below exception:</p>
  
  <pre><code>BusinessServices.Test.BusinessServiceTest_Integration.Test103:
  
  Microsoft.Practices.Unity.ResolutionFailedException : Resolution of the dependency failed, type = "BusinessServices.BusinessService", name = "(none)".
  Exception occurred while: while resolving.
  Exception is: InvalidOperationException - The current type, DataAccess.IEmailService, is an interface and cannot be constructed. Are you missing a type mapping?
  -----------------------------------------------
  At the time of the exception, the container was:
  
    Resolving BusinessServices.BusinessService,(none)
    Resolving parameter "emailService" of constructor BusinessServices.BusinessService(DataAccess.IEmailService emailService)
      Resolving DataAccess.IEmailService,(none)
  
    ----&gt; System.InvalidOperationException : The current type, DataAccess.IEmailService, is an interface and cannot be constructed. Are you missing a type mapping?
  </code></pre>
  
  <p>The workaround I came up with is to specify the registrations in code as well and have a wrapper method around <code>container.RegisterType</code> to register <code>IEmailService</code> with <code>(none)</code> named mapping as well based on the <code>appSetting</code> value. </p>
  
  <pre><code>IUnityContainer container;
  
  // registering unity
  static void Load()
  {
      container = new UnityContainer().LoadConfiguration();
  
      RegisterType&lt;IEmailService, TestEmailService&gt;("Test");
      RegisterType&lt;IEmailService, LiveEmailService&gt;("Live");
  }
  
  // register the `Test` or `Live` implementation with `(none)` named mapping as per appSetting
  static void RegisterType&lt;TFrom, TTo&gt;(string name) 
      where TTo : TFrom
  {
      var tFromAppSetting= ConfigurationManager.AppSettings[typeof(TFrom).Name];
      if (!string.IsNullOrEmpty(tFromAppSetting) &amp;&amp; tFromAppSetting == name)
          container.RegisterType&lt;TFrom, TTo&gt;();
  }
  </code></pre>
  
  <p>This works, but I end up specifying the registrations in two places - config as well as code. Is there a better way for doing this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I actually had got it correct by code. I do not need the unity config at all. The <code>RegisterType&lt;TFrom, TTo&gt;(string name)</code> registers <em>either</em> the <code>Test</code> or <code>Live</code> implementation as <strong><code>(none)</code></strong> named mapping depending on <code>appSetting</code> value. <code>BusinessService</code> is also resolved without exception. </p>
  
  <p>As there is no unity config, I do not have load the configuration. </p>
  
  <pre><code>container = new UnityContainer();
  </code></pre>