AutoMapper 帮助类
AutoMapper帮助类
/// <summary>
/// AutoMapper帮助类
/// </summary>
public static class AutoMapperHelper
{
/// <summary>
/// 类型映射
/// </summary>
public static T MapTo<T>(this object obj)
{
if (obj == null) return default(T);
var config = new MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(T)));
var mapper = config.CreateMapper();
return mapper.Map<T>(obj);
}
/// <summary>
/// 集合列表类型映射
/// </summary>
public static List<TDestination> MapToList<TDestination>(this IEnumerable<TDestination> source)
{
Type sourceType = source.GetType().GetGenericArguments()[]; //获取枚举的成员类型
var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
var mapper = config.CreateMapper();
return mapper.Map<List<TDestination>>(source);
}
/// <summary>
/// 集合列表类型映射
/// </summary>
public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
var mapper = config.CreateMapper();
return mapper.Map<List<TDestination>>(source);
}
/// <summary>
/// 类型映射
/// </summary>
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
where TSource : class
where TDestination : class
{
if (source == null) return destination;
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(source);
}
}
AutoMapFromAttribute
public class AutoMapFromAttribute : AutoMapAttributeBase
{
public MemberList MemberList { get; set; }
public AutoMapFromAttribute(params Type[] targetTypes)
: base(targetTypes)
{
}
public AutoMapFromAttribute(MemberList memberList, params Type[] targetTypes)
: this(targetTypes)
{
this.MemberList = memberList;
}
public override void CreateMap(IMapperConfigurationExpression configuration, Type type)
{
)
return;
foreach (Type targetType in this.TargetTypes)
configuration.CreateMap(targetType, type, MemberList.Destination);
}
public static bool IsNullOrEmpty<T>(ICollection<T> source)
{
if (source != null)
;
return true;
}
}
AutoMapAttributeBase
public abstract class AutoMapAttributeBase : Attribute
{
public Type[] TargetTypes { get; private set; }
protected AutoMapAttributeBase(params Type[] targetTypes)
{
this.TargetTypes = targetTypes;
}
public abstract void CreateMap(IMapperConfigurationExpression configuration, Type type);
}
AutoMapper 帮助类的更多相关文章
- 使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到实体类
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...
- AutoMapper随笔记
平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html#skill 先看效果:(完整Demo:https://git ...
- AutoMapper简单用法
首先在NuGet添加AutoMapper /// <summary> /// AutoMapper帮助类 /// </summary> public static class ...
- C#进阶系列——DDD领域驱动设计初探(五):AutoMapper使用
前言:前篇搭建了下WCF的代码,就提到了DTO的概念,对于为什么要有这么一个DTO的对象,上章可能对于这点不太详尽,在此不厌其烦再来提提它的作用: 从安全上面考虑,领域Model都带有领域业务,让Cl ...
- 升级AutoMapper后遇到的“Missing map”与“Missing type map configuration”问题
前几天发现 AutoMapper 3.3 的一个性能问题(详见:遭遇AutoMapper性能问题:映射200条数据比100条慢了近千倍),于是将 AutoMapper 升级至最新的 5.1.1 看是否 ...
- AutoMapper中的Map和DynamicMap——高手注重细节,思考和总结
近日在做项目的时候,遇到了个怪问题,关于AutoMapper的细节问题,也是不为一般人所关注的. 本人研究AutoMapper也没有多长时间,而且研究的过程中也写了关于AutoMapper的系列基础教 ...
- AutoMapper 使用总结
初识AutoMapper 在开始本篇文章之前,先来思考一个问题:一个项目分多层架构,如显示层.业务逻辑层.服务层.数据访问层.层与层访问需要数据载体,也就是类.如果多层通用一个类,一则会暴露出每层的字 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)
前言 Hi,大家好,我是Rector 时间飞逝,一个星期又过去了,今天还是星期五,Rector在图享网继续跟大家分享系列文本:一步一步创建ASP.NET MVC5程序[Repository+Autof ...
- Asp.Net AutoMapper用法
1.AutoMapper简介 用于两个对象映射,例如把Model的属性值赋值给View Model.传统写法会一个一个属性的映射很麻烦,使用AutoMapper两句代码搞定. 2.AutoMapper ...
随机推荐
- 常用SEO优化
- 解释型vs编译型 动态vs静态 强类型vs弱类型
------------------------------------------------------------ 释型.动态语言与静态语言.强类型语言与弱类型语言的区别 编译型和解释型 我们先 ...
- 使用button的open-type="getUserInfo"引导用户进行授权
https://blog.csdn.net/weixin_39602178/article/details/80295684 一. 前言 小程序官方文档,上面说明 > wx.getUserInf ...
- 模板继承and自定义模板标签和过滤器
自定义模板标签和 过滤器: 因为模板标签和过滤器只给我们提供了 这么多 无法对我们的使用造成更多的便利 ,剩下的就需要我们自己去创建新的 模板标签和过滤器了 1.在settings中的INSTALLE ...
- [翻译] MMMaterialDesignSpinner
MMMaterialDesignSpinner Usage To run the example project, clone the repo, and run pod install from t ...
- Java实例---简单的个人管理系统
代码分析 FileOperate.java package com.ftl.testperson; import java.io.File ; import java.io.FileInputStre ...
- 【MyBatis】MyBatis实现CRUD操作
1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...
- 性能调优的本质、Spark资源使用原理和调优要点分析
本课主题 大数据性能调优的本质 Spark 性能调优要点分析 Spark 资源使用原理流程 Spark 资源调优最佳实战 Spark 更高性能的算子 引言 我们谈大数据性能调优,到底在谈什么,它的本质 ...
- c++计算器后续(3)
自娱自乐: 本来只是想改改第二次的代码规范的,然后好像把原来的代码玩坏了,真是尴尬...然后大概是又发现了一些东西.以上. main的参数: 大概是说main函数的括号里是可以带参数的,写成这个样子: ...
- Mof提权科普
今天再拿一个站的时候遇到了很多问题,拿站的过程就不说了,其中要用到mof提权,不管能不能提下,我进行一个mof提权的科普 这里我综合各类mof提权进行了 综合 首先说一下,无shell情况下的mysq ...