Automapper: map from a non-polymorphic list to a polymorphic list

My models:

class SourceViewModel
{
    bool IsCpmplex {get;set;}
    String SimpleProp {get;set;}
    String ComplexProp {get;set;}
}
class TargetSimpleModel
{
    String SimpleProp {get;set;}
}
class TargetComplexModel : TargetSimpleModel
{
    String ComplexProp {get;set;}
}

I want to match List<SourceViewModel>with List<TargetSimpleModel>with inheritance in the target list. Inheritance is based on the original Peoperty IsCopmplex (it should be used as a discriminator).

An example of the result.

If the list of sources is as follows:

{
    SourceViewModel{IsComplex=true,...},
    SourceViewModel{IsComplex=false,...}
}

Dest list should be:

{
    TargetComplexModel, // with all props mapped 
    TargetSimpleModel
}

My current confg (not working correctly):

Mapper.CreateMap<SourceViewModel, TargetSimpleModel>()
    .ConstructUsing(model => model.IsComplex ? new TargetComplexModel() : new TargetSimpleModel())
    .Include<SourceViewModel, TargetComplexModel>()
Mapper.CreateMap<SourceViewModel, TargetComplexModel>();

It works, but does not display the TargetComplexModel properties (e.g. ComplexProp). I need a way to force automapper to use a spatial map for a derived type when ConstructUsing returns it.

Any ideas?

+4
source share