(文章目录)
前言
对象映射框架Master可以帮助开发人员将对象映射到数据库,以进行数据持久化。它还可以支持ORM(对象关系映射),以及其他数据库技术,比如存储过程。它可以帮助开发人员更快、更有效地完成数据库操作。
Master官网:https://github.com/MapsterMapper/Mapster
一、对象映射master的使用
1.安装包
Mapster
2.基本使用
先定义两个一样的类sourceObject和DestObjectTest
public class SourceObjectTest
{
public string Name { get; set; }
public string Sex { get; set; }
public string Address { get; set; }
public List<int> Age { get; set; }
}
2.1 普通做法
using ConsoleTest;
using Mapster;
SourceObjectTest sourceObject = new SourceObjectTest();
sourceObject.Name = "愚公";
sourceObject.Age = new List<int> { 20, 13, 26 };
sourceObject.Address = "福建省XX市XX县";
sourceObject.Sex = "男";
//-------------------------------------------------普通做法-----------------------------------------------
DestObjectTest destObject = new DestObjectTest();
destObject.Name = sourceObject.Name;
destObject.Sex = sourceObject.Sex;
destObject.Address = sourceObject.Address;
destObject.Age = sourceObject.Age;
2.2 基本映射之映射到新对象
using ConsoleTest;
using Mapster;
SourceObjectTest sourceObject = new SourceObjectTest();
sourceObject.Name = "愚公";
sourceObject.Age = new List<int> { 20, 13, 26 };
sourceObject.Address = "福建省XX市XX县";
sourceObject.Sex = "男";
//-------------------------------------------------基本映射之映射到新对象-----------------------------------------------
DestObjectTest destObject1 = sourceObject.Adapt<DestObjectTest>();
2.3 基本映射之映射到现有对象
using ConsoleTest;
using Mapster;
SourceObjectTest sourceObject = new SourceObjectTest();
sourceObject.Name = "愚公";
sourceObject.Age = new List<int> { 20, 13, 26 };
sourceObject.Address = "福建省XX市XX县";
sourceObject.Sex = "男";
//-------------------------------------------------基本映射之映射到现有对象-----------------------------------------------
DestObjectTest destObject = new DestObjectTest();
destObject.Name = "李四";
destObject.Age = new List<int> { 20 };
destObject.Address = "中国XX省XX市XX县";
destObject.Sex = "女";
destObject = sourceObject.Adapt(destObject);
2.4 IQueryable映射到新对象
using (MyDbContext context = new MyDbContext())
{
// 使用 ProjectToType 映射到目标类型
var destinations = context.Sources.ProjectToType<Destination>().ToList();
// 手动编写映射
var destinations = context.Sources.Select(c => new Destination {
Id = p.Id,
Name = p.Name,
Surname = p.Surname,
....
})
.ToList();
}
这个就不做多说了主要是数据库查询出来的数据直接映射到新对象
3.自定义映射
当我们的映射对象的属性与源对象的属性不一致时我们就可以使用映射器。映射器的主要作用就是使得SourceObjectTest的name属性和DifferentDestObjectTest的UserName是一个意思,只是创建Model类的时候取得名字不一样,实现不一样名称的映射。
定义DifferentDestObjectTest类
public class DifferentDestObjectTest
{
public string UserName { get; set; }
public string Sex { get; set; }
public string Address { get; set; }
public List<int> Age { get; set; }
}
3.1 直接在 TypeAdapterConfig 配置对象的映射关系
using ConsoleTest;
using Mapster;
using MapsterMapper;
SourceObjectTest sourceObject = new SourceObjectTest();
sourceObject.Name = "愚公";
sourceObject.Age = new List<int> { 20, 13, 26 };
sourceObject.Address = "福建省XX市XX县";
sourceObject.Sex = "男";
//-------------------------------------------------TypeAdapterConfig-----------------------------------------------
var config = new TypeAdapterConfig();
config.ForType<SourceObjectTest, DifferentDestObjectTest>()
.Map(dest => dest.UserName, src => src.Name)
.Map(dest => dest.Address, src => src.Name + src.Address);
var mapper = new Mapper(config);
DifferentDestObjectTest differentDestObjectTest = mapper.Map<DifferentDestObjectTest>(sourceObject);
3.2 使用接口的方式
1、定义配置类
public class DifferentDestObjectTestRegister : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.ForType<SourceObjectTest, DifferentDestObjectTest>()
.Map(dest => dest.UserName, src => src.Name)
.Map(dest => dest.Address, src => src.Name + src.Address);
}
}
2、使用
using ConsoleTest;
using Mapster;
using MapsterMapper;
using System.Reflection;
SourceObjectTest sourceObject = new SourceObjectTest();
sourceObject.Name = "愚公";
sourceObject.Age = new List<int> { 20, 13, 26 };
sourceObject.Address = "福建省XX市XX县";
sourceObject.Sex = "男";
//-------------------------------------------------TypeAdapterConfig-----------------------------------------------
var config = new TypeAdapterConfig();
config.Scan(Assembly.GetExecutingAssembly());
var mapper = new Mapper(config);
DifferentDestObjectTest differentDestObjectTest = mapper.Map<DifferentDestObjectTest>(sourceObject);
3.2 使用映射配置的方式
using ConsoleTest;
using Mapster;
using MapsterMapper;
using System.Reflection;
SourceObjectTest sourceObject = new SourceObjectTest();
sourceObject.Name = "愚公";
sourceObject.Age = new List<int> { 20, 13, 26 };
sourceObject.Address = "福建省XX市XX县";
sourceObject.Sex = "男";
//-------------------------------------------------映射配置-----------------------------------------------
TypeAdapterConfig<SourceObjectTest, DifferentDestObjectTest>
.NewConfig()
.Map(dest => dest.UserName,
src => src.Name);
DifferentDestObjectTest differentDestObjectTest = TypeAdapter
.Adapt<SourceObjectTest, DifferentDestObjectTest>(sourceObject);
标签:CORE,sourceObject,Name,映射,05,XX,Master,using,new
From: https://blog.51cto.com/u_15437432/6294680