標籤:conf install img .com app 環境 null title try
1.AutoMapper簡單介紹
官網:http://automapper.org/
源碼:https://github.com/AutoMapper/AutoMapper
NUGET安裝:
PM> Install-Package AutoMapper
AutoMapper是基於對象到對象約定的映射工具,常用於(但並不僅限制於)把複雜的物件模型轉為DTO,一般用於ViewModel模式和跨 服務範疇。
AutoMapper給使用者提供了便捷的配置API,就像使用約定來完成自動對應那樣。
AutoMapper包含以下功能:平展、投影、配置驗證、列表和數組、嵌套映射、自訂類型轉換程式、自訂值轉換程式 、自訂值格式程式 、Null值替換
AutoMapper是一款單向映射器。這意味著它並沒有內建映射對象支援來回寫至原始源,除非使用者在更新映射對象之後明確地建立逆向反射。這需要 通過設計完成,因為讓DTO回寫到,比方說:領域模型或其他東西,就會更改它的持久性,同時人們也認為它是反模式的。在這種解決方案中,命令訊息在雙向映射 中往往是更好的選擇。然而,在某些特定環境中,有人可能會為雙向映射辯解,比如:非常簡單的CRUD應用程式。一個支援雙向映射的架構就是Glue。
介紹來自:http://www.oschina.net/p/automapper/similar_projects
2.使用方法
- 【AutoMapper官方文檔】DTO與Domin Model相互轉換(上)
- 【AutoMapper官方文檔】DTO與Domin Model相互轉換(中)
- 【AutoMapper官方文檔】DTO與Domin Model相互轉換(下)
簡單樣本1(兩個類型之間的映射)
// 配置 AutoMapperMapper.CreateMap<Order, OrderDto>();// 執行 mappingOrderDto dto = Mapper.Map<Order, OrderDto>(order);
簡單樣本2(兩個映射的對象有部分欄位名稱不一樣)
Mapper.CreateMap<AddressDto, Address>(). ForMember(d => d.Country, opt => opt.MapFrom(s => s.CountryName));
簡單樣本3(清單類型之間的映射)
AutoMapper.Mapper.CreateMap< Address, AddressDto >();var addressDtoList = AutoMapper.Mapper.Map<List< Address >, List< AddressDto >>( addressList);
參考文章:http://www.cnblogs.com/smileberry/p/3838143.html (另外封裝了AutoMapperHelper,使用更加簡單)
3.AutoMapper + EF6
使用nuget安裝,自動引入AutoMapper和AutoMapper.EF6類庫
PM> Install-Package AutoMapper.EF6
擴充方法一覽
public static TDestination[] ProjectToArray<TDestination>(this IQueryable queryable); public static TDestination[] ProjectToArray<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static Task<TDestination[]> ProjectToArrayAsync<TDestination>(this IQueryable queryable); public static Task<TDestination[]> ProjectToArrayAsync<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static TDestination ProjectToFirst<TDestination>(this IQueryable queryable); public static TDestination ProjectToFirst<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static Task<TDestination> ProjectToFirstAsync<TDestination>(this IQueryable queryable); public static Task<TDestination> ProjectToFirstAsync<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static TDestination ProjectToFirstOrDefault<TDestination>(this IQueryable queryable); public static TDestination ProjectToFirstOrDefault<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static Task<TDestination> ProjectToFirstOrDefaultAsync<TDestination>(this IQueryable queryable); public static Task<TDestination> ProjectToFirstOrDefaultAsync<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static List<TDestination> ProjectToList<TDestination>(this IQueryable queryable); public static List<TDestination> ProjectToList<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static Task<List<TDestination>> ProjectToListAsync<TDestination>(this IQueryable queryable); public static Task<List<TDestination>> ProjectToListAsync<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static IQueryable<TDestination> ProjectToQueryable<TDestination>(this IQueryable queryable); public static IQueryable<TDestination> ProjectToQueryable<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static TDestination ProjectToSingle<TDestination>(this IQueryable queryable); public static TDestination ProjectToSingle<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static Task<TDestination> ProjectToSingleAsync<TDestination>(this IQueryable queryable); public static Task<TDestination> ProjectToSingleAsync<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static TDestination ProjectToSingleOrDefault<TDestination>(this IQueryable queryable); public static TDestination ProjectToSingleOrDefault<TDestination>(this IQueryable queryable, IConfigurationProvider config); public static Task<TDestination> ProjectToSingleOrDefaultAsync<TDestination>(this IQueryable queryable); public static Task<TDestination> ProjectToSingleOrDefaultAsync<TDestination>(this IQueryable queryable, IConfigurationProvider config);
這樣可以很方便的轉換為資料、列表、擷取第一條資料等,還可支援非同步
AutoMapper的使用