123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using QWPlatform.Models;
- namespace PMS.BusinessModels
- {
- /// <summary>
- /// 创 建 人:王海洋
- /// 创建日期:2018-12-10
- /// 功能描述:业务对象与数据实体对象进行转换
- /// </summary>
- public class UIModel
- {
- /// <summary>
- /// 将当前对象转为数据实体对象
- /// </summary>
- /// <typeparam name="T">指定的数据实体对象</typeparam>
- /// <returns>返回数据实体</returns>
- public T ToDBModel<T>() where T : DataModelBase, new()
- {
- var type = typeof(T);
- //获取到当前类的字段映射数据列及值
- var dict = this.GetUIModelValues();
- var model = new T();
- //获取所有属性
- var props = type.GetProperties();
- foreach (var p in props)
- {
- //找出定义了字段的属性
- var t = typeof(FieldBaseAttribute);
- var objs = p.GetCustomAttributes(t, false);
- if (objs.Length > 0)
- {
- //获取一个对象
- var obj = objs.First() as FieldBaseAttribute;
- if (obj != null)
- {//返回该字段对象
- var key = obj.ColumnName.Trim();
- if (dict.ContainsKey(key))
- {
- var val = dict[key];
- if (val != null)
- {
- var csVal = QWPlatform.SystemLibrary.DBTypeConvert.DbTypeToCSType(val);
- //获取原型预定义的type
- if (p.PropertyType.IsGenericType)
- {
- //获取泛型基础类型
- var baseTypes = p.PropertyType.GetGenericArguments();
- //获取原型定义类型
- var ptype = p.PropertyType.GetGenericTypeDefinition();
- if (ptype == typeof(Nullable<>))
- {
- if (baseTypes.Length > 0)
- {//获取默认第一个类型
- var baseType = baseTypes[0];
- var cv = Convert.ChangeType(csVal, baseType);
- p.SetValue(model, cv, null);
- }
- }
- }
- else
- {
- var vtype = csVal.GetType();
- //将类型进行转换
- var rval = Convert.ChangeType(csVal, vtype);
- p.SetValue(model, rval, null);
- }
- }
- else
- {
- p.SetValue(model, DefaultForType(p.PropertyType), null);
- }
- }
- }
- }
- }
- return model;
- }
- /// <summary>
- /// 将数据库的Model转换到UI的model中
- /// </summary>
- /// <typeparam name="T">数据库Model的类型</typeparam>
- /// <param name="t">数据库Model的实例</param>
- public void FromDBModel<T>(T t) where T : DataModelBase, new()
- {
- //获取数据字典
- var dict = this.GetDBModelValues<T>(t);
- //获取UI的属性列表
- var uiprops = this.GetType().GetProperties();
- foreach (var p in uiprops)
- {//循环所有UI的属性。进行设置值
- var attrs = p.GetCustomAttributes(typeof(DbColumn), true);
- if (attrs.Length > 0)
- {
- var dbcolAttr = attrs.First() as DbColumn;
- var key = dbcolAttr.ColName;
- if (dict.ContainsKey(key))
- {//字典中包括该值
- var val = dict[key];
- //转换后的值
- var csVal = QWPlatform.SystemLibrary.DBTypeConvert.DbTypeToCSType(val);
- //设置到当前对象中
- p.SetValue(this, csVal, null);
- }
- }
- }
- }
- //获取当前模型的值
- private Dictionary<string, object> GetUIModelValues()
- {
- var type = this.GetType();
- //获取所有属性
- var props = type.GetProperties();
- var dict = new Dictionary<string, object>();
- foreach (var p in props)
- {
- //找出定义了字段的属性
- var t = typeof(DbColumn);
- var objs = p.GetCustomAttributes(t, false);
- if (objs.Length > 0)
- {
- //获取一个对象
- var obj = objs.First() as DbColumn;
- if (obj != null)
- {//返回该字段对象
- if (!dict.ContainsKey(obj.ColName))
- {
- var val = this.GetModelValue(this, p.Name);
- dict.Add(obj.ColName, val);
- }
- }
- }
- }
- return dict;
- }
- //获取数据库返回的model对象值
- private Dictionary<string, object> GetDBModelValues<T>(T t) where T : DataModelBase
- {
- var type = t.GetType();
- //获取所有属性
- var props = type.GetProperties();
- var dict = new Dictionary<string, object>();
- foreach (var p in props)
- {
- //找出定义了字段的属性
- var fieldAttr = typeof(FieldBaseAttribute);
- var objs = p.GetCustomAttributes(fieldAttr, false);
- if (objs.Length > 0)
- {
- //获取一个对象
- var obj = objs.First() as FieldBaseAttribute;
- if (obj != null)
- {//返回该字段对象
- var key = obj.ColumnName;
- if (!dict.ContainsKey(key))
- {//字段名:值
- var val = this.GetModelValue(t, p.Name);
- dict.Add(key, val);
- }
- }
- }
- }
- return dict;
- }
- /*根据模型取出对象值*/
- private object GetModelValue(object obj, string propName)
- {
- var type = obj.GetType();
- PropertyInfo propertyInfo = type.GetProperty(propName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
- if (propertyInfo != null)
- {
- return propertyInfo.GetValue(obj, null);
- }
- return null;
- }
- /*获取当前类型的默认值*/
- private static object DefaultForType(Type targetType)
- {
- return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
- }
- /// <summary>
- /// 将当前对象转换为json数据
- /// </summary>
- /// <returns></returns>
- public string ToJson()
- {
- return QWPlatform.SystemLibrary.Utils.Strings.ObjectToJson(this);
- }
- }
- }
|