AutoMapper是一种流行的对象到对象映射库,可用于映射属于不同类型的对象。例如,您可能需要将应用程序中的DTO(数据传输对象)映射到模型对象。

AutoMapper省去了手动映射此类不兼容类型的一个或多个属性的繁琐工作。

一、在Visual Studio中创建一个项目

笔者采用控制台应用程序。

然后安装AutoMapper。可以在NuGet软件包管理器窗口安装AutoMapper

二、使用AutoMapper创建映射

诸如AutoMapper之类的对象到对象映射器将一种类型的输入对象转换为另一种类型的输出对象。考虑以下两个类。

  //实体
public class AuthorModel
{
public int ID { get; set; }
public string AuthorName { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
} //映射实体DTO
public class AuthorDTO
{
public int ID { get; set; }
public string AuthorName { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}

配置类型AuthorModel和AuthorDTO之间创建映射关系。

      var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AuthorModel, AuthorDTO>();
});

然后,执行类型之间的映射就像下面的代码所示一样简单。

      IMapper iMapper = config.CreateMapper();
var model = new AuthorModel();
var MapAuthor = iMapper.Map<AuthorModel, AuthorDTO>(model);//映射实体

三、使用AutoMapper完整示例

现在让我们处理一些数据。请参考以下代码,该代码将一些数据存储到源对象中,然后在完成映射后在目标对象中显示属性值。

    static void Main(string[] args)
{
//注册配置
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AuthorModel, AuthorDTO>();
}); //创建映射
IMapper iMapper = config.CreateMapper(); var model = new AuthorModel();
model.ID = 1;
model.AuthorName = "LaoPaoEr";
model.Sex = "男";
model.Age = 23;
model.Phone = "18276250000";
model.Address = "中国上海市浦东新区XXXXX"; var MapAuthor = iMapper.Map<AuthorModel, AuthorDTO>(model);//映射实体 Console.WriteLine(MapAuthor);
Console.WriteLine($"Author Name: {model.AuthorName}");
Console.ReadLine();
}

当您执行上述代码时,将显示存储在目标对象中的作者姓名,使用AutoMapper成功映射了对象!

更多AutoMapper用法请转到http://automapper.org/

C#中使用AutoMapper的更多相关文章

  1. ASP.NET.Core中使用AutoMapper

      首先需要在NuGet中引用AutoMapper的类库 install-package AutoMapper install-package AutoMapper.Extensions.Micros ...

  2. .NET Core中使用AutoMapper

    何为AutoMapper AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 安装AutoMapper 这里我们在NuGet中下载安装Au ...

  3. .NET CORE 中使用AutoMapper进行对象映射

    简介 AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMappe ...

  4. 在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射

    一.前言 在实际项目开发过程中,我们使用到的各种 ORM 组件都可以很便捷的将我们获取到的数据绑定到对应的 List<T> 集合中,因为我们最终想要在页面上展示的数据与数据库实体类之间可能 ...

  5. 在 ASP.NET Core 中使用 AutoMapper 使 Entity 和 Resource 之间进行映射

    目录 从 NuGet 安装 AutoMapper 添加 Entity类 和 Resource类 添加一个 Profile文件,配置映射关系 在Startup中对AutoMapper进行注册 在项目中使 ...

  6. Dotnet Core中使用AutoMapper

    官网:http://automapper.org/ 文档:https://automapper.readthedocs.io/en/latest/index.html GitHub:https://g ...

  7. ASP.NET CORE 中使用AutoMapper进行对象映射

    ASP.NET CORE 中使用AutoMapper进行对象映射 1.什么是AutoMapper? AutoMapper是基于对象到对象约定的映射工具,常用于(但并不仅限制于)把复杂的对象模型转为DT ...

  8. 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)

    这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...

  9. .Net Core 中使用AutoMapper

    1.新建一个类 using AutoMapper; using YourModels; using YourViewModels; namespace YourNamespace { public c ...

  10. ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射

    本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...

随机推荐

  1. CJ88 DUMP The ASSERT condition was violated

    一.CJ88运行某个项目时DUMP,其他项目正常 The ASSERT condition was violated. 源代码位置为交易货币为空导致DUMP 经过长时间的源码调试,也只定位在查询语句这 ...

  2. M-SOLUTIONS Programming Contest 2020 游记 (AB水题,CD模拟,E题DFS)

    A - Kyu in AtCoder 水题 B - Magic 2 题意很好理解,但写的时候注意一下边界 void solve() { int a, b, c, k; cin >> a & ...

  3. 如何设置IDEA代码风格为Google风格,使用Google风格format

    1.在Github仓库寻找:google style 为了节省大家时间直接放链接了:Here 2.进到项目 找到名为intellij-java-google-style.xml 文件 Ctrl + F ...

  4. 关于el-upload上传图片的一些坑clearFiles()的使用

    https://blog.csdn.net/weixin_46421824/article/details/109195624?spm=1001.2101.3001.6661.1&utm_me ...

  5. 《IT项目管理知识》出书了,特点:全是干货

    大纲目录,及内容如下.本书属于付费内容

  6. 基于React开发的chatgpt网页版(仿chatgpt)

    在浏览github的时候发现了一个好玩的项目本项目,是github大神Yidadaa开发的chatgpt网页版,该开源项目是跨平台的,Web / PWA / Linux / Win / MacOS都可 ...

  7. 07-逻辑仿真工具VCS-Post processing with VCD+ files

    逻辑仿真工具-VCS 编译完成不会产生波形,仿真完成之后,生成波形文件,通过dve产看波形 vcd是波形文件的格式,但是所占的内存比较大,后面出现了vpd(VCD+)波形文件 将一些系统函数嵌入到源代 ...

  8. NodeJS安装指南(Mac)

    nvm,node,npm之间的区别 nvm:nodejs 版本管理工具. 也就是说:一个 nvm 可以管理很多 node 版本和 npm 版本. nodejs:在项目开发时的所需要的代码库 npm:n ...

  9. Go-基本类型-int-float-bool-byte-rune

  10. 快速迁移Grafana/Prometheus等的方式方法

    快速迁移Grafana/Prometheus等的方式方法 背景 有一套鲲鹏环境下面的Grafana监控平台. 同事想能够将平台内的时序数据库等迁移到一个别的机器上进行使用. 自从自己开始搞国产化之后, ...