Wondering the best way to handle this:
public class SrcClass{
public int? Value1 {get;set;}
public int? Value2 {get;set;}
}
public class DestClass{
public DestClass(int? value1 = 1){
Value1 = value1
}
public int Value1 {get;set;}
public int Value2 {get;set;
}
Mapper.createMap<SrcClass,DestClass>()
.ConstructUsing( src => new DestClass(src.Value1));
var destObject = Mapper.Map<DestClass>(new SrcClass());
Now the problem is that destObject.Value1 is null instead of 1 since AutoMapper set the value back to the value of the src object after the constructor is run. The way I see it, I have two bad options:
- I can repeat the default value in the
createMapmethod. But this will introduce bugs later if someone adds or changes a default value in the constructor forSrcClassand fails to add that into the mapper configuration - I can add an ignore of
Value1after theConstructUsing, but this suffers from the same problem as option 1. If someone comes in and adds in a new value with a default parameter, but fails to add it to the mapper, it will be a source of a bug.
What I'd really like to do is somehow tell Automapper to only map properties that weren't passed in during the constructor. Not really sure that it's even possible for the framework to handle something like that. Any other ideas?
src.Value1as the parameter but then expect AutoMapper not to use it? Or do you want AutoMapper not to use it ifsrc.Valueis null?DestClasscould do some logic inside of it to set a bunch of parameters. I think explicit ignoring is the only way to goDestClass.Value1andDestClass.Value2be nullable?