zoukankan      html  css  js  c++  java
  • AutoMapper使用笔记

    •  创建和使用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);
    • Mapper.AssertConfigurationIsValid(); 用于检查映射是否完整,Destinate model的没被忽略的字段必须全都被映射到位,否则就会被这个方法给报出错误来!
    • 如果想忽略所有的不存在对应的属性,可以写一个扩展方法:
      复制代码
      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后。

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

      Mapper.Map<Source, Destination>(source, destination);
    • AutoMapper也是可以轻松转换List的:

      Mapper.CreateMap<SourceType, DestinationType>();
      List<Destination> destList = Mapper.Map<List<Source>, List<Destination>(sourceList); 
    •  既然知道怎么创建映射了,也知道怎么使用映射了,我们可以再写一些扩展方法,把映射转换放在扩展方法里,这样来调用的代码更为简洁,都看不到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);
  • 相关阅读:
    Linux 相关scsi命令
    存储
    Multipath多路径冗余全解析
    Oracle中alter system命令参数之scope
    scipy安装失败
    font
    查看端口占用
    oracle参数优化
    组播
    sql给整数补零
  • 原文地址:https://www.cnblogs.com/zcm123/p/3232419.html
Copyright © 2011-2022 走看看