xvdiff
xvdiff

Reputation: 2229

Automapping error: The type does not have a default constructor

I'm running into an error when I try to map a persistence object to a domain model which does not have a default constructor.

public class SetupGroup {

    public List<CameraDisplayMap> Mappings { get; set; }

}

public class CameraDisplayMap {

    public Camera Camera { get; private set; }
    public Display Display { get; private set; }

    public CameraDisplayMap(Camera camera, Display display)
        ...

}

Repository

public IEnumerable<CameraDisplayMap> GetSetupGroupMappings(int setupGroupId) {

    return ((IQueryable<SetupGroupPto> GetAll())
        .Where(x => x.Id == setupGroupId)
        .Select(x => x.Mappings) // returns CameraDisplayMapPto from SetupGroupPto
        .Project()
        .To<CameraDisplayMap>();
}

I've already tried registering this using mapping configurations...

Mapper.CreateMap<CameraDisplayMapPto, CameraDisplayMap>()
            .ConstructUsing(x => new CameraDisplayMap(x.Camera, x.Display));

...with both using .ConstructUsing() and .ConvertUsing(), but it seams this doesn't work with the automapper projections.

Stack:

at System.Linq.Expressions.Expression.New(Type type) <---
at [..].ProjectionExpression`1.BuildExpression[TDest]()

Upvotes: 2

Views: 4849

Answers (1)

xvdiff
xvdiff

Reputation: 2229

Bogard, you rock! The most recent version of Automapper now supports projection conversions:

http://lostechies.com/jimmybogard/2014/12/23/automapper-3-3-feature-projection-conversions/

For anyone stumbling upon this, as of Version 3.3 of Automapper, you can declare the projection conversions like so:

Mapper.CreateMap<CameraDisplayMapPto, CameraDisplayMap>()
      ---->   .ConstructProjectionUsing(x => new CameraDisplayMap(x.Camera, x.Display));

Upvotes: 4

Related Questions