zoukankan      html  css  js  c++  java
  • 关于AutoMApping 实体映射

    安装AutoMapping包

    把订单实体映射成订单DTO实体

     .ReverseMap()加上这个方法后 下面自定义 映射规则  第一个就是来源对象 第二个就是目标对象  

    https://www.cnblogs.com/fred-bao/p/5700776.html

     ABP中 映射三种写法

    1.第一种写法 直接在DTO上面写AutoMapper

    [AutoMapTo]把当前类映射为Person

    [AutoMapForm]把Person映射为当前类

    2.第二种写法  创建映射类

    在Module中注册

    3.映射类继承Profile

    相同类型对象进行映射     

    使用MapTo() 赋值不会报错   但是映射不成功    只能用MapTo<>()进行类型转换;   相同类型使用MapTo<>()进行映射  映射完成后  两个对象为同一个对象实例

                var aa = new Customer() { Name="张三",CustomerNumber="0001",Phone="150"};
                var cc = new Customer() { Name="王五"};
                aa.MapTo(cc);
                var dd = aa.MapTo<Customer>();
    

      

                CreateMap<OrderInput, SalesOrder>()
                    .ForMember(dest => dest.Lines, opt => opt.Ignore());

    把OrderInput对象映射为SalesOrder 对象的时候    SalesOrder 中的Lines属性不会被映射    Lines必须要是一个对象    如果只是一个属性就会报错

    ******************************

    统一映射

    创建映射类  有N个

        public class AccountProfile : Profile, IProfile
        {
            public AccountProfile()
            {
                CreateMap<Account, AccountDto>();
            }
    
        }
    

      找到Iprofile接口所在的程序集 

      找到这个程序集中所有继承IProfile的类  

      统一注入映射规则

                var allTypes = typeof(IProfile)
                    .Assembly
                    .GetTypes()
                    .Where(type => type.IsClass)
                    .Where(type => typeof(IProfile).GetTypeInfo().IsAssignableFrom(type))
                    .ToList();
    
                Mapper.Initialize(y =>
                {
                    foreach (var type in allTypes)
                    {
                        y.AddProfiles(type);
                    }
                });
    

      

  • 相关阅读:
    C# 高效字符串连接 StringBuilder介绍
    C#图解教程
    C#中string类型是值类型还是引用类型?
    UML类图10分钟快速入门
    C#设计模式--单例模式
    计算机是如何启动的?
    2018年计划
    转:SQL进阶之变量、事务、存储过程与触发器
    2020/02/06,武汉
    2020/02/06,渐渐,from eason for you for her
  • 原文地址:https://www.cnblogs.com/jiangchengbiao/p/9722431.html
Copyright © 2011-2022 走看看