开发者

Automapper - Nested entities

开发者 https://www.devze.com 2022-12-08 00:47 出处:网络
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, Sup

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 a开发者_运维问答re 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?


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())

0

精彩评论

暂无评论...
验证码 换一张
取 消