简介

AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

官网:http://automapper.org/

文档:https://automapper.readthedocs.io/en/latest/index.html

GitHub:https://github.com/AutoMapper/AutoMapper/blob/master/docs/index.rst

平台支持:

  • .NET 4.6.1+
  • .NET Standard 2.0+ https://docs.microsoft.com/en-us/dotnet/standard/net-standard

使用

Nuget安装

AutoMapper
AutoMapper.Extensions.Microsoft.DependencyInjection //依赖注入AutoMapper,需要下载该包。

在Startup中添加AutoMapper

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//添加对AutoMapper的支持
services.AddAutoMapper();
}

创建AutoMapper映射规则

public class AutoMapperConfigs:Profile
{
//添加你的实体映射关系.
public AutoMapperConfigs()
{
CreateMap<DBPoundSheet, PoundSheetViewModel>();
CreateMap<PoundSheetViewModel, DBPoundSheet>();
}
}

在构造函数中注入你的IMapper

IMapper _mapper;

public PoundListController(IMapper mapper)
{
_mapper = mapper;
}

单个对象转换

//typeof(model)="PoundSheetViewModel"
DBPoundSheet dBPoundSheet = _mapper.Map<DBPoundSheet>(model);

集合对象转换

.NET CORE 中使用AutoMapper进行对象映射的更多相关文章

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

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

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

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

  3. Dotnet Core中使用AutoMapper

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

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

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

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

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

  6. ASP.NET.Core中使用AutoMapper

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

  7. .NET Core中使用AutoMapper

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

  8. .Net Core 中使用AutoMapper

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

  9. AutoMapper,对象映射的简单使用

    using AutoMapper; using AutoMapper.Configuration; using System; using System.Collections.Generic; us ...

随机推荐

  1. SpringBoot Web Https 配置

    不管是游戏服务器开发,还是其它服务开发,越来越多的平台都要求服务端必须支持https的访问.以增加安全性.比如目前火热的小程序,要求服务端必须支持https,苹果商店也有说http请求要修改为http ...

  2. postman接口测试举例情况

    http请求:http请求分为请求头和请求体,get请求只有请求头没有请求体. 1.get请求 是可以直接在浏览器访问,不需要借助任何工具.好看一些,可以打开postman测试接口 http://xx ...

  3. 每日分享!JavaScript的鼠标事件(11个事件)

    鼠标的11个事件 具体的事件解释如下: click:按下鼠标(通常是按下主按钮)时触发. dblclick:在同一个元素上双击鼠标时触发. mousedown:按下鼠标键时触发. mouseup:释放 ...

  4. nodeJs的nodemailer发邮件报错hostname/IP doesn't match certificate's altnames怎么解决?

    今天在开发过程中碰到一个问题,即使用node发送邮件时报错hostname/IP doesn't match certificate's altnames,在网上查了解决办法有两个, 加rejectU ...

  5. .NET Core IdentityServer4实战 第三章-使用EntityFramework Core进行持久化配置

    内容:本文带大家使用IdentityServer4进行使用使用EntityFramework Core进行配置和操作数据 作者:zara(张子浩) 欢迎分享,但需在文章鲜明处留下原文地址. 前两章内容 ...

  6. 很详细的Django入门详解

    Django 是用Python开发的一个免费开源的Web框架,可以用于快速搭建高性能,优雅的网站!采用了MVC的框架模式,即模型M,视图V和控制器C,也可以称为MVT模式,模型M,视图V,模板T.在学 ...

  7. 基于promise对小程序http请求方法封装

    原因是我不想每次请求都复制粘贴那么长的请求地址,所以我把前边那一坨请求地址作为基础地址,只传后台给的路由就ok,而且,并不是每次请求都要显示正在加载,这对小程序体验很差,所以,我加了个形参,用来判断是 ...

  8. SQL优化 MySQL版 - 避免索引失效原则(二)

    避免索引失效原则(二) 注:继上一篇文章继续讲解: 避免索引失效原则(一)https://www.cnblogs.com/StanleyBlogs/p/10482048.html#4195062 作者 ...

  9. Asp.Net Core 轻松学-使用MariaDB/MySql/PostgreSQL和支持多个上下文对象

    前言 在上一篇文章中(Asp.Net Core 轻松学-10分钟使用EFCore连接MSSQL数据库)[https://www.cnblogs.com/viter/p/10243577.html],介 ...

  10. ArrayList 和 LinkedList 源码分析

    List 表示的就是线性表,是具有相同特性的数据元素的有限序列.它主要有两种存储结构,顺序存储和链式存储,分别对应着 ArrayList 和 LinkedList 的实现,接下来以 jdk7 代码为例 ...