Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using automapper to apply a filter to a collection

Tags:

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.

like image 955
stevie_c Avatar asked Jun 03 '11 11:06

stevie_c


People also ask

Can AutoMapper map collections?

Polymorphic element types in collectionsAutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.

When should I use AutoMapper?

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.

Is AutoMapper worth using?

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!

What is AutoMapper good for?

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.


2 Answers

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".

like image 188
achekh Avatar answered Sep 17 '22 22:09

achekh


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.

like image 23
ataddeini Avatar answered Sep 19 '22 22:09

ataddeini