Automapper Conditional Collection Mapping

Does anyone know a way (or a workaround) to conditional map a source's property in per map condition?

The purpose here is to conditionally map a collection of child objects based on a web service operation parameter. Such as:

Parent GetParent(bool includeChildren);

So far the only feasible solution I found was to either create a wrapping class to add a boolean property, like:

public class ParentMapper
{
    Parent Parent;
    public bool IncludeChildren {get;set;}
}

Or to add directly an IncludeChildren property on the model source class, witch I really don't like because of the mixing of purposes.

A perfect solution would be something like:

TDestination Map<TSource, TDestination>(TSource source, bool includeCollections);

But I don't think I'll get any luck with an efficient solution for that.

Any help would be appreciated...

Answers


Currently there is nothing built-in allowing you to achieve this. You could do the following though:

var destinations = Mapper.Map<List<Parent>, List<ParentDto>>(
    sources.Where(source => source.Child !=null)
);

Another way of doing this is:

config.CreateMap<Parent, ParentDto>()
            .AfterMap((source, dest) =>
            {
                 if (source.Child !=null)
                 {
                     //do some stuff here
                 }
            });

Need Your Help

Stuck in Linkedin login via Webview

android exception linkedin linkedin-api

Actually my app is using linkedin login via linkedin app and webview. Login with linkedin is working fine but if in case linkedin app is not available i am using webview to process login. That was ...

Java Network application cache dependencies locally

java networking

I have several Java apps that run directly from the a shared drive on the LAN. This has worked fine until recently when we setup a remote office on a VPN. The actual applications are small, but have