title: DTO's and calls between services
body: <p>Say I have two services in my service layer, <code>ServiceA</code> and <code>ServiceB</code>, each with an interface (<code>IServiceA</code> and <code>IServiceB</code> respectively).</p>
  
  <p>The UI layer only has reference to the service interfaces which return <strong>DTOs</strong> from their methods. The concrete service classes are responsible for mapping the domain models (EF POCOs) into DTOs.</p>
  
  <p><code>ServiceA</code> takes a dependency on <code>IServiceB</code> via dependency injection using an IoC container, in order to call some methods on that service.</p>
  
  <p>There are a couple of problems that arise in doing this:</p>
  
  <ol>
  <li><p>Unnecessary/duplicated mapping to and from a DTO just to call the method and/or consume the result.</p></li>
  <li><p>Tightly coupling the calling method to the DTO contracts of the called methods input parameters and return type.</p></li>
  </ol>
  
  <p>Initially I thought to refactor the logic down into an internal method and call that from both services. However as <code>ServiceA</code> takes a dependency on the interface <code>IServiceB</code> the internal methods are not exposed.</p>
  
  <p>How would you go about dealing with this issue? </p>
  
  <p><em><strong>Further information</em></strong> (added example code as requested):</p>
  
  <pre><code>// This is the domain model
  public class Customer
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  // This is a dto for the domain model
  public class CustomerDto
  {
      public string Name { get; set; }
  }
  
  // Interface for ServiceA
  public interface IServiceA
  {
      void AddCustomer();
  }
  
  // ServiceA
  public class ServiceA : IServiceA
  {
      private readonly IServiceB _serviceB;
  
      // ServiceA takes in an IServiceB as a dependency
      public ServiceA(IServiceB serviceB)
      {
          _serviceB = serviceB;
      }
  
      public void AddCustomer()
      {
          var entity = new Customer();
  
          // !! This is the key part !!
  
          // I have to map to a dto in order to call the method on ServiceB.
          // This is a VERY simple example but this unnecessary mapping 
          // keeps cropping up throughout the service layer whenever
          // I want to make calls between services.
  
          var dto = Mapper.CreateFrom&lt;CustomerDto&gt;(entity);
  
          _serviceB.DoSomethingElseWithACustomer(dto);
      }
  }
  
  // Interface for ServiceB
  public interface IServiceB
  {
      void DoSomethingElseWithACustomer(CustomerDto customer);
  }
  
  // ServiceB
  public class ServiceB : IServiceB
  {
      public void DoSomethingElseWithACustomer(CustomerDto customer)
      {
          // Some logic here
      }
  }
  </code></pre>