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

  • 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
  • 【AutoMapper官方文档】DTO与Domin Model相互转换(中)
  • 【AutoMapper官方文档】DTO与Domin Model相互转换(下)

简单示例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);

这样可以很方便的转换为数据、列表、获取第一条数据等,还可支持异步

 

你可能感兴趣的:(AutoMapper的使用)