zoukankan      html  css  js  c++  java
  • 继承映射

    Mapping Inheritance

    AutoMapper 1.1 had a method called .Include<> when creating your maps which allowed AutoMapper to automatically select the most derived mapping for a class.

    Take:

        public class Order { }
        public class OnlineOrder : Order { }
        public class MailOrder : Order { }
    
        public class OrderDto { }
        public class OnlineOrderDto : OrderDto { }
        public class MailOrderDto : OrderDto { }
    
        Mapper.Initialize(cfg => {
        cfg.CreateMap<Order, OrderDto>()
              .Include<OnlineOrder, OnlineOrderDto>()
              .Include<MailOrder, MailOrderDto>();
        cfg.CreateMap<OnlineOrder, OnlineOrderDto>();
        cfg.CreateMap<MailOrder, MailOrderDto>();
        });
    
        // Perform Mapping
        var order = new OnlineOrder();
        var mapped = Mapper.Map(order, order.GetType(), typeof(OrderDto));
        Assert.IsType<OnlineOrderDto>(mapped);
    

    You will notice that because the mapped object is a OnlineOrder, AutoMapper has seen you have a more specific mapping for OnlineOrder than OrderDto, and automatically chosen that.

    Specifying inheritance in derived classes

    Instead of configuring inheritance from the base class, you can specify inheritance from the derived classes:

    Mapper.Initialize(cfg => {
    cfg.CreateMap<Order, OrderDto>()
        .ForMember(o => o.Id, m => m.MapFrom(s => s.OrderId));
    cfg.CreateMap<OnlineOrder, OnlineOrderDto>()
        .IncludeBase<Order, OrderDto>();
    cfg.CreateMap<MailOrder, MailOrderDto>()
        .IncludeBase<Order, OrderDto>();
    });
    

    Inheritance Mapping Priorities

    This introduces additional complexity because there are multiple ways a property can be mapped. The priority of these sources are as follows

    • Explicit Mapping (using .MapFrom())
    • Inherited Explicit Mapping
    • Ignore Property Mapping
    • Convention Mapping (Properties that are matched via convention)

    To demonstrate this, lets modify our classes shown above

        //Domain Objects
        public class Order { }
        public class OnlineOrder : Order 
        { 
            public string Referrer { get; set; }
        }
        public class MailOrder : Order { }
    
        //Dtos
        public class OrderDto
        {
            public string Referrer { get; set; }
        }
    
        //Mappings
        Mapper.Initialize(cfg => {
        cfg.CreateMap<Order, OrderDto>()
              .Include<OnlineOrder, OrderDto>()
              .Include<MailOrder, OrderDto>()
              .ForMember(o=>o.Referrer, m=>m.Ignore());
        cfg.CreateMap<OnlineOrder, OrderDto>();
        cfg.CreateMap<MailOrder, OrderDto>();
        });
    
        // Perform Mapping
        var order = new OnlineOrder { Referrer = "google" };
        var mapped = Mapper.Map(order, order.GetType(), typeof(OrderDto));
        Assert.Equals("google", mapped.Referrer);
    

    Notice that in our mapping configuration, we have ignored Referrer (because it doesn't exist in the order base class), but convention has a higher priority than Ignored properties in the base class mappings, so the property still gets mapped.

    Overall this feature should make using AutoMapper with classes that leverage inheritance feel more natural.

  • 相关阅读:
    关于链表
    Linux操作系统(3):crond 任务调度
    高速信号的确定
    python 多线程 QTimer定时自动重复执行某个函数,QSS应用
    使用qtawesome这个第三方库来实现按钮中的Font Awesome字体图标的显示,叠层显示,多窗口显示,窗口禁止缩放,最大化,tap widget使用,按键颜色,建立相关文件路径,点击主窗口退出程序
    IP,子网掩码,网关,DNS的关系解析
    ZLAN6042使用源码(modbus-tcp)
    模块cv2的用法
    python(如何将数据写入本地txt文本文件)
    你猜我猜的经验-电源
  • 原文地址:https://www.cnblogs.com/Leman/p/5774354.html
Copyright © 2011-2022 走看看