AutoMapper的使用


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.使用方法

簡單示例1(兩個類型之間的映射)

// 配置 AutoMapper
Mapper.CreateMap<Order, OrderDto>();
// 執行 mapping
OrderDto 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);

這樣可以很方便的轉換為數據、列表、獲取第一條數據等,還可支持異步

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM