使用 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. QT学习:10 IO类

    --- title: framework-cpp-qt-10-IO类 EntryName: framework-cpp-qt-10-QIODevice date: 2020-04-17 10:24:0 ...

  2. HTTP协议 学习:1-报文分析

    HTTP协议 学习:1-报文分析 背景 上一讲我们介绍了HTTP协议的一些 概念 ,对HTTP协议有了一个基础的认识. 正如之前学习MQTT协议一样,我们需要对HTTP的报文进行分析. HTTP 报文 ...

  3. debian11安装备忘

    1. 网卡驱动 参考网址:如何安装Debian RTL8821CE驱动? 2. 分辨率 貌似还是有点问题,还要进一步研究一下 参考网址:虚拟机中debian11修改控制台(console)分辨率|li ...

  4. 解决方案 | cad选择集找出包含特定字符串的多行文本

    代码如下: 1 # 选择文本中出现特定单词的多行文字 2 # 下面的代码将选择条件定义为文本字符串中出现"The"的任意选项.此示例还演示了选择方法的用法:MtextSelectB ...

  5. log4cpp的安装及使用

    目录 前言 安装 使用 示例代码 配置文件 编译链接 输出 前言 本文的操作均在ubuntu20.04下进行 安装 本文仅介绍从源码编译安装log4cpp的过程. ①在开始编译前,首先要确保系统中安装 ...

  6. Python 利用argparse模块实现脚本命令行参数解析

    利用argparse模块实现脚本命令行参数解析 By:授客 QQ:1033553122 #代码实践1 study.py内容如下 #!/usr/bin/env python # -*- coding:u ...

  7. php执行出现权限问题

  8. pytest-req插件:更简单的做接口测试

    pytest-req插件:更简单的做接口测试 背景 我们经常会用到 pytest 和 requests 进行接口自动化测试. pytest 提供了非常方便的插件开发能力,在pytest中使用reque ...

  9. adb shell 批处理文件

    adb shell 批处理文件 手机截屏,并把图片传到电脑

  10. Jmeter函数助手27-urlencode

    urlencode函数用于将字符串进行application/x-www-form-urlencoded编码格式化. String to encode in URL encoded chars:填入字 ...