使用 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/

AutoMaper使用的更多相关文章

  1. EF AutoMaper

    Mapper.CreateMap<Source,Dest>(); 该方法已弃用,使用下面这个 Mapper.Initialize(x=>x.CreateMap<Source,D ...

  2. C# AutoMaper使用自定义主键

    有时候实际业务中主键不一定叫Id,比如示例数据库Northwind中的RegionID,TerritoryID等,本示例用Abp框架并以Northwind数据库Region表为数据依据 一.在Core ...

  3. C# AutoMaper简单使用

    AutoMapper它是一种对象与对象之间的映射器,让AutoMapper有意思的就是在于它提供了一些将类型A映射到类型B这种无聊的实例,只要B遵循AutoMapper已经建立的惯例,那么大多数情况下 ...

  4. AutoMapper搬运工之初探AutoMapper

    写在前面 知道AutoMapper很久了,但是一直没有用,最近刚好有个场景需要就用了,果然是利器.看了git上的wiki,发现内容其实wiki上写的很全面了,深入的暂时还没挖掘到.不过和群里的朋友交流 ...

  5. 使用C#设计Fluent Interface

    我们经常使用的一些框架例如:EF,Automaper,NHibernate等都提供了非常优秀的Fluent Interface, 这样的API充分利用了VS的智能提示,而且写出来的代码非常整洁.我们如 ...

  6. Core 1.0中的管道-中间件模式

    ASP.NET Core 1.0中的管道-中间件模式 SP.NET Core 1.0借鉴了Katana项目的管道设计(Pipeline).日志记录.用户认证.MVC等模块都以中间件(Middlewar ...

  7. C#中设计Fluent API

    C#中设计Fluent API 我们经常使用的一些框架例如:EF,Automaper,NHibernate等都提供了非常优秀的Fluent API, 这样的API充分利用了VS的智能提示,而且写出来的 ...

  8. AutoMapper使用说明

    1.引用命名空间 using AutoMapper;using AutoMapper.Mappers; 2.实体类和dto public class Order { public int orderi ...

  9. 使用NPOI导出Excel引发异常(IsReadOnly = “book.IsReadOnly”引发了类型“System.NotImplementedException”的异常)

    前言: 本人调式npoi导入.导出试用成功后,引入到项目中,导入完美运行,但是导出怎么样都看不到现在的页面,而且浏览器和后台都没有报任务错误,让人好事纳闷,后来去调式,发现在除了一个IsReadOnl ...

  10. .Net 事件总线之Autofac解耦

    事件总线是通过一个中间服务,剥离了常规事件的发布与订阅(消费)强依赖关系的一种技术实现.事件总线的基础知识可参考圣杰的博客[事件总线知多少] 本片博客不再详细概述事件总线基础知识,核心点放置使用Aut ...

随机推荐

  1. 一文带你深入理解SpringMVC的执行原理

    今天大致来看一下Spring MVC的执行流程是什么样的 执行流程:也就是一个请求是怎么到我们Controller的,返回值是怎么给客户端的 本文分析的问题: 文件上传的请求是怎么处理的 跨域是怎么处 ...

  2. 缺氧debu模式

    OxygenNotIncluded_Data(E:\SteamLibrary\steamapps\common\OxygenNotIncluded\OxygenNotIncluded_Data) 文件 ...

  3. [FLET] 01 可以拖动的方块

    from typing import List import flet from flet import ( Container, Draggable, DragTarget, Page, Row, ...

  4. Top cluster 树分块

    写点基础的东西.随便写的,勿喷. top cluster 一个 cluster 是一个联通子图,且至多有两个点与其他部分连接 这两个点被称为 boundary node 其余点被称为 internal ...

  5. 树莓派4B-MAX9814麦克风模块

    树莓派4B-MAX9814麦克风模块 硬件需求 树莓派 MAX9814模块 杜邦线 MAX9814模块 电子特性 实验电路板 实验电路局部 典型工作特性 引角接线 代码展示 import RPi.GP ...

  6. css 手稿

    CSS 网页的布局和外观的显示样式 没有一张图解决不了的事:https://www.processon.com/mindmap/5e368a8be4b0d27af184e118 选择器 * 通配符选择 ...

  7. 异构数据源数据同步 → 从源码分析 DataX 敏感信息的加解密

    开心一刻 出门扔垃圾,看到一大爷摔地上了 过去问大爷:我账户余额 0.8,能扶你起来不 大爷往旁边挪了挪 跟我说到:孩子,快,你也躺下,这个来钱快! 我没理大爷,径直去扔了垃圾 然后飞速的躺在了大爷旁 ...

  8. 国产开源存储之光:Curve 通过信创认证

    网易数帆喜讯再传,Curve 近日通过信息技术应用创新(信创)认证! Curve 是一款高性能.易运维.云原生的分布式存储系统,由网易数帆存储团队发起开源,现为 CNCF 沙箱项目.国家工业信息安全发 ...

  9. 全网最适合入门的面向对象编程教程:18 类和对象的 Python 实现-多重继承与 PyQtGraph 串口数据绘制曲线图

    全网最适合入门的面向对象编程教程:18 类和对象的 Python 实现-多重继承与 PyQtGraph 串口数据绘制曲线图 摘要: 本文主要介绍了 Python 中创建自定义类时如何使用多重继承.菱形 ...

  10. oeasy 教您玩转linux 之 010209 装酷利器 hollywood

    我们来回顾一下 上一部分我们都讲了什么? 屏幕故障风格的软件包bb 可以设置音频 这次装一个酷 下个hollywood软件包 apt show hollywood apt search hollywo ...