title: Best way to use StructureMap to implement Strategy pattern
body: <p>My web app has some slight variations in business logic and presentation logic depending on the type of user that is logged in. It seems like getting variations by injecting different concrete classes based on the user type is a good fit for DI. So I'm wondering what features of StructureMap I should use to achieve this (or if I'm way off base on the purposes of DI). </p>
  
  <p>(I just learned that Profiles are not the way to accomplish this because setting the Profile isn't a per-thread operation: <a href="https://stackoverflow.com/questions/1494937/are-structuremap-profiles-thread-safe">Are StructureMap profiles thread safe?</a>)</p>
  
  <h2>EDIT</h2>
  
  <p>Is this the way to go about this?</p>
  
  <pre><code>public class HomeController
  {
      private ISomeDependancy _someDependancy;
  
      public HomeController(ISomeDependancy someDependancy)
      {
          _someDependancy = someDependancy;
      }
  
      public string GetNameFromDependancy()
      {
          return _someDependancy.GetName();
      }
  }
  
  public interface ISomeDependancy
  {
      string GetName();
  }
  
  public class VersionASomeDependancy : ISomeDependancy
  {
      public string GetName()
      {
          return "My Name is Version A";
      }
  }
  
  public class VersionBSomeDependancy : ISomeDependancy
  {
      public string GetName()
      {
          return "My Name is Version B";
      }
  }
  
  public class VersionARegistry : Registry
  {
      public VersionARegistry()
      {
          // build up complex graph here
          ForRequestedType&lt;ISomeDependancy&gt;().TheDefaultIsConcreteType&lt;VersionASomeDependancy&gt;();
      }
  }
  
  public class VersionBRegistry : Registry
  {
      public VersionBRegistry()
      {
          // build up complex graph here
          ForRequestedType&lt;ISomeDependancy&gt;().TheDefaultIsConcreteType&lt;VersionBSomeDependancy&gt;();
      }
  }
  
  public class ContainerA : Container
  {
      public ContainerA()
          : base(new VersionARegistry())
      {
      }
  }
  
  public class ContainerB : Container
  {
      public ContainerB()
          : base(new VersionBRegistry())
      {
      }
  }
  
  [TestFixture]
  public class Harness
  {
      [Test]
      public void ensure_that_versions_load_based_on_named_containers()
      {
          ObjectFactory.Initialize(c =&gt;
          {
              c.ForRequestedType&lt;IContainer&gt;().AddInstances(
                  x =&gt;
                  {
                      x.OfConcreteType&lt;ContainerA&gt;().WithName("VersionA");
                      x.OfConcreteType&lt;ContainerB&gt;().WithName("VersionB");
                  }).CacheBy(InstanceScope.Singleton);
          });
  
          HomeController controller;
  
          IContainer containerForVersionA = ObjectFactory.GetNamedInstance&lt;IContainer&gt;("VersionA");
          controller = containerForVersionA.GetInstance&lt;HomeController&gt;();
          Assert.That(controller.GetNameFromDependancy(), Is.EqualTo("My Name is Version A"));
  
          IContainer containerForVersionB = ObjectFactory.GetNamedInstance&lt;IContainer&gt;("VersionB");
          controller = containerForVersionB.GetInstance&lt;HomeController&gt;();
          Assert.That(controller.GetNameFromDependancy(), Is.EqualTo("My Name is Version B"));
      }
  }
  </code></pre>