AutoMapper对象映射工具:主要是将某一个实体转成另一个实体。

1.引用NuGet包;搜索:AutoMapper

2.创建实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoMapper
{
public static class AutoMapperExtension
{
/// <summary>
/// 单个对象映射
/// </summary>
/// <typeparam name="TSource">源对象</typeparam>
/// <typeparam name="TDestination">目标对象</typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static TDestination MapTo<TSource, TDestination>(TSource source)
{
if (source == null) return default(TDestination);
Mapper.Initialize(x => x.CreateMap(typeof(TSource), typeof(TDestination)));
return Mapper.Map<TDestination>(source);
} /// <summary>
/// 集合列表类型映射
/// </summary>
/// <typeparam name="TSource">源对象</typeparam>
/// <typeparam name="TDestination">目标对象</typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
{
if (source == null) return default(List<TDestination>);
Mapper.Initialize(x => x.CreateMap(typeof(TSource), typeof(TDestination)));
return Mapper.Map<List<TDestination>>(source);
}
}
}

3.作为例子。建立两个实体对象

(老会员实体)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoMapper
{
public class UserModel
{
/// <summary>
/// 会员编号
/// </summary>
public Int32 UserId { get; set; } /// <summary>
/// 会员名称
/// </summary>
public String Name { get; set; }
}
}

新会员实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoMapper
{
/// <summary>
/// 新会员表
/// </summary>
public class UserNewModel
{
/// <summary>
/// 会员编号
/// </summary>
public Int32 UserId { get; set; } /// <summary>
/// 会员名称
/// </summary>
public String Name { get; set; }
}
}

4.使用方法。在项目过程中。如果需要将两个实体进行转化。使用实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoMapper
{
public class Extension
{
/// <summary>
/// 将user转成userNew
/// </summary>
public static void Model()
{
var user = new UserModel()
{
UserId = ,
Name = "王彬"
};
var userNew = new UserNewModel();
//将老会员实体转成新会员实体
var u = AutoMapperExtension.MapTo<UserModel,UserNewModel>(user); } public static void ModelList()
{
List<UserModel> Users = new List<UserModel>(); var user = new UserModel()
{
UserId = ,
Name = "王彬"
}; Users.Add(user); var userNew = new List<UserNewModel>();
//将老会员实体转成新会员实体
var ulist = AutoMapperExtension.MapToList<UserModel, UserNewModel>(Users);
}
}
}

最新文章

  1. Task三个列子的分享
  2. BigDecimal
  3. 实战p12文件转pem文件
  4. 百度面试题 字符串相似度 算法 similar_text 和页面相似度算法
  5. 用Objective-C写了一个简单的批量更改文件名的程序
  6. js删除数据的几种方法
  7. ionic hybrid备忘
  8. [工具] slf4j-api、slf4j-log4j12以及log4j之间的关系
  9. 如何配置和使用Tomcat访问日志
  10. [Docker基础]Docker安装教程
  11. 复活广州.net俱乐部
  12. VIM学习二: VIM配置代码及效果图
  13. 【消息中间件之RabbitMQ学习】-开篇-001
  14. [UE4]抓取准备
  15. Crystal Reports报表使用 [一]
  16. 外显子分析报错解决方案bin field of BAM record does not equal value computed based on alignment start and end, and length of sequence to which read is aligned
  17. 重写&amp;重载
  18. POJ2724 Purifying Machine
  19. html 线条重叠变粗
  20. JSON.parse()和JSON.stringify()以及stringify()字符串格式化

热门文章

  1. 程序员的软实力武器-smart原则
  2. 开源摄影机:Axiom Camera
  3. Java 截取中英文混合字符串
  4. libevent之Reactor模式
  5. Java集合之Set
  6. UML之构件图
  7. 开源框架VTMagic的使用介绍
  8. LeetCode之“链表”:Reverse Nodes in k-Group
  9. 增量会话对象——DeltaSession
  10. ffdshow 源代码分析 4: 位图覆盖滤镜(滤镜部分Filter)