title: How does IoC container know which named instance to inject?
body: <p>When there are multiple named implementations for a given interface, how does the container (I am using Unity in a Prism application) know which one to inject unless I call the container.Resolve with the registered name?  Here is a simple example:</p>
  
  <pre><code>public interface IDependencyClass
  {
      void DoSomething();
  }
  
  public class DependencyClassA : IDependencyClass
  {
      void DoSomething() { }
  }
  
  public class DependencyClassB : IDependencyClass
  {
      void DoSomething() { }
  }
  
  public interface IConsumer
  {
      void TakeUserSpecificAction();
  }
  
  public class Consumer : IConsumer
  {
      IDependencyClass dependencyInstance;
      public Consumer(IDependencyClass _dependencyInstance)
      {
          dependencyInstance = _dependencyInstance;
      }
  
      public void TakeUserSpecificAction()
      {
          dependencyInstance.DoSomething();
      }
  }
  
  public class MyBootStrapper : UnityBootstrapper
  {
  
      protected override void ConfigureContainer()
      {
          base.ConfigureContainer();
          Container.RegisterType&lt;IDependencyClass, DependencyClassA&gt;( "InstanceA" );
          Container.RegisterType&lt;IDependencyClass, DependencyClassB&gt;( "InstanceB" );
          Container.RegisterType&lt;IConsumer, Consumer&gt;();
      }
  }
  </code></pre>
  
  <p>and here is my MainViewModel from my application.  The "RaiseSomeCommand" command is not enabled until the user has logged in.  When it is enabled, it can execute the ReaiseConsumerCommandRequest, which in turn calls the consumer.  Here is my ViewModel.</p>
  
  <pre><code>public class MainWindowViewModel
      {
          private readonly IRegionManager regionManager;
          private readonly ILoginService loginService;
          private readonly IConsumer consumer;
          public ICommand RaiseSomeCommand { get; set; }
          public MainWindowViewModel( IRegionManager regMgr, ILoginService _loginService, IConsumer _consumer )
          {
              regionManager = regMgr;
              loginService = _loginService;
              consumer = _consumer;
              NavigateCommand = new DelegateCommand&lt;string&gt;( Navigate );
              LoginViewRequest = new InteractionRequest&lt;INotification&gt;();
              RaiseSomeCommand = new DelegateCommand( RaiseConsumerCommandRequest );
          }
  
  private void RaiseConsumerCommandRequest()
              {
                  consumer.TakeUserSpecificAction();
              }
  
          }
  </code></pre>
  
  <p>So, when I execute </p>
  
  <pre><code>consumer.TakeUserSpecificAction();
  </code></pre>
  
  <p>which DependencyClass instance am I using?  DependencyClassA or DependencyClassB.  Also, If I want to use specifically say DependencyClassB, What do I need to do to make it happen.  I don't want to call </p>
  
  <pre><code>container.Reslove&lt;IDependencyClass&gt;("InstanceB") 
  </code></pre>
  
  <p>in my ViewModel because I am then using the container as a service locator.  I am also passing the container reference around.</p>
  
  <p>I have seen in some code examples that the constructor parameter for the consumer class is decorated with a Dependency attribute like below.</p>
  
  <pre><code>public class Consumer
  {
      IDependencyClass dependencyInstance;
      public Consumer([Dependency("InstanceB")]IDependencyClass _dependencyInstance)
      {
          dependencyInstance = _dependencyInstance;
      }
  }
  </code></pre>
  
  <p>But then, I am putting a hard constraint on the Consumer to use only the "InstanceB" implementation. Secondly, I am creating a dependency to Unity.  Thirdly, now  I have to clone the Consumer class to use "InstanceA" Implementation.  That goes against the DRY principle.</p>
  
  <p>I have heard that these conditions are application decisions and not an IoC related logic.  I can agree with that argument.  But then, where and how in the application would I resolve the right implementation without violating one rule or another?</p>
  
  <p>I can't see how I can inject the right concrete instance unless I choose to use one of the above two options.  Container.Resolve or Dependency attribute.  Can anybody help please?</p>