title: Avoiding Service Locator with AutoFac 2
body: <p>I'm building an application which uses AutoFac 2 for DI.  I've been <a href="http://code.google.com/p/autofac/wiki/BestPractices" rel="nofollow noreferrer">reading</a> that using a static IoCHelper (Service Locator) should be avoided.</p>
  
  <p><strong>IoCHelper.cs</strong></p>
  
  <pre><code>public static class IoCHelper
  {
      private static AutofacDependencyResolver _resolver;
  
      public static void InitializeWith(AutofacDependencyResolver resolver)
      {
          _resolver = resolver;
      }
  
      public static T Resolve&lt;T&gt;()
      {
          return _resolver.Resolve&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p>From answers to a <a href="https://stackoverflow.com/questions/2726606/issue-with-autofac-2-and-mvc2-using-httprequestscoped/2735667#2735667">previous question</a>, I found a way to help reduce the need for using my IoCHelper in my UnitOfWork through the use of <a href="http://code.google.com/p/autofac/wiki/NewInV2" rel="nofollow noreferrer">Auto-generated Factories</a>.  Continuing down this path, I'm curious if I can completely eliminate my IoCHelper.</p>
  
  <p>Here is the scenario:</p>
  
  <p>I have a static Settings class that serves as a wrapper around my configuration implementation.  Since the Settings class is a dependency to a majority of my other classes, the wrapper keeps me from having to inject the settings class all over my application.</p>
  
  <p><strong>Settings.cs</strong></p>
  
  <pre><code>public static class Settings
  {
      public static IAppSettings AppSettings
      {
          get
          {
              return IoCHelper.Resolve&lt;IAppSettings&gt;();
          }
      }
  }
  
  public interface IAppSettings
  {
      string Setting1 { get; }
      string Setting2 { get; }
  }
  
  public class AppSettings : IAppSettings
  {
      public string Setting1
      {
          get
          {
              return GetSettings().AppSettings["setting1"];
          }
      }
  
      public string Setting2
      {
          get
          {
              return GetSettings().AppSettings["setting2"];
          }
      }
  
      protected static IConfigurationSettings GetSettings()
      {
          return IoCHelper.Resolve&lt;IConfigurationSettings&gt;();
      }
  }
  </code></pre>
  
  <p>Is there a way to handle this without using a service locator and without having to resort to injecting AppSettings into each and every class?  Listed below are the 3 areas in which I keep leaning on ServiceLocator instead of constructor injection:</p>
  
  <ul>
  <li>AppSettings</li>
  <li>Logging</li>
  <li>Caching</li>
  </ul>