首页 > 其他 > 详细

实体类与实体DTO类之间的转换

时间:2019-05-21 11:20:00      阅读:118      评论:0      收藏:0      [点我收藏+]

实体类与实体DTO类之间的转换

1.通过使用第三方序列化反序列化工具Newtonsoft.Json

通过Nuget包管理器添加Newtonsoft.Json
技术分享图片
构造一个Trans的静态方法:

public static TOut Trans<TIn,TOut>(TIn tIn)
        {
            return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));
        }

前端调用:

People people = new People
            {
                Id = 1,
                Name = "ddd",
                Class = 1

            };
PeopleCopy peopleCopy = SerializeMapper.Trans<People, PeopleCopy>(people);

2.通过反射实现

构造一个Trans的静态方法:

public static TOut Trans<TIn,TOut>(TIn tIn)
        {
            TOut tOut = Activator.CreateInstance<TOut>();          
            foreach (var outfield in tOut.GetType().GetFields())
            {
                foreach (var infield in tIn.GetType().GetFields())
                {
                    if (outfield.Name.Equals(infield.Name))
                    {
                        outfield.SetValue(tOut, infield.GetValue(tIn));
                        break;
                    }                   
                }
            }
            foreach (var outProperty in tOut.GetType().GetProperties())
            {
                foreach (var inProperty in tIn.GetType().GetProperties())
                {
                    if (outProperty.Name.Equals(inProperty.Name))
                    {
                        outProperty.SetValue(tOut,inProperty.GetValue(tIn));
                        break;
                    }
                }

            }
            return tOut;
        }

前端调用:

People people = new People
            {
                Id = 1,
                Name = "ddd",
                Class = 1

            };
PeopleCopy peopleCopy = ReflectionMapper.Trans<People, PeopleCopy>(people);

3.通过表达式目录树加字典缓存实现

静态字典:

public static Dictionary<string, Object> _Dictionary = new Dictionary<string, object>();

构造一个Trans的静态方法:

public static TOut Trans<TIn, TOut>(TIn tIn)
        {   
            TOut tOut = Activator.CreateInstance<TOut>();
            string key = string.Format("funckey_{0}_{1}", tIn.GetType().Name, tOut.GetType().Name);
            if (!_Dictionary.ContainsKey(key))
            {
                ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
                List<MemberBinding> memberBindingsList = new List<MemberBinding>();
                foreach (var item in typeof(TOut).GetProperties())
                {
                    MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, property);
                    memberBindingsList.Add(memberBinding);
                }
                foreach (var item in typeof(TOut).GetFields())
                {
                    MemberExpression filed = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
                    MemberBinding memberBinding = Expression.Bind(item, filed);
                    memberBindingsList.Add(memberBinding);
                }
                MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingsList.ToArray());
                Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
                    {
                        parameterExpression
                    });
                Func<TIn, TOut> func = lambda.Compile();
                _Dictionary[key] = func;
            }
            return ((Func<TIn, TOut>)_Dictionary[key]).Invoke(tIn);
        }

前端调用:

People people = new People
            {
                Id = 1,
                Name = "ddd",
                Class = 1

            };
PeopleCopy peopleCopy = ExpressionMapper.Trans<People, PeopleCopy>(people);

4. 通过表达式目录树加泛型缓存实现(静态构造函数)

构造一个泛型缓存类:

 public class ExpressionGenericMapper<TIn,TOut>
    {
        private static Func<TIn, TOut> _FUNC = null;
        static ExpressionGenericMapper()
        {
            ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn),"p");
            List<MemberBinding> memberBindingsList = new List<MemberBinding>();
            foreach (var item in typeof(TOut).GetProperties())
            {
                MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
                MemberBinding memberBinding = Expression.Bind(item, property);
                memberBindingsList.Add(memberBinding);
            }
            foreach (var item in typeof(TOut).GetFields())
            {
                MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
                MemberBinding memberBinding = Expression.Bind(item, property);
                memberBindingsList.Add(memberBinding);
            }
            MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingsList.ToArray());
            Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
            {
                    parameterExpression
            });
            _FUNC = lambda.Compile();//拼装是一次性的
        }

        public static TOut Trans(TIn tIn)
        {
           return  _FUNC(tIn);
        }
    }

前端调用:

People people = new People
            {
                Id = 1,
                Name = "ddd",
                Class = 1

            };
PeopleCopy peopleCopy = ExpressionGenericMapper.Trans<People, PeopleCopy>(people);

原文地址:https://blog.csdn.net/m0_37591671/article/details/80386122

实体类与实体DTO类之间的转换

原文:https://www.cnblogs.com/jpfss/p/10898536.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!