title: Injecting dependency into DTO
body: <p><strong>Introduction</strong></p>
  
  <p>I'm using ASP.Net MVC3. My Controllers talk to a service layer, and the service layer talks to a Data Acces layer which uses Entity Framework.
  I get a specific entity using Entity Framework. This entity is converted into a DTO. Then I deliver this DTO to a MVC controller. Something like this:</p>
  
  <p>pseudo code:</p>
  
  <pre><code>// This is inside my Service Layer
  var entity = DataAccess.GetById(id);
  var dto = createDtoWithValuesFrom(entity);
  return dto; // Return dto to MVC controller
  </code></pre>
  
  <p>In this DTO I would like to use a dependency, to for example a Calculator. Let's say my DTO looks like this:</p>
  
  <pre><code>public class Customer
  {
    private ICalculator Calculator;
  
    public class Customer(ICalculator calculator)
    {
       Calculator = calculator;
    }
  
    public string Name { get; set; }
    public decimal Discount
    {
       get
       {
          return Calculator.Discount();
       }
    }
  }
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>How do I instanciate my DTO, and let Autofac inject a calculator?
  I can think of a way to do this:</p>
  
  <pre><code>var calculator = DependencyResolver.Current.GetService&lt;ICalculator&gt;;
  var dto = new DTO(calculator );
  </code></pre>
  
  <p>But I don't know if this is the best way to do it, since it smells of ServiceLocator, and I've read that it's not prefered to use that.</p>