I'm trying to find the correct way to implement this.
return isOptionTrue? Mapper.Map<Context>(myObject) : Mapper.Map<ReplayContext>(myObject);
Context has all the fields I actually need.
public class ContextReplay: Context
{
//This class is a work around to trick Automapper
}
Then of course the mappings
Mapper.CreateMap<myObject, Context>()
.ForMember(x => x.Id, opt => opt.MapFrom(y => y.Id))
.ForMember(x => x.Name, opt => opt.MapFrom(y => y.Name));
.ForMember(x => x.Important, opt => opt.MapFrom(y => y.NormalProp));
Mapper.CreateMap<myObject, ContextReplay>()
.ForMember(x => x.WellId, opt => opt.MapFrom(y => y.Id))
.ForMember(x => x.Name, opt => opt.MapFrom(y => y.Name));
.ForMember(x => x.Important, opt => opt.MapFrom(y => y.ReplayProp));
As you can see, I really want:
Mapper.CreateMap<myObject, Context>()
.ForMember(x => x.WellId, opt => opt.MapFrom(y => y.Id))
.ForMember(x => x.Name, opt => opt.MapFrom(y => y.Name));
.ForMember(x => x.Important, opt => opt.MapFrom(y => isOptionTrue? y.NormProp: y.ReplayProp));
For questions: Yes i have an empty sublcass, becuase I cont have two unique MyObject to Context mappings. This whole thing seems like a super hacky work around. I can't imagine they really don't support something like this, but im at a loss. I have no idea how to get my bool passed along with it.
Perhaps my imagination is limited, but personally I find "auto mapping" to be of limited use, except when mapping between two "identical" classes. Wouldn't it be more straight-forward, faster, and easier to maintain the following bit of code?
var context = new Context {
WellId = myObject.Id,
Name = myObject.Name,
Important = (isOptionTrue ? myObject.NormProp : myObject.ReplayProp)
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.