Automapper - Nested Objects

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

I created a simple map for ProductDTO and ProductSupplierDTO, and it works, as I assume, for future objects.

However, when processing DTO, I can update an existing object, so I am doing something like this:

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

For primitive types existing on a Product, its fine, like any ORM, will recognize if it is dirty or not. However, I don’t want Automapper to simply replace Product.Suppliers with a new set, I want to connect some logic somewhere to iterate over the product. Suppressors and check whether an entity exists and whether it updates, or create a new one, ProductSupplier is not a value object, it has an Id, ProductSupplierId.

I can not find where to connect this inside the display.

Any suggestions?

+3
source share
1 answer

If you ever want to return, the UseDestinationValue () option is how you tell AutoMapper not to replace the values ​​of the destination properties.

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

+4

Source: https://habr.com/ru/post/1720170/


All Articles