zoukankan      html  css  js  c++  java
  • 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);
    复制代码

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

  • 相关阅读:
    ThinkPHP中PHPExcel的使用(包含日期格式)
    python学习一
    autocomplete 在火狐中文输入的问题
    NPM WARN 错误分析汇总
    react+reatrouter+redux学习笔记
    a++与++a的区别
    ES6箭头函数(Arrow Functions)
    document.referrer
    modem无法编译
    linux时钟浅析 转载
  • 原文地址:https://www.cnblogs.com/yanglang/p/7058612.html
Copyright © 2011-2022 走看看