即兴添加一下

AutoMaper使用

使用 AutoMapper 进行赋值

一. 什么是 AutoMapper

  AutoMapper是对象到对象的映射工具。在完成映射规则之后,AutoMapper可以将源对象转换为目标对象。

二. AutoMapper 安装使用

1.安装 AutoMapper

Vs2015及以上版本右键解决方案--->管理Nuget方案中输入AutoMaper选择对应.NETFRAMEWORK支持版本进行安装。

2.引入 AutoMapperHelper AutoMapper 扩展帮助类

 /// <summary>
/// AutoMapper扩展帮助类
/// </summary>
public static class AutoMapperHelper
{
    /// <summary>
    ///  类型映射
    /// </summary>
    public static T MapTo<T>(this object obj)
    {
        if (obj == null) return default(T);
        Mapper.CreateMap(obj.GetType(), typeof(T));
        return Mapper.Map<T>(obj);
    }
    /// <summary>
    /// 集合列表类型映射
    /// </summary>
    public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
    {
        foreach (var first in source)
        {
            var type = first.GetType();
            Mapper.CreateMap(type, typeof(TDestination));
            break;
        }
        return Mapper.Map<List<TDestination>>(source);
    }
    /// <summary>
    /// 集合列表类型映射
    /// </summary>
    public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
    {
        //IEnumerable<T> 类型需要创建元素的映射
        Mapper.CreateMap<TSource, TDestination>();
        return Mapper.Map<List<TDestination>>(source);
    }
    /// <summary>
    /// 类型映射
    /// </summary>
    public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
        where TSource : class
        where TDestination : class
    {
        if (source == null) return destination;
        Mapper.CreateMap<TSource, TDestination>();
        return Mapper.Map(source, destination);
    }
    /// <summary>
    /// DataReader映射
    /// </summary>
    public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader)
    {
        Mapper.Reset();
        Mapper.CreateMap<IDataReader, IEnumerable<T>>();
        return Mapper.Map<IDataReader, IEnumerable<T>>(reader);
    }
} 

两个对象:

        //这里是原对象
         public class datas
        {
        public string appKey { get; set; }
        public string kpzdbs { get; set; }
        public string jqbh { get; set; }
        public string fpqqlsh { get; set; }
        public string kplx { get; set; }
        public string fplxdm { get; set; }
        public string zsfs { get; set; }
        public string tspzbs { get; set; }
        public string xfsh { get; set; }
        public string xfmc { get; set; }
        public string xfdzdh { get; set; }
        public string xfyhzh { get; set; }
        public string gfmc { get; set; }
        public string gfsh { get; set; }
        public string gfdzdh { get; set; }
        public string gfyhzh { get; set; }
        public string gfsjh { get; set; }
        public string gfdzyx { get; set; }
        public string qdbz { get; set; }
        public string bz { get; set; }
        public string kpr { get; set; }
        public string skr { get; set; }
        public string fhr { get; set; }
        public string jshj { get; set; }
        public string hjje { get; set; }
        public string hjse { get; set; }
        public string sblx { get; set; }
    }

   //这里是被赋值的对象
    public class datasdto : datas
    {
        public List<fyxm> fyxm { get; set; }
        public string sign { get; set; }
    }

赋值使用:

    //原赋值对象
    datas datasmodel = new datas();
    //被赋值对象
    datasdto dtoDatasdtomodel = new datasdto();
    //赋值
    dtoDatasdtomodel = datasmodel.MapTo(dtoDatasdtomodel);

结语

AutoMapper 是一款强大的对象映射器,本文只是项目中的一个简单实例,用于记录学习,有错误的地方希望大佬能给予指出,会及时修改。

官网地址:http://automapper.org/

官方文档:https://docs.automapper.org/en/latest/

posted @ 2020-12-29 09:54  我本梁人  阅读(389)  评论(0编辑  收藏  举报