title: Implementing the strategy pattern with generics and StructureMap
body: <p>I am trying to implement the strategy pattern in my repository layer using SM and generics. For that I have an interface, IPocoRepository, which has a concrete implementation using Entity Framework. This I have managed to wire up in my Bootstrapper-file:</p>
  
  <pre><code>For(typeof(IPocoRepository&lt;&gt;)).Use(typeof(EntityFrameworkRepository&lt;&gt;));
  </code></pre>
  
  <p>The problem appears when I try to implement caching for this interface. In my cached class I want an instance of the base repository class, so that I can keep my design DRY. Let me outline how these three files look:</p>
  
  <pre><code>public interface IPocoRepository&lt;T&gt;
  {
      IQueryable&lt;T&gt; GetAll();
      ...
  
  
  public class EntityFrameworkRepository&lt;T&gt; : IPocoRepository&lt;T&gt; where T : class
  {
      public IQueryable&lt;T&gt; GetAll()
      {
          ...
  
  public class CachedRepository&lt;T&gt; : IPocoRepository&lt;T&gt; where T : class
  {
      private IPocoRepository&lt;T&gt; _pocoRepository;
  
      public CachedRepository(IPocoRepository&lt;T&gt; pr)
      {
          _pocoRepository = pr;
      }
  
      public IQueryable&lt;T&gt; GetAll()
      {
          var all = (IQueryable&lt;T&gt;)CacheProvider.Get(_cacheKey);
          if (!CacheProvider.IsSet(_cacheKey))
          {
              all = _pocoRepository.GetAll();
              ...
  </code></pre>
  
  <p>Edit: I want StructureMap to return CachedRepository when IPocoRepository is requested, <em>except when requested for in CachedRepository</em> - then I want it to return EntityFrameworkRepository.</p>
  
  <p>I know this is simple when dealing with non-generic classes:</p>
  
  <pre><code>For&lt;ICountyRepository&gt;().Use&lt;CachedCountyRepository&gt;()
  .Ctor&lt;ICountyRepository&gt;().Is&lt;CountyRepository&gt;();
  </code></pre>
  
  <p>I tried searching the documentation for how to do this, but couldn't find anything. Any help would be appreciated!</p>