【愚公系列】2023年03月 .NET CORE工具案例-对象映射Master的使用
【摘要】 前言对象映射框架Master可以帮助开发人员将对象映射到数据库,以进行数据持久化。它还可以支持ORM(对象关系映射),以及其他数据库技术,比如存储过程。它可以帮助开发人员更快、更有效地完成数据库操作。Master官网:https://github.com/MapsterMapper/Mapster 一、对象映射master的使用 1.安装包Mapster 2.基本使用先定义两个一样的类so...
前言
对象映射框架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);
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)