zoukankan      html  css  js  c++  java
  • .net Core中使用AutoMapper

    第一种:只引用AutoMapper包写一个AutoMapperExtension的扩展方法

    废话不多说扩展方法代码如下

      1 /// <summary>
      2     /// AutoMapper扩展
      3     /// </summary>
      4     public static class AutoMapperExtension
      5     {
      6         /// <summary>
      7         /// 类型映射
      8         /// </summary>
      9         /// <typeparam name="TDestination">映射后的对象</typeparam>
     10         /// <param name="obj">要映射的对象</param>
     11         /// <returns></returns>
     12         public static TDestination MapTo<TDestination>(this object obj) where TDestination : class
     13         {
     14             if (obj == null) return default(TDestination);
     15             var config = new MapperConfiguration(cfg => cfg.CreateMap<TDestination, object>());
     16             var mapper = config.CreateMapper();
     17             return mapper.Map<TDestination>(obj);
     18         }
     19 
     20         /// <summary>
     21         /// 集合列表类型映射
     22         /// </summary>
     23         /// <typeparam name="TDestination">目标对象类型</typeparam>
     24         /// <param name="source">数据源</param>
     25         /// <returns></returns>
     26         public static List<TDestination> MapTo<TDestination>(this IEnumerable<TDestination> source) where TDestination : class
     27         {
     28             if (source == null) return default(List<TDestination>);
     29 
     30             var config = new MapperConfiguration(cfg => cfg.CreateMap(source.GetType(), typeof(TDestination)));
     31             var mapper = config.CreateMapper();
     32             return mapper.Map<List<TDestination>>(source);
     33         }
     34 
     35         /// <summary>
     36         /// 集合列表类型映射
     37         /// </summary>
     38         /// <typeparam name="TSource">数据源类型</typeparam>
     39         /// <typeparam name="TDestination">目标对象类型</typeparam>
     40         /// <param name="source">数据源</param>
     41         /// <returns></returns>
     42         public static List<TDestination> MapTo<TSource, TDestination>(this IEnumerable<TSource> source)
     43             where TDestination : class
     44             where TSource : class
     45         {
     46             if (source == null) return new List<TDestination>();
     47 
     48             var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
     49             var mapper = config.CreateMapper();
     50             return mapper.Map<List<TDestination>>(source);
     51         }
     52 
     53         /// <summary>
     54         /// 集合列表类型映射
     55         /// </summary>
     56         /// <typeparam name="TSource">数据源类型</typeparam>
     57         /// <typeparam name="TDestination">目标对象类型</typeparam>
     58         /// <param name="source">数据源</param>
     59         /// <param name="configure">自定义配置</param>
     60         /// <returns></returns>
     61         public static List<TDestination> MapTo<TSource, TDestination>(this IEnumerable<TSource> source, Action<IMapperConfigurationExpression> configure)
     62             where TDestination : class
     63             where TSource : class
     64         {
     65             if (source == null) return new List<TDestination>();
     66 
     67             var config = new MapperConfiguration(configure);
     68             var mapper = config.CreateMapper();
     69             return mapper.Map<List<TDestination>>(source);
     70         }
     71 
     72         /// <summary>
     73         /// 类型映射
     74         /// </summary>
     75         /// <typeparam name="TSource">数据源类型</typeparam>
     76         /// <typeparam name="TDestination">目标对象类型</typeparam>
     77         /// <param name="source">数据源</param>
     78         /// <param name="destination">目标对象</param>
     79         /// <returns></returns>
     80         public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
     81             where TSource : class
     82             where TDestination : class
     83         {
     84             if (source == null) return destination;
     85 
     86             var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
     87             var mapper = config.CreateMapper();
     88             return mapper.Map<TSource, TDestination>(source, destination);
     89         }
     90 
     91         /// <summary>
     92         /// 类型映射,默认字段名字一一对应
     93         /// </summary>
     94         /// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam>
     95         /// <typeparam name="TSource">要被转化的实体,Entity</typeparam>
     96         /// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>
     97         /// <returns>转化之后的实体</returns>
     98         public static TDestination MapTo<TSource, TDestination>(this TSource source)
     99             where TDestination : class
    100             where TSource : class
    101         {
    102             if (source == null) return default(TDestination);
    103 
    104             var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
    105             var mapper = config.CreateMapper();
    106             return mapper.Map<TDestination>(source);
    107         }
    108 
    109     }
    View Code

    使用AutoMapper推荐这个AutoMapperExtension.MapTo<TSource, TDestination>(this TSource source);方法

     class Program
        {
            static void Main(string[] args)
            {
                Student student = new Student
                {
                    Id=5,
                    Name="张三",
                    Email="146855"
                };
                StudentView model = AutoMapperExtension.MapTo<Student,StudentView>(student);
                bool b = model is null;
                Console.WriteLine(b);
                Console.ReadKey();
            }
        }
    
        public class Student
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public string Email { get; set; }
    
        }
        public class StudentView
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public string Email { get; set; }
            public string ClassName { get; set; }
    
        }
    View Code

    结果如下student转化studentView成功

    第二种:写一个配置类懒得写直接参考下面的文章,麻烦一点

    文章来自https://www.cnblogs.com/chenxi001/archive/2019/11/05/11800943.html

    在我们的项目中慢慢的要把数据库的实体模型和视图模型进行分离,防止被人拿到我们表字段。在学校的时候自己只是有将很多数据库模型,写成一个视图模型返回到前台。

     首先我们把这两个包引入项目中去。

    然后我们创建一个转换配置类,这个类要继承 Profile 将我们需要转换的类写到我们构造函数里面去,这里要注意我们左边的UserEntity是要将这个类型的数据转换成UserModel

    当然这个可以反过来,但是你转换的关系一定要正确,左为要转换的数据类型,右边是转换后的数据类型。(我已经帮你们测试了,关系不对乱转报错。)

    在Startup 写入注入这个配置

     

    下面我们创建两个类

    这个是我们平常中数据库表实体

    这是我们的视图模型

     这个里要注意,在转化的时候只有名字相同的字段才会成功附上对应的值。(大小写可以不管,但是在项目中要做到一致,我上面没有一致是测试一下。)

    然后就是使用了

    我们只要在使用的地方,注入进来了,就可以使用了。

    这个是单个实体的转换

     简写方法,直接写要转成什么类型就可以了,但是转换的配置类法不变。

     当然我们还有集合的转换了

  • 相关阅读:
    Java 集合类
    Java 中的四种引用及垃圾回收策略
    Java 类的加载过程(阿里面试题)
    JMeter进行一次简单的压力测试
    python使用 HTMLTestRunner.py生成测试报告
    python读取配置文件
    python的IDE工具-- Pycharm
    selenium webdriver+python基本操作
    SQL开发测试使用基础
    python学习笔记
  • 原文地址:https://www.cnblogs.com/hudean/p/12305297.html
Copyright © 2011-2022 走看看