• AutoMapper的使用


    常规的使用,一般先初始化配置,一个应用只需初始化一次

    //全局文件中写入此项 
    Mapper.Initialize(cfg => cfg.CreateMap<Order, OrderDto>()); //or var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());
    var mapper = config.CreateMapper();
    // or
    var mapper = new Mapper(config);
    OrderDto dto = mapper.Map<OrderDto>(order);
    // or
    OrderDto dto = Mapper.Map<OrderDto>(order);

    使用AutoMapper进行Entity与DTO之间的转换,不同字段之间的映射转换

     AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<StreetEvent, StreetEventDTO>()
                .ForMember(dest => dest.EvtSrc, opt => opt.MapFrom(src => src.evt_src))
                .ForMember(dest => dest.MapId, opt => opt.MapFrom(src => src.map_id))
                );
    
                var targetList = AutoMapper.Mapper.Map<List<StreetEventDTO>>(list);

    实体之间的自定义类型转换,参考:

    https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

    复制代码
       public class Source
        {
            public string Value1 { get; set; }
            public string Value2 { get; set; }
            public string Value3 { get; set; }
        }
    
      public class Destination
        {
            public int Value1 { get; set; }
            public DateTime Value2 { get; set; }
            public Type Value3 { get; set; }
        }
    
    [Test]
        public void Example()
        {
            Mapper.Initialize(cfg => {
              cfg.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
              cfg.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
              cfg.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();
              cfg.CreateMap<Source, Destination>();
            });
            Mapper.AssertConfigurationIsValid();
        
            var source = new Source
            {
                Value1 = "5",
                Value2 = "01/01/2000",
                Value3 = "AutoMapperSamples.GlobalTypeConverters.GlobalTypeConverters+Destination"
            };
            
            Destination result = Mapper.Map<Source, Destination>(source);
            result.Value3.ShouldEqual(typeof (Destination));
        }
        
        public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
        {
            public DateTime Convert(string source, DateTime destination, ResolutionContext context)
            {
                return System.Convert.ToDateTime(source);
            }
        }
        
        public class TypeTypeConverter : ITypeConverter<string, Type>
        {
            public Type Convert(string source, Type destination, ResolutionContext context)
            {
                  return context.SourceType;
            }
        }
    复制代码

    自定义实体值的转换,适用于目标与源实体直接的值类型计算,参考

    https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers

    复制代码
    public class Source
        {
            public int Value1 { get; set; }
            public int Value2 { get; set; }
        }
        
        public class Destination
        {
            public int Total { get; set; }
        }
    
      public class CustomResolver : IValueResolver<Source, Destination, int>
        {
            public int Resolve(Source source, Destination destination, int member, ResolutionContext context)
            {
                return source.Value1 + source.Value2;
            }
        }
    
    Mapper.Initialize(cfg => 
           cfg.CreateMap<Source, Destination>()
             .ForMember(dest => dest.Total, opt => opt.ResolveUsing<CustomResolver>());
        Mapper.AssertConfigurationIsValid();
        
        var source = new Source
            {
                Value1 = 5,
                Value2 = 7
            };
        
        var result = Mapper.Map<Source, Destination>(source);
        
        result.Total.ShouldEqual(12);
    复制代码
    复制代码
    public class MultBy2Resolver : IValueResolver<object, object, int> {
        public int Resolve(object source, object dest, int destMember, ResolutionContext context) {
            return destMember * 2;
        }
    }
    
      Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>()
            .ForMember(dest => dest.Total, 
                opt => opt.ResolveUsing(new CustomResolver())
            );
    
    
    
    Mapper.Initialize(cfg => {
    cfg.CreateMap<Source, Destination>()
        .ForMember(dest => dest.Total,
            opt => opt.ResolveUsing<CustomResolver, decimal>(src => src.SubTotal));
    cfg.CreateMap<OtherSource, OtherDest>()
        .ForMember(dest => dest.OtherTotal,
            opt => opt.ResolveUsing<CustomResolver, decimal>(src => src.OtherSubTotal));
    });
    
    public class CustomResolver : IMemberValueResolver<object, object, decimal, decimal> {
        public decimal Resolve(object source, object destination, decimal sourceMember, decimal destinationMember, ResolutionContext context) {
    // logic here
        }
    }
  • 相关阅读:
    Linux下的游戏及模拟器
    [Java]学习Java(3)数组、字符串
    [Javascript]Event属性
    下载文件名乱码
    比那 施氏食狮史 还要强的文章
    [Javascript]脚本学习(1)
    [Java]学习Java(2)基本类型与对象
    [Java]学习Java(1)运算符&语句&类
    LumaQQ开发文档
    Extensions for FireFox
  • 原文地址:https://www.cnblogs.com/tsql/p/8743062.html
Copyright © 2020-2023  润新知