Mapper 实体转换Entiy to Dto
实际使用中发现很多问题 如果用EFcore 框架,这个表达式树生成一个新的实体
导致EFcore 跟踪失败!
/// <summary>
/// 生成表达式目录树 泛型缓存
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
public class ExpressionGenericMapper<TIn, TOut>//Mapper`2
{
private static Func<TIn, TOut> _FUNC = null;
private static Func<TIn, TOut, TOut> _FUNC2 = null;
public static TOut Trans(TIn t)
{
return _FUNC(t);
} /// <summary>
/// 批量实体转换
/// </summary>
/// <param name="list">被转换的实体List</param>
/// <returns></returns>
public static List<TOut> TransList(List<TIn> list)
{
List<TOut> outs = new List<TOut>();
foreach (var item in list)
{
outs.Add(Trans(item));
}
return outs;
} /// <summary>
/// 把TOut 按照TIn来更新
/// </summary>
/// <param name="news"></param>
/// <param name="old"></param>
/// <returns></returns>
public static TOut Modify(TIn news, TOut old)
{
return _FUNC2(news, old);
} static ExpressionGenericMapper()
{
Type stringType = typeof(string);
#region 创建构造对象的委托
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "m");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
var type = typeof(TIn);
if (item.PropertyType.IsClass && item.PropertyType != stringType)//排除String以外的引用类型,不进行赋值
{
continue;
}
var prop = type.GetProperty(item.GetMappingName());//从Out类找映射的字段名
if (prop != null)//如果TIn里能找到该属性
{ MemberExpression property = Expression.Property(parameterExpression, prop);
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
}
foreach (var item in typeof(TOut).GetFields())
{
var type = typeof(TIn);
if (item.FieldType.IsClass && item.FieldType != stringType)//排除String以外的引用类型,不进行赋值
{
continue;
}
var prop = type.GetField(item.GetMappingName());//从Out类找映射的字段名
if (prop != null)
{
MemberExpression property = Expression.Field(parameterExpression, prop);
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
} }
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
_FUNC = lambda.Compile();//拼装是一次性的
}
#endregion
#region 修改对象的委托
{ ParameterExpression oExpression = Expression.Parameter(typeof(TOut), "o");
ParameterExpression nExpression = Expression.Parameter(typeof(TIn), "n");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
var type = typeof(TIn);
if (item.PropertyType.IsClass && item.PropertyType != stringType)//排除String以外的引用类型,不进行赋值
{
continue;
}
var prop = type.GetProperty(item.GetMappingName());
if (prop != null)
{
MemberExpression property = Expression.Property(nExpression, prop);
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
else
{
var propOld = typeof(TOut).GetProperty(item.Name);
MemberExpression property = Expression.Property(oExpression, propOld);
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
}
foreach (var item in typeof(TOut).GetFields())
{
var type = typeof(TIn);
if (item.FieldType.IsClass && item.FieldType != stringType)//排除String以外的引用类型,不进行赋值
{
continue;
}
var prop = type.GetField(item.GetMappingName());
if (prop != null)
{
MemberExpression property = Expression.Field(nExpression, prop);
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
else
{
var propOld = typeof(TOut).GetField(item.Name);
MemberExpression property = Expression.Field(oExpression, propOld);
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
Expression<Func<TIn, TOut, TOut>> lambda = Expression.Lambda<Func<TIn, TOut, TOut>>(memberInitExpression, new ParameterExpression[]
{
nExpression, oExpression
});
_FUNC2 = lambda.Compile();//拼装是一次性的
}
#endregion
} } public static class ExpressionGenericMapperExtend
{
public static string GetMappingName(this PropertyInfo prop)
{ if (prop.IsDefined(typeof(MappingAttribute), true))
{
MappingAttribute display = prop.GetCustomAttribute(typeof(MappingAttribute)) as MappingAttribute;
return display.MappingName;
}
return prop.Name; } public static string GetMappingName(this FieldInfo prop)
{ if (prop.IsDefined(typeof(MappingAttribute), true))
{
MappingAttribute display = prop.GetCustomAttribute(typeof(MappingAttribute)) as MappingAttribute;
return display.MappingName;
}
return prop.Name; }
} [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MappingAttribute : Attribute
{
public string MappingName;
public MappingAttribute(string _MappingName)
{
this.MappingName = _MappingName;
} }
Mapper 实体转换Entiy to Dto的更多相关文章
- mapstruct 实体转换及List转换,@Mapper注解转换
本文参考 https://blog.csdn.net/u012373815/article/details/88367456 主要是为了自己使用方便查询. 这些都是我平时用到了,大家有什么好方法或者有 ...
- 【道德经】漫谈实体、对象、DTO及AutoMapper的使用
写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...
- .Net Core2.2 使用 AutoMapper进行实体转换
一.遇到的问题 在. Core Api 的编写中,我们经常会对一些功能点进行新增编辑操作,同时我们有时也会进行查询,但是我们查询的表的数据与我们返回的数据相差甚大,这是我们有需要自己手动进行类型的转换 ...
- DataTable转List<Model>通用类【实体转换辅助类】
/// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...
- HBaseConvetorUtil 实体转换工具
HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil { /** * @Title: convetor * @De ...
- 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?
当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...
- Datatable转实体 实体转换辅助类
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...
- html实体转换
摘要: 在 HTML 中,某些字符是预留的.在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中 ...
- C# 获取config文件 实体转换
随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...
随机推荐
- 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
上篇文章讲了MySQL架构体系,了解到MySQL Server端的优化器可以生成Explain执行计划,而执行计划可以帮助我们分析SQL语句性能瓶颈,优化SQL查询逻辑,今天就一块学习Explain执 ...
- 题解【洛谷 P1466 [USACO2.2]集合 Subset Sums】
题目传送门 设 \(sum=1+2+3+4+\dots+n=\dfrac{n(n+1)}{2}\). 如果 \(2\nmid sum\),则显然没有方案. 如果 \(2\mid sum\),则这两个集 ...
- php数组和对象相互转换
function arrayToObject($e){ if( gettype($e)!='array' ) return; foreach($e as $k=>$v){ if( gettype ...
- Odoo14 一些好用的开源的模块
# odoo14中一些好用的开源的模块 1.intero_reload_form 刷新按钮(页面数据刷新,而不是按F5刷新整个页面) 2.ms_magic_button 弹框下拉选项 3.sessio ...
- Win10 x64 安装Eplan P8 2.7 小结
一.软件安装准备及过程 为免版权纠纷,此处不提供下载链接,请自行查找资源. 1.打开"Electric P8 2.7.3.11418"目录,以管理员身份运行"setup. ...
- Oracle-DDL,DML理解以及应用
SQL语句:虽然SQL语句不区分大小写,但是字符串的值时区分大小写的.SQL是结构化查询语句,操作数据库需要向数据库发送SQL语句,数据库会理解SQL语句中含义并执行SQL语句分为:DDL(数据定义语 ...
- java学习第二天面向对象.day08
this 在方法中表示调用当前方法的对象,this与主方法中对象类名调用是同理的,也是去指向堆中的地址. this可以解决成员变量和形参的问题 使用构造器还是setter方法 构造器:在创建对象的时侯 ...
- python使用pickle序列化对象读取输出二进制文件
import pickle class tick: name = '牛牛牛' age = 10 samp = [1,2,3,'aaa',[12,3],tick()] with open('te.xxx ...
- mac M1通过homebrew安装python3报错Error: Command failed with exit 128: git
fatal: not in a git directoryError: Command failed with exit 128: git 只需要运行 git config --global --ad ...
- Sentinel控制台1.8.3修改源码,修改配置后推送到Nacos
目录 1. 接着上一篇 2. 思路 3. 下载Sentinel源码 4. 看Gateway里面读取的配置信息 5. 修改Sentinel控制台源码 6. 熔断规则测试 7. 限流规则测试 8. 打包使 ...