0
我想这样创建的映射:AutoMapper和条件映射为null
如果源属性有它的ID == 0,设置目标属性设置为null
如果源属性有它的ID = 0,使用默认配置
例如该物业地图:
Mapper.CreateMap<ItemViewModel, Item>()
.ForMember(x => x.DestinationArticle, o => o.SetNullIfSourceHasIdZero(x => x.SourceArticle));
个
的DestinationArticle和SourceArticle特性是实现的:
public interface IEntity
{
long Id { get; set }
}
我目前的解决办法是这样的:
.ForMember(x => x.DestinationArticle, o => o.MapFrom(x => x.SourceArticle != null && x.SourceArticle.Id != 0 ? Mapper.Map<Article>(x.SourceArticle) : null))
这也不是那么好,而不是通用的。
那么,有没有什么办法可以创建,SetNullIfSourceHasIdZero扩展方法或某事之前提到的,这可能使我的生活更轻松?
这意味着我要创建我SetNullIfSourceHasIdZero对我所有的视图模型类,但那种映射情景...... – cryss 2012-03-22 15:05:42
如何: '公类SetNullIfSourceHasIdZero:ValueResolver { 保护覆盖ùResolveCore(T源) { /*逻辑* /} }' ,然后使用它利ke this: 'opt => opt.ResolveUsing >()' –
2014-11-20 08:33:31