实际使用中发现很多问题 如果用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的更多相关文章

  1. mapstruct 实体转换及List转换,@Mapper注解转换

    本文参考 https://blog.csdn.net/u012373815/article/details/88367456 主要是为了自己使用方便查询. 这些都是我平时用到了,大家有什么好方法或者有 ...

  2. 【道德经】漫谈实体、对象、DTO及AutoMapper的使用

    写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...

  3. .Net Core2.2 使用 AutoMapper进行实体转换

    一.遇到的问题 在. Core Api 的编写中,我们经常会对一些功能点进行新增编辑操作,同时我们有时也会进行查询,但是我们查询的表的数据与我们返回的数据相差甚大,这是我们有需要自己手动进行类型的转换 ...

  4. DataTable转List<Model>通用类【实体转换辅助类】

    /// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...

  5. HBaseConvetorUtil 实体转换工具

    HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil {        /**    * @Title: convetor    * @De ...

  6. 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?

    当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...

  7. Datatable转实体 实体转换辅助类

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  8. html实体转换

    摘要: 在 HTML 中,某些字符是预留的.在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中 ...

  9. C# 获取config文件 实体转换

    随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...

随机推荐

  1. 丽泽普及2022交流赛day21 社论

    A 暴力 . greater<double> -> greater<int> \(100\) -> \(50\) 代码丢了 . B dp . 考场上代码抢救一下就过 ...

  2. odoo 14 一些常见问题集

    1 # 当你往tree或者form视图中增加action的时候 2 # 记住!千万别重名 3 # 一旦重名,Export.Delete.Archive.Unarchive都会消失不见 4 # tree ...

  3. RestTemplate上传文件

    1.上传的文件是File类型 如果文件保存在本地,即可以通过File file = new File(path) 或者 文件路径地址获取到指定文件 public String uploadFile(F ...

  4. 8. 利用Ansible快速构建MGR | 深入浅出MGR

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 目录 1. 安装ansbile 2. 配置ansible 3. 建立ssh信任 4. 测试ansible 5. 使用ans ...

  5. 万答#15,都有哪些情况可能导致MGR服务无法启动

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 本文转载自微信公众号 "老叶茶馆" 欢迎大家关注! 1.都有 ...

  6. 移动端实现HTML5 mp3录音踩坑指南:系统播放音量变小、一些机型录音断断续续 之 MediaRecorder和AudioWorklet的终极对决

    目录 H5录音见坑填坑 采用MediaRecorder采集音频 音频格式:WebM和PCM 从WebM封装容器中提取PCM数据 录音的兼容性 困扰已久的H5录音时系统播放音量变小的问题 H5录音见坑填 ...

  7. TDM 三部曲 (与 Deep Retrieval)

    推荐系统的主要目的是从海量物品库中高效检索用户最感兴趣的物品,既然是"海量",意味着用户基本不可能浏览完所有的物品,所以才需要推荐系统来辅助用户高效获取感兴趣的信息.同样也正是因为 ...

  8. java-集合排序,队列,散列表map以及如何遍历

    1.1集合排序 可以通过集合的工具类java.util.Collections的静态方法sort需要注意的时,只能对List排序,因为它有序. Collections.sort(list); 排序字符 ...

  9. python包合集-shutil

    一.简介 shutil是 python 中的高级文件操作模块,与os模块形成互补的关系,os主要提供了文件或文件夹的新建.删除.查看等方法,还提供了对文件以及目录的路径操作.shutil模块提供了移动 ...

  10. es5 es6 新增

    es5的新特性 对于数组和字符串都进行了加强 map 遍历 es6的新特性 数组的增强 find 查找findIndex 查找下标 字符的增强 includes 是否包含 (包含返回true 不包含返 ...