-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Construction
Jimmy Bogard edited this page Aug 5, 2015
·
7 revisions
AutoMapper can map to destination constructors based on source members:
public class Source {
public int Value { get; set; }
}
public class SourceDto {
public SourceDto(int value) {
_value = value;
}
private int _value;
public int Value {
get { return _value; }
}
}
Mapper.CreateMap<Source, SourceDto>();
If the destination constructor parameter names don't match, you can modify them at config time:
public class Source {
public int Value { get; set; }
}
public class SourceDto {
public SourceDto(int valueParamSomeOtherName) {
_value = valueParamSomeOtherName;
}
private int _value;
public int Value {
get { return _value; }
}
}
Mapper.CreateMap<Source, SourceDto>()
.ForCtorParam("valueParamSomeOtherName", opt => opt.MapFrom(src => src.Value));
This works for both LINQ projections and in-memory mapping.