title: Unity Container Multiple Implementations of same interface
body: <p>I'm studying up on the unity containers and have a quick question on how to resolve a class's construction to multiple different implementations of an interface.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public interface IRenderer
  {
      void DrawSquare(Square square);
      void DrawCircle(Circle circle);
  }
  
  public interface IShape
  {
      void Draw(IRenderer renderer);
  }
  
  public class Dx11Renderer : IRenderer
  {
      public void DrawSquare(Square square)
      {
      }
  
      public void DrawCircle(Circle circle)
      {
      }
  }
  
  public class GlRenderer : IRenderer
  {
      public void DrawSquare(Square square)
      {
      }
  
      public void DrawCircle(Circle circle)
      {
      }
  }
  
  public class Circle : IShape
  {
      public void Draw(IRenderer renderer) { renderer.DrawCircle(this); }   
  }
  
  public class Square
  {
      public void Draw(IRenderer renderer) { renderer.DrawSquare(this); }   
  }
  
  public class Canvas
  {
      private readonly IRenderer _renderer;
  
      private List&lt;Circle&gt; _circles = new List&lt;Circle&gt;();
      private List&lt;Square&gt; _squares = new List&lt;Square&gt;(); 
  
      public Canvas(IRenderer renderer)
      {
          _renderer = renderer;
      }
  
      public void Draw()
      {
          foreach (Circle c in _circles)
          {
              c.Draw(_renderer);
          }
  
          foreach (Square s in _squares)
          {
              s.Draw(_renderer);
          }
      }
  }
  </code></pre>
  
  <p>and to register/resolve</p>
  
  <pre><code>        // Create the container
          var container = new UnityContainer();
  
          // registration
  
          container.RegisterType&lt;IRenderer, GlRenderer&gt;("GL");
          container.RegisterType&lt;IRenderer, Dx11Renderer&gt;("DX11");
  
          Canvas canvas = container.Resolve&lt;Canvas&gt;("GL");
  </code></pre>
  
  <p>This throws a "ResolutionFailedException" so I must be using this incorrectly. </p>
  
  <p>Can someone explain if this is bad practice, or how I can achieve this.</p>
  
  <p>Thanks</p>
  
  <p>UPDATE:</p>
  
  <p>So what I have done is registered Canvas twice with each type of dependencies like so:</p>
  
  <pre><code>// Canvas with an OpenGL Renderer
  container.RegisterType&lt;Canvas&gt;("GLCanvas", new InjectionConstructor(new ResolvedParameter&lt;IRenderer&gt;("GL")));
  // Canvas with a DirectX Renderer
  container.RegisterType&lt;Canvas&gt;("DXCanvas", new InjectionConstructor(new ResolvedParameter&lt;IRenderer&gt;("DX11")));
  
  Canvas canvas = container.Resolve&lt;Canvas&gt;("GLCanvas");
  </code></pre>
  
  <p>This works well for me!</p>