First talk about DTO
DTO is what stuff?
DTO (Data Transfer Object) is a data transfer object, it means an object, but all the data inside of it.
Why use DTO?
1, DTO pay more attention to data on the domain object reasonable package, so as not to over-expose the behavior of the field object to the presentation layer
2, DTO is designed for the needs of the UI, and business-oriented domain model is designed. Therefore more suitable for DTO and interactive presentation layer by DTO us to achieve decoupling between the presentation layer and the Model field, and therefore the changes will not affect the UI layer field Model
3, DTO it means data only, does not contain any business logic, are thin type of object that can be flexible depending on the application requirements when using the UI
Auto Folders
Now that we know the benefits of using DTO, then we certainly want to use it right away, but this will involve a problem: how to achieve conversion between DTO and field Model?
There are two ideas, we either convert to write their own code or use a tool. But on the application, I still think that using relatively simple and quick tool, then use the tool bar. In fact, many of these conversion tools, but I decided to use AutoMapper, because it is lightweight enough, but also very popular, large foreign cattle were all using it. Use AutoMapper can easily achieve the conversion between the DTO and field Model, it is a powerful Object-Object Mapping tool.
First, how to add AutoMapper to the project?
In the use of open vs Tools - Libraries Package Manager - Package management console, enter "Install-Package AutoMapper" command, you can add AutoMapper into the project -
Second, eat chestnuts
Chestnut 1 (the mapping between the two types)
1 Mapper.CreateMap<AddressDto, Address>(); 2 AddressDto dto = new AddressDto 3 { 4 Country = "China", 5 City = "ShangHai", 6 Street = "JinZhong Street" 7 }; 8 Address address = Mapper.Map<AddressDto,Address>(Dto);
Chestnut 2 (part two objects mapped field names are not the same)
Address mapping to AddressDto, AddressDto CountryName field corresponds to Address field Country:
1 Mapper.CreateMap<AddressDto, Address>(). ForMember(d => d.Country, opt => opt.MapFrom(s => s.CountryName));
Chestnut (mapping between the list type) 3
Source Type List <Address>, the target type List <AddressDto>:
AutoMapper.Mapper.CreateMap< Address, AddressDto >(); var addressDtoList = AutoMapper.Mapper.Map<List< Address >, List< AddressDto >>( addressList);
4 chestnut (mapped by change search application)
1 public class ProductBll 2 3 { 4 Public IProductRepository productRepository{ set; get; } 5 public ProductDTO CreateProduct(ProductDTO productDTO) 6 { 7 Mapper.CreateMap<ProductDTO, Product>(); 8 Product product = Mapper.Map<ProductDTO, Product>(productDTO); 9 productRepository.AddProduct(product); 10 return productDTO; 11 } 12 13 public List<ProductDTO> GetProduct() 14 { 15 Mapper.CreateMap<Product, ProductDTO>(); 16 List<ProductDTO> arr = new List<ProductDTO>(); 17 productRepository.GetProduct().ForEach(i => 18 { 19 arr.Add(Mapper.Map<Product, ProductDTO>(i)); 20 }); 21 return arr; 22 } 23 24 public ProductDTO ModifyProduct(ProductDTO productDTO) 25 { 26 Mapper.CreateMap<ProductDTO, Product>(); 27 Product product = Mapper.Map<ProductDTO, Product>(productDTO); 28 productRepository.ModifyProduct(product); 29 return productDTO; 30 } 31 }
Third, make it easy to use AutoMapper
Eaten chestnut above, what do you think of it? If you want to continue to eat, then go to see the specific API documentation AutoMapper it! If the project really use the time, I think it should be some sort of method AutoMapper, it is best to package it, here I am in the form of extension methods packaged as AutoMapperHelper, so that future use AutoMapper becomes the SO EASY the ~
. 1 the using the System.Collections; 2 the using the System.Collections.Generic; . 3 the using the System.Data; . 4 the using AutoMapper; . 5 namespace Infrastructure.Utility . 6 . 7 { . 8 /// <Summary> . 9 /// AutoMapper helper extension 10 // / </ Summary> . 11 public static class AutoMapperHelper 12 is { 13 is /// <Summary> 14 /// type mapping 15 /// </ Summary> 16 public static T MapTo<T>(this object obj) 17 { 18 if (obj == null) return default(T); 19 Mapper.CreateMap(obj.GetType(), typeof(T)); 20 return Mapper.Map<T>(obj); 21 } 22 /// <summary> 23 /// 集合列表类型映射 24 /// </summary> 25 public static List<TDestination> MapToList<TDestination>(this IEnumerable source) 26 { 27 foreach (var first in source) 28 { 29 var type = first.GetType(); 30 Mapper.CreateMap(type, typeof(TDestination)); 31 break; 32 } 33 return Mapper.Map<List<TDestination>>(source); 34 } 35 /// <summary> 36 /// 集合列表类型映射 37 /// </summary> 38 public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source) 39 { 40 //IEnumerable<T> 类型需要创建元素的映射 41 Mapper.CreateMap<TSource, TDestination>(); 42 return Mapper.Map<List<TDestination>>(source); 43 } 44 /// <summary> 45 /// 类型映射 46 /// </summary> 47 public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination) 48 where TSource : class 49 where TDestination : class 50 { 51 if (source == null) return destination; 52 Mapper.CreateMap<TSource, TDestination>(); 53 return Mapper.Map(source, destination); 54 } 55 /// <summary> 56 /// DataReader映射 57 /// </summary> 58 public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader) 59 { 60 Mapper.Reset(); 61 Mapper.CreateMap<IDataReader, IEnumerable<T>>(); 62 return Mapper.Map<IDataReader, IEnumerable<T>>(reader); 63 } 64 } 65 }
You can use this chestnuts like the following:
// 对象Utsui ShipInfoModel ShipInfoModel = ShipInfo.MapTo <ShipInfoModel> (); // string table Utsui List <ShipInfoModel> shipInfoModellist = ShipInfoList.MapToList < ShipInfoModel> ();
summary
More use in the project DTO decouple the presentation layer and the Model field, with AutoMapper to achieve conversion DTO and field Model, so AutoMapper fly for a while in your project