AutoMapper使用笔记

  1.  创建和使用Map
    Mapper.CreateMap<SourceType, DestinationType>()
    
            .ForMember(dest=> dest.Property, opt => opt.MapFrom(src => src.OtherProperty))
    
            .ForMember(dest => dest.IgnoreProperty, opt => opt.Ignore());
    
    

      然后就可以使用这个Map了:

    DestinationType dest = Mapper.Map<SourceType, DestinationType>(source);
  2. Mapper.AssertConfigurationIsValid(); 用于检查映射是否完整,Destinate model的没被忽略的字段必须全都被映射到位,否则就会被这个方法给报出错误来!
  3. 如果想忽略所有的不存在对应的属性,可以写一个扩展方法:
    public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>
    
    (this IMappingExpression<TSource, TDestination> expression)
    
            {
    
                const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
    
                var sourceType = typeof (TSource);
    
                var destinationProperties = typeof (TDestination).GetProperties(flags);
    
    
    
                foreach (var property in destinationProperties)
    
                {
    
                    if (sourceType.GetProperty(property.Name, flags) == null)
    
                    {
    
                        expression.ForMember(property.Name, opt => opt.Ignore());
    
                    }
    
                }
    
                return expression;
    
            }

    使用方式:

    Mapper.CreateMap<SourceType, DestinationType>().IgnoreAllNonExisting()
    
                    .ForMember(prop => x.Property, opt => opt.MapFrom(src => src.OtherProperty));

     改正:InnoreAllNonExisting这个扩展方法最好不要放在ForMember之后,因为ForMember主要用于手动映射一些名称不一样的属性,映射好了后你又来个IgnoreAll, 得~,手动映射全部失效,白搭了,所以最好是放在在CreateMap后。

  4. 如果你想在使用Map的时候不创建一个新的对象,该怎么做呢? 很简单:

    Mapper.Map<Source, Destination>(source, destination);

     

  5. AutoMapper也是可以轻松转换List的:

    Mapper.CreateMap<SourceType, DestinationType>();
    
    List<Destination> destList = Mapper.Map<List<Source>, List<Destination>(sourceList); 
  6.  既然知道怎么创建映射了,也知道怎么使用映射了,我们可以再写一些扩展方法,把映射转换放在扩展方法里,这样来调用的代码更为简洁,都看不到Mapper的踪影,举个例子:

    public static class MapperExtensions
    
        {
    
            // Company
    
            public static Company ToEntity(this CompanyViewModel model)
    
            {
    
                return Mapper.Map<CompanyViewModel, Company>(model);
    
            }
    
    
    
            public static Company ToEntity(this CompanyViewModel model, Company entity)
    
            {
    
                return Mapper.Map(model, entity);
    
            }
    
    
    
            public static CompanyViewModel ToModel(this Company entity)
    
            {
    
                return Mapper.Map<Company, CompanyViewModel>(entity);
    
            }
    
    
    
            public static List<CompanyViewModel> ToModelList(this List<Company> entities)
    
            {
    
                return Mapper.Map<List<Company>, List<CompanyViewModel>>(entities);
    
            }
    
    }
    
    

      调用方式:

    var model = entity.ToModel(); 
    
    var entity = model.ToEntity();
    
    
    
    var entity = model.ToEntity(entity);
    
    

      

你可能感兴趣的:(mapper)