tags:

views:

351

answers:

2

There was a very interesting discussion on LosTechies regarding AutoMapper(an argument for/against 2-way mapping).

This actually caught my attention due to the problem I'm currently working through. I'm working on a shipment piece to provide information such as rates/delivery times to my users. To centralize the actual services, I have a WCF web service which persists any domain entities.

To simplify the domain model I basically have 2 classes:

public class Shipment
{
 public IList<Item> Items{get;set;}
}

public class Item
{
 //some primitive properties
}

I also have a corresponding set of DTOs that were created to lighten the load across the wire. The presentation piece(or pieces, whatever touches the web service), use the DTOs with no knowledge of the domain model.

My question comes here. To create a shipment, the service accepts a list of items. There is logic to creating shipments, which is all hidden behind the web service. In essence, this means that ItemDTO is passed across the wire(client -> server), shipments are created, then ShipmentDTO is passed back(server -> client). Now, ShipmentDTO has a child list of ItemDTO as well, which creates the 2-way mapping scenario.

This is more than a simple CRUD operation and I'm farely new to the Command Message pattern, so I'm curious how the community would solve this issue.

Do you pass a DTO both ways with 2-way mapping?

Sample usage(presentation layer):

List<ItemDTO> list = new List<ItemDTO>();
//add items to list


ShipmentServiceClient client = new ShipmentServiceClient();
List<ShipmentDTO> shipments = client.GetShipments(list);

//shipments are now displayed to the user
//with respective costs and other useful data
A: 

At risk of missing the point of your question, I'll say that it is OK to use a DTO bi-directionally, so long as you're not re-using the DTO for the wrong reason. In your case, is the request to the server to create a shipment for a bunch of items fully populating each ItemDTO, so that when the server gets it back, the ItemDTOs are basically the same? Or, are you really requesting to ship several items, but the ItemDTO that comes back has some additional details filled in by the server (e.g. per-item shipping cost, inventory status, etc.)? If the latter case, I would say you shouldn't re-use the ItemDTO to represent both a particular item being requested for shipment and the shipping details about an item.

Trinition
The ItemDTO is actually populated(or chosen) by the client. The ShipmentDTO is what carries cost, etc and is populated by the server. My issues resulted from the AutoMapper discussion, where it seems to be best practice to use 1-way mapping as there is always a way to not insitute something bi-directional.
Alexis Abril
A: 

Something that was mentioned in that post that I didn't quite grasp was the wording in a response. "Incoming DTOs are mapped to command messages". Saying, DTOs can be bidirectional, the mappings(AutoMapper) provided, however are unidirectional.

Alexis Abril