Tiny Mapper

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

1.介绍

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

2.使用

首先能够经过NuGet下载安装:github

using Nelibur.ObjectMapper;app

咱们分别定义两个对象: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两个类:ui

TinyMapper.Bind<Person, PersonDto>();.net

使用:code

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);// 指定映射名
});

更多的信息能够参考源站的文档文档

相关文章
相关标签/搜索