Tiny Mapper

时间:2023-03-09 14:37:19
Tiny Mapper

今天看到一个对象映射工具-TinyMapper##

1.介绍###

Tiny Mapper是一个.net平台的开源的对象映射组件,其它的对象映射组件比如AutoMapper有兴趣的可以去看,Tiny Mapper的github地址TinyMapper,Tiny Mapper最大的特点是快。

2.使用###

首先可以通过NuGet下载安装:

using Nelibur.ObjectMapper;

我们分别定义两个对象:Person,PersonDtO

public class Person
{
public string Address { get; set; } public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Guid Id { get; set; } public string NickName { get; set; } public DateTime CreateTime { get; set; } public string Phone { get; set; }
} public class PersonDto
{
public Guid Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Address { get; set; } public string NickName { get; set; } public string Phone { get; set; }
}

Bind两个类:

TinyMapper.Bind<Person, PersonDto>();

使用:

var person = new Person
{
Id = Guid.NewGuid(),
FirstName = "X",
LastName = "Yy",
Email = "24048@qq.com",
Address = "Wall Street",
CreateTime = DateTime.Now,
NickName = "xiaoye",
Phone = "13913541238",
}; var personDto = TinyMapper.Map<PersonDto>(person); Console.WriteLine(personDto.Phone);

当然Bind还有重载的方法:

TinyMapper.Bind<Person, PersonDto>(config =>
{
config.Ignore(x => x.Id);// 忽略Id
config.Bind(x => x.FirstName, y => y.FirstName);// 指定映射名
});

更多的信息可以参考源站的文档