AutoMapper的使用方法

AutoMapper支持对象—对象映射的开源工具,主要负责将一个数据对象的数据映射到另外一个数据对象上,下面来介绍一下其基本用法,


首先,先创建三个Model

    public class Source
    {
        public int ID { get; set; }

        public string Name { get; set; } //目标中没有Name

        public string MemberNumber { get; set; }

        public int Height { get; set; }

        public int Value { get; set; }

        public string FromGroup { get; set; } //目标中没有FromGroup,但有Group成员

        public string MainAirlinaName { get; set; } //目标中没有MainAirlinaName,但有MainAirlineName

        public DateTime Date { get; set; }//目标中没有Date

        public Customer Customer { get; set; } //目标中没有Customer对象,但有个同名的string类型变量成员

        public List<Customer> CustomerList { get; set; }

        public decimal GetTotalWithoutTax()
        {
            return 1088.10m;
        }
    }

    public class Destination
    {
        public int ID { get; set; }

        public int MemberNumber { get; set; }  //源中成员MemberNumber的类型是string

        public uint Height { get; set; } //源中成员Height的类型是int

        public int Value { get; set; }

        public string Group { get; set; } //源中没有Group,但有FromGroup字段

        public string MainAirlineName { get; set; } //源中没有MainAirlineName,但有MainAirlinaName

        public DateTime EventDate { get; set; } //源中没有EventDate

        public int EventHour { get; set; }//源中没有EventHour

        public int EventMinute { get; set; } //源中没有EventMinute

        public decimal TotalWithTax { get; set; }//源中没有TotalWithTax

        public string CustomerName { get; set; }//源中没有CustomerName

        public string TotalWithoutTax { get; set; }//源中没有TotalWithoutTax

        public string Customer { get; set; } //源中成员Customer的类型是类Customer

        public List<Customer> CustomerList { get; set; }
    }

    public class Customer
    {
        public string Name { get; set; }
    }    


1:最基本的用法、扁平化映射:

 //写法一:          
            MapperConfiguration config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Destination>());
            IMapper mapper = config.CreateMapper();
            Destination destination1 = mapper.Map<Destination>(source);

            //写法二(推荐此写法):
            Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());
            Destination destination2 = Mapper.Map<Destination>(source);


            //写法三:利用Profile
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile<SourceProfile>();
            });
            Destination destination3 = Mapper.Map<Destination>(source);

2:一些比较常用的转换:

其用法一般是Mapper.Initialize(cfg=>{ cfg.CreateMap<Source, Destination>() .ForMember(dest => dest.CustomerName, opt => XXXX) });

下面附上一些常用的例子

            Mapper.Initialize(cfg =>
            {
                cfg.RecognizePrefixes("From");  //表示源对象中的From***成员变量值能够映射到目标对象中***成员变量,如:源对象中的FromGroup成员将映射到目标对象中的Group成员
                cfg.ReplaceMemberName("Airlina", "Airline"); //表示源对象中的含有Airlina的成员变量映射到目标对象中含有Airline的成员变量      
                cfg.CreateMap<Source, Destination>()
                    .ForMember(dest => dest.MemberNumber, opt =>   //空值替换
                    {
                        opt.NullSubstitute("654321");
                    })
                  .ForMember(dest => dest.CustomerName, opt => opt.Ignore()) //忽略CustomerName字段不被映射
                  .ForMember(dest => dest.Height, opt => opt.Condition(src => (src.Height > 150))) //条件映射
                  .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.Date.Date)) //指定字段映射
                  .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.Date.Hour))
                  .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.Date.Minute))
                  .ForMember(dest => dest.Customer, opt => opt.MapFrom(src => src.Customer.Name));                  
            });            
            Destination destination = Mapper.Map<Source, Destination>(source, opt =>
            {
                opt.BeforeMap((src, dest) => src.Height = src.Height + 10); //前映射
                opt.AfterMap((src, dest) => dest.CustomerName = "季羡林");  //后映射
            });






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值