关于c#:如何在-C-中使用-AutoMapper

28次阅读

共计 3350 个字符,预计需要花费 9 分钟才能阅读完成。

译文链接:https://www.infoworld.com/art…

AutoMapper 是一个十分风行的 object-to-object 映射库,它的目标就是帮忙你实现不同类型对象之间的映射,举一个例子,在 DDD 开发模式中,你可能须要实现将 DTO object 映射为 Model object,在过来,你须要人肉的将这两个类型下的属性字段进行一一映射,当初 AutoMapper 就能够帮你节俭 这种冗余的模板式代码 匹配所消耗的工夫。

开始玩 AutoMapper 之前,你须要在 Visual Studio 中创立一个 Project 并且装置 AutoMapper,你能够从 NuGet 上下载,也能够在 NuGet Package Manager Console 控制台输出如下命令:


PM> Install-Package AutoMapper

应用 AutoMapper 创立映射关系

像 AutoMapper 这样的 object-to-object 映射工具,它必须可能做到将一种输出类型转换成另一个输入类型,是不是很拗口,能够先思考上面的两个类。


    public class AuthorModel
    {
        public int Id
        {get; set;}
        public string FirstName
        {get;set;}
        public string LastName
        {get; set;}
        public string Address
        {get; set;}
    }

    public class AuthorDTO
    {
        public int Id
        {get; set;}
        public string FirstName
        {get; set;}
        public string LastName
        {get; set;}
        public string Address
        {get; set;}
    }

接下来,上面的代码段将会告知你如何应用 AutoMapper 在 AuthorModel 和 AuthorDTO 这两个对象之间创立一个 mapping 关系。


var config = new MapperConfiguration(cfg => {cfg.CreateMap<AuthorModel, AuthorDTO>();
            });

最终的 mapping 转换,你还须要减少几句上面的代码,实现两个类型之间的转换。


IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);

一个 AutoMapper 的例子

接下来能够上一些数据了,能够参考上面的代码片段,我筹备先在 source object 上赋值,而后执行 AutoMapper 中的 Map 办法之后,在 destination object 上原样显示进去。


var config = new MapperConfiguration(cfg => {cfg.CreateMap<AuthorModel, AuthorDTO>();
            });
IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
source.Id = 1;
source.FirstName = "Joydip";
source.LastName = "Kanjilal";
source.Address = "India";
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
Console.WriteLine("Author Name:"+ destination.FirstName + " " + destination.LastName);

当你执行完这段代码之后,destination object 上的 Author Name 将会输入到管制台上,指标对象上的 FirstName 和 LastName 和 source object 上的这两个属性值保持一致,阐明 automapper 曾经帮你胜利映射。

值得注意的是,AutoMapper 不仅仅能够 mapping 一个类,还能够 mapping 多个类,默认状况下,AutoMapper 会依照默认约定匹配,也就是被 mapping 的对象之间具备雷同的属性名称能力被胜利映射,但现实情况下,很多被映射的属性名称是不雷同的,这个时候就须要人工染指指定 mapping 关系让 AutoMapper 依照你设定的执行,假设你须要实现 Contact 到 ContactDetails 之间的映射,上面的例子展现了如何去实现这种关系。


var config = new MapperConfiguration(cfg => {cfg.CreateMap<AuthorModel, AuthorDTO>()
                .ForMember(destination => destination.ContactDetails,
               opts => opts.MapFrom(source => source.Contact));
            });

上面的语句能够创立最终的 destination object 对象。


var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);

有时候你曾经生成了 destination object,在这根底上你还想二次映射,这时能够应用上面代替语句。


iMapper.Map(sourceObject, destinationObject);

实质上来说,下面的这段代码罕用于匹配两个已存在的 object。

应用 AutoMapping 的 projections 性能

AutoMapper 提供了十分好的 projections 性能,projections ???????? 的中央在于在 mapping 映射时能够忽视两者的 object 数据结构是否统一,比如说让 source 的多个属性 映射到 destination 的一个属性上,而下面咱们始终探讨的都是一对一的 object mapping。

接下来咱们一起学习下 projection,举个例子,思考如下类。


    public class Address
    {public string City { get; set;}
        public string State {get; set;}
        public string Country {get; set;}
    }

接下来在 AuthorModel 类中新增一个 Address 属性用来存储 Author 的地址信息,批改后的 AuthorModel 类如下:


    public class AuthorModel
    {
        public int Id
        {get; set;}
        public string FirstName
        {get;set;}
        public string LastName
        {get; set;}
        public Address Address
        {get; set;}
    }

而后再更新一下 AuthorDTO 类


    public class AuthorDTO
    {
        public int Id
        {get; set;}
        public string FirstName
        {get; set;}
        public string LastName
        {get; set;}
        public string City {get; set;}
        public string State {get; set;}
        public string Country {get; set;}
    }

接下来咱们须要将 AuthorDTO 映射到 AuthorModel,上面的代码片段展现了如何去实现。


var config = new MapperConfiguration(cfg => {cfg.CreateMap<AuthorDTO, AuthorModel>()
                   .ForMember(destination => destination.Address,
              map => map.MapFrom(
                  source => new Address
                  {
                      City = source .City,
                      State = source .State,
                      Country = source.Country
                  }));

我会在后续的文章中持续探讨 AutoMapper 更多的高级个性,当初,你能够通过这个链接:http://automapper.org/ 去学习更多的 AutoMapper 常识。

更多高质量干货:参见我的 GitHub: dotnetfly

正文完
 0