其他分享
首页 > 其他分享> > 实体类与实体DTO类之间的转换

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

作者:互联网

原文:实体类与实体DTO类之间的转换

实体类与实体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);

标签:实体类,DTO,Name,实体,item,tIn,TOut,typeof,Expression
来源: https://www.cnblogs.com/lonelyxmas/p/10382267.html