title: Dependency injection or service location?
body: <p>I'm trying to learn dependency injection, and there are many subtleties to it I'm yet to grasp. One of the books that I've started reading for that purpose is <em>"<a href="http://codebetter.com/karlseguin/2008/06/25/foundations-of-programming-ebook/" rel="nofollow">Foundations of Programming</a>"</em> by Karl Seguin. There's an example about dependency injection:</p>
  
  <pre><code>public class Car
  {
      private int _id;
  
      public void Save()
      {
          if (!IsValid())
          {
              //todo: come up with a better exception
              throw new InvalidOperationException("The car must be in a valid state");
          }
  
          IDataAccess dataAccess = ObjectFactory.GetInstance&lt;IDataAccess&gt;();
          if (_id == 0)
          {
              _id = dataAccess.Save(this);
          }
          else
          {
              dataAccess.Update(this);
          }
      } 
  }
  </code></pre>
  
  <p>And then he goes ahead and suggest adding another level of indirection, rather than calling <code>ObjectFactory</code> directly in the method:</p>
  
  <pre><code>public static class DataFactory
  {
      public static IDataAccess CreateInstance
      {
          get
          {
              return ObjectFactory.GetInstance&lt;IDataAccess&gt;();
          }
      }
  }
  </code></pre>
  
  <p>But isn't this <em>"Service Location"</em> in fact?</p>