.NET之AutoMapper对象映射工具运用
2024-10-09 19:55:28
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);
}
}
}
最新文章
- Task三个列子的分享
- BigDecimal
- 实战p12文件转pem文件
- 百度面试题 字符串相似度 算法 similar_text 和页面相似度算法
- 用Objective-C写了一个简单的批量更改文件名的程序
- js删除数据的几种方法
- ionic hybrid备忘
- [工具] slf4j-api、slf4j-log4j12以及log4j之间的关系
- 如何配置和使用Tomcat访问日志
- [Docker基础]Docker安装教程
- 复活广州.net俱乐部
- VIM学习二: VIM配置代码及效果图
- 【消息中间件之RabbitMQ学习】-开篇-001
- [UE4]抓取准备
- Crystal Reports报表使用 [一]
- 外显子分析报错解决方案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
- 重写&;重载
- POJ2724 Purifying Machine
- html 线条重叠变粗
- JSON.parse()和JSON.stringify()以及stringify()字符串格式化