title: MVC Mapping layer accessing Service layer
body: <p>I have designed my application (ASP.NET MVC with EF6) so there is a Mapping layer responsible of holding mappings from Entities to ViewModels back and forth. It is used by the Controllers, so basically the usual Edit action looks like </p>
  
  <pre><code>var entity = DataService.Get(viewModel.Id);
  entity = Mapper.MapToEntity(viewModel, entity);
  DataService.Update(entity);
  </code></pre>
  
  <p>The base MapToEntity implementation uses AutoMapper to map the basic properties of the object.</p>
  
  <p>Update() is implemented in EF6 as a function which marks the entity as modified and calls the underlying context SaveChanges().</p>
  
  <p>However, for more complex mappings, like entities with related entities, e.g. an Order, the Mapper class performs more operations. For example, updating the related entity objects (OrderDetails) with the ViewModel related object data, assigning the parent Id (OrderId) to the new child objects, and finally the problematic issue: deleting a child object.</p>
  
  <p>Right now, my specialised Mappers have a reference to the Service Object of the related entity, which has a MarkEntityToDelete function, called just to mark in the underlying DbSet that entity for deleting.</p>
  
  <pre><code>foreach (var orderDetail in viewModel.OrderDetails.Where(d =&gt; d.Id != 0 || d.Delete == false))
  {
      var orderDetailToUpdate = orderDetail.Id == 0 ? new OrderDetail() : _orderDataService.GetOrderDetail(orderDetail.Id);
  
      if (orderDetail.Delete)
      {
          _orderDataService.MarkLineaAsDeleted(orderDetailToUpdate);
      }
      else
      {
          Mapper.Map(orderDetail, orderDetailToUpdate);
  
          if (orderDetail.Id != 0) continue;
  
          orderDetailToUpdate.MantenimientoId = order.Id;
          order.OrderDetails.Add(orderDetailToUpdate);
      }
  }
  </code></pre>
  
  <p>This is a dependency which is only there because the implementation uses EF6 and the transaction border is inside the Service, so the Mapper cannot directly delete the object, just mark it for deleting.</p>
  
  <p>Although this solution works well in practice, I don't particularly like it very much. I don't think the Mapper object should have a dependency on a Service object in this way, just to mark a child object for deleting.
  I tried to use the following code:</p>
  
  <pre><code>entity.EntregasCuenta.Remove(entity.EntregasCuenta.Single(e =&gt; e.Id == entregaCuenta.Id));
  </code></pre>
  
  <p>but it gives a foreign key check error, which is expected, as I don't delete the related entity but the relationship.</p>
  
  <p>So my question is, how should the mapper deal with this situation? Mapping from a ViewModel to the Entity should be done by a Mapping layer, but updating the related entity should be done by a Service. Finally, the Controller responsibility should be only to call the three operations in order, that is, retrieve entity (service), map viewmodel data to entity (mapping layer), update changes (service).</p>
  
  <p>Am I missing something? </p>