当前位置:编程学习 > C#/ASP.NET >>

使用AutoMapper实现Dto和Model的自由转换(中)

在上一篇文章中我们构造出了完整的应用场景,包括我们的Model、Dto以及它们之间的转换规则。下面就可以卷起袖子,开始我们的AutoMapper之旅了。
【二】以Convention方式实现零配置的对象映射
我们的AddressDto和Address结构完全一致,且字段名也完全相同。对于这样的类型转换,AutoMapper为我们提供了Convention,正如它的官网上所说的:

引用
AutoMapper uses a convention-based matching algorithm to match up source to destination values.

我们要做的只是将要映射的两个类型告诉AutoMapper(调用Mapper类的Static方法CreateMap并传入要映射的类型):
C#代码 
Mapper.CreateMap<AddressDto, Address>(); 

Mapper.CreateMap<AddressDto, Address>();

然后就可以交给AutoMapper帮我们搞定一切了:
C#代码 
AddressDto dto = new AddressDto  
{  
    Country = "China",  
    City = "Beijing",  
    Street = "Dongzhimen Street",  
    PostCode = "100001" 
};  
Address address = Mapper.Map<AddressDto,Address>(Dto);  
address.Country.ShouldEqual("China");  
address.City.ShouldEqual("Beijing");  
address.Street.ShouldEqual("Dongzhimen Street");  
address.PostCode.ShouldEqual("100001"); 

            AddressDto dto = new AddressDto
            {
                Country = "China",
                City = "Beijing",
                Street = "Dongzhimen Street",
                PostCode = "100001"
            };
            Address address = Mapper.Map<AddressDto,Address>(Dto);
            address.Country.ShouldEqual("China");
            address.City.ShouldEqual("Beijing");
            address.Street.ShouldEqual("Dongzhimen Street");
            address.PostCode.ShouldEqual("100001");

如果AddressDto中有值为空的属性,AutoMapper在映射的时候会把Address中的相应属性也置为空:
C#代码 
Address address = Mapper.Map<AddressDto,Address>(new AddressDto  
                                                       {  
                                                           Country = "China" 
                                                       });  
address.City.ShouldBeNull();  
address.Street.ShouldBeNull();  
address.PostCode.ShouldBeNull(); 

            Address address = Mapper.Map<AddressDto,Address>(new AddressDto
                                                                   {
                                                                       Country = "China"
                                                                   });
            address.City.ShouldBeNull();
            address.Street.ShouldBeNull();
            address.PostCode.ShouldBeNull();

甚至如果传入一个空的AddressDto,AutoMapper也会帮我们得到一个空的Address对象。
C#代码 
Address address = Mapper.Map<AddressDto,Address>(null);  
address.ShouldBeNull(); 

            Address address = Mapper.Map<AddressDto,Address>(null);
            address.ShouldBeNull();

千万不要把这种Convention的映射方式当成“玩具”,它在映射具有相同字段名的复杂类型的时候还是具有相当大的威力的。
例如,考虑我们的BookStoreDto到BookStore的映射,两者的字段名称完全相同,只是字段的类型不一致。如果我们定义好了BookDto到Book的映射规则,再加上上述Convention方式的AddressDto到Address的映射,就可以用“零配置”实现BookStoreDto到BookStore的映射了:
C#代码 
IMappingExpression<BookDto, Book> expression = Mapper.CreateMap<BookDto,Book>();  
// Define mapping rules from BookDto to Book here  
Mapper.CreateMap<AddressDto, Address>();  
Mapper.CreateMap<BookStoreDto, BookStore>(); 

            IMappingExpression<BookDto, Book> expression = Mapper.CreateMap<BookDto,Book>();
            // Define mapping rules from BookDto to Book here
            Mapper.CreateMap<AddressDto, Address>();
            Mapper.CreateMap<BookStoreDto, BookStore>();

然后我们就可以直接转换BookStoreDto了:
C#代码 
BookStoreDto dto = new BookStoreDto  
            &n

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,