AutoMapper的使用

來源:互聯網
上載者:User

標籤: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的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.