views:

237

answers:

1

I'm updating or creating an entity that has a child relationship, say the aggregate root is Product (ProductId, Title) which has zero or more ProductSuppliers (ProductSupplierId, SuppliedAtPrice, SupplierInfoId), and the DTO represents a similar structure (all the info). Simple enough.

I've created a simple map for ProductDTO and ProductSupplierDTO and it works I guess as intended for new entities.

However when processing the DTO, I might be updating an existing entity, so I do something like this:

Product product = productService.GetViaProductId(productDTO.ProductId) ?? new Product();
productDTOMapper.Map(productDTO, product);
productService.Update(product);

For primitive types existing on Product, its fine as any ORM will recognize if the values are dirty or not. However I don't want Automapper to just replace Product.Suppliers with the new set, I want to plug in some logic somewhere to iterate over product.Suppliers and check if an entity already exists and update that, or create a new one, ProductSupplier is not a value object, it has an Id, ProductSupplierId.

I can't find where to plug this in within the mapping.

Any suggestions?

+2  A: 

In case you ever want to go back, the UseDestinationValue() option is how you tell AutoMapper to not replace destination property values.

.ForMember(dto => dto.SomeCollection, opt => opt.UseDestinationValue())

Jimmy Bogard

related questions