title: .NET MVC3 Service Locator / Dependency Resolver Question With Ninject
body: <p>I have what I'd consider a standard .NET MVC3 Repository pattern project that I've been playing/learning with. It's pretty standard structure.</p>
  <ul>
  <li>Repository project (with the below mentioned Caching infrastructure)</li>
  <li>Domain Model Project</li>
  <li>Service Layer Project</li>
  <li>MVC Presentation project</li>
  </ul>
  <p>I've run into a scenario where I need to inject a private member of a class that only has a static constructor which leaves me out of luck for constructor injection.</p>
  <p>The class in question is a wrapper to use an AppFabric caching implementation that I just completed. (For those so inclined, my implementation is based on
  <a href="https://github.com/geersch/AppFabric" rel="nofollow noreferrer">https://github.com/geersch/AppFabric</a> )</p>
  <p>Essentially I have:</p>
  <ul>
  <li>interface ICacheProvider</li>
  <li>class DefaultCacheProvider : ICacheProvider</li>
  <li>static class Cache (utilizing whatever implementation I've injected)</li>
  </ul>
  <p>The static class cache is where I'd like to inject an ICacheProvider that gets resolved to DefaultCacheProvider.</p>
  <pre><code>    private static readonly ICacheProvider CacheProvider;
  
      static Cache()
      {
          //DependencyResolver.Current.GetService&lt;ICacheProvider&gt;();
  
          //CacheProvider =
          //    (ICacheProvider)ServiceLocator.Current
          //                            .GetInstance(typeof(ICacheProvider));
      }
  
      public static void Add(string key, object value)
      {
          CacheProvider.Add(key, value);
      }
  
      public static void Add(string key, object value, TimeSpan timeout)
      {
          CacheProvider.Add(key, value, timeout);
      }
  
      public static object Get(string key)
      {
          return CacheProvider[key];
      }
  
      public static bool Remove(string key)
      {
          return CacheProvider.Remove(key);
      }
  </code></pre>
  <p>Based off what I've read, this seems like a scenario for ServiceLocator but I've seen some very strong opinions on it (anti pattern, etc. etc), that and my familiarity with it is low
  so I'm unsure of an implementation that would work.</p>
  <p>I've seen the recommendation on StackOverflow to design the Cache class as a standard class and inject the ICacheProvider in SingletonScope</p>
  <pre><code>kernel.Bind&lt;ICacheProvider&gt;().To&lt;DefaultCacheProvider&gt;().InSingletonScope();
  </code></pre>
  <p>but I personally would prefer a static wrapper for ease of use.</p>
  <p>Is a ServiceLocator setup the way to go here or is there something else obvious that I'm unaware of? If ServiceLocator is the way to go, is there any tie-in with Ninject to utilize? I
  know Ninject now has service locator capabilities but was unsure how to implement.</p>
  <p>Thanks for any info.</p>