I have a domain model that contains a collection and I want to use AutoMapper to map the parent and children to the view model but I don't want children that have been "soft" deleted to be taken across. For instance:
public class Customer { public EntitySet<Order> {get;set;} } public class Order { public DateTime? DeletedDate {get;set;} }
my AutoMapper definition would be
Mapper.CreateMap<Customer, CustomerViewModel>(); Mapper.CreateMap<Order, OrderViewModel>();
and I don't want Orders to be in the view model that have a value for DeletedDate.
Is that possible in AutoMapper? Many thanks in advance,
Steve.
Polymorphic element types in collectionsAutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.
AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.
AutoMapper will save you writing a LOT of boring mapping code and it will probably spare you from a few nasty bugs as well. The only thing you must be aware of is that the mapping uses convensions and you really want to follow these. As long as you do that, AutoMapper is a great tool!
AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.
I came across similar issue and finally the approach similar to the one below worked for me:
Mapper.CreateMap<Customer, CustomerViewModel>() .ForMember(dest => dest.Orders, opt => opt.MapFrom(src => src.Orders.Where(o => !o.DeletedDate.HasValue)));
This assumes your Customer entity and CustomerViewModel dto have collections named "Orders".
This sounds like it would be a good fit for a custom ValueResolver. It will allow you to do your logic checks in an isolated fashion. I don't have Visual Studio in front of me right now, but I can add some sample code later if you'd like.
EDIT: After tinkering with this I don't think a ValueResolver is the way to go. I was able to get it to work by using the following conditional configuration for the Order
mapping:
Mapper.CreateMap<Order, OrderViewModel>() .ForAllMembers(opt => opt.Condition(src => !src.DeletedDate.HasValue));
The only thing with this is that theOrderViewModel
will still come over but it will be null
. In other words if you had 3 orders, and one had a deletion date, then the number of orders you will have in your view model will still be 3, but the deleted value will be null
. I'm guessing it would be best to just have 2, but I'm not seeing a clear way to do that right now.
Here's a post with a response from the author of AutoMapper that talks about a Skip
method, but I wasn't able to see that feature in the latest release that I'm using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With