/// <summary> /// Provided the method to copy the value from src to target via map. /// if the mapping is null or empty, so use the property name mapping. /// </summary> /// <param name="src">the source data.</param> /// <param name="target">the target data.</param> /// <param name="map">the property mapping.</param> public static void CopyProperty(object src, object target, Dictionary <string, string> mapping) { ThrowExceptionUtil.ArgumentNotNull(src, "src"); ThrowExceptionUtil.ArgumentNotNull(target, "target"); IEntityProxy srcProxy = EntityProxyManager.Instance.GetEntityProxyFromType(src.GetType()); IEntityProxy targetProxy = EntityProxyManager.Instance.GetEntityProxyFromType(target.GetType()); if (mapping != null) { foreach (string item in mapping.Keys) { targetProxy.SetPropertyValue(target, mapping[item], srcProxy.GetPropertyValue(src, item)); } } else { List <PropertySchema> list = srcProxy.GetPropertyList(); foreach (PropertySchema item in list) { targetProxy.SetPropertyValue(target, item.Name, srcProxy.GetPropertyValue(src, item.Name)); } } }
/// <summary> /// Provided to fomat string using the razor format. /// </summary> /// <param name="format">string format</param> /// <param name="obj">the instance.</param> /// <returns>the converted string.</returns> public static string RazorFormat(this string format, object obj) { ThrowExceptionUtil.ArgumentNotNull(format, "format"); ThrowExceptionUtil.ArgumentNotNull(obj, "obj"); string result = string.Empty; List <string> propertyList = new List <string>(); Match match = FormatRegex.Match(format); while (match.Success) { propertyList.Add(match.Groups["name"].Value); match = match.NextMatch(); } result = format; if (obj is DataRow) { DataRow rowItem = obj as DataRow; foreach (string item in propertyList) { if (rowItem.Table.Columns.Contains(item)) { result = result.Replace("@" + item, rowItem[item].ToString()); } } } else if (obj is BeeDataAdapter) { BeeDataAdapter dataAdapter = obj as BeeDataAdapter; foreach (string item in propertyList) { result = result.Replace("@" + item, dataAdapter.Format(item)); } } else { IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(obj.GetType()); foreach (string item in propertyList) { object propertyValue = entityProxy.GetPropertyValue(obj, item); result = result.Replace("@" + item, propertyValue == null ? string.Empty : propertyValue.ToString()); } } return(result); }
public static string CommonToString <T>(T value) where T : class { if (value == null) { return("null"); } IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); List <PropertySchema> list = entityProxy.GetPropertyList(); StringBuilder builder = new StringBuilder(); foreach (PropertySchema schema in list) { object targetValue = entityProxy.GetPropertyValue(value, schema.Name); ModelPropertyAttribute modelProperty = schema.GetCustomerAttribute <ModelPropertyAttribute>(); string title = schema.Name; if (modelProperty != null) { if (!string.IsNullOrEmpty(modelProperty.Description)) { title = modelProperty.Description; } } if (schema.PropertyType == typeof(DateTime)) { continue; } if (schema.PropertyType.IsValueType) { targetValue = targetValue == null ? "null" : targetValue.ToString(); } builder.AppendFormat("{0}:{1},", title, targetValue); } return(builder.ToString()); }
public static string CompareObject <T>(T src, T target, CompareObjectType compareObjectType) where T : class { StringBuilder builder = new StringBuilder(); if (src == null) { builder.AppendFormat("新增:{0}", CommonToString(target)); } else if (target == null) { builder.AppendFormat("删除:{0}", CommonToString(src)); } else { builder.AppendFormat("修改:"); IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); List <PropertySchema> list = entityProxy.GetPropertyList(); foreach (PropertySchema schema in list) { object srcValue = entityProxy.GetPropertyValue(src, schema.Name); object targetValue = entityProxy.GetPropertyValue(target, schema.Name); ModelPropertyAttribute modelProperty = schema.GetCustomerAttribute <ModelPropertyAttribute>(); string title = schema.Name; if (modelProperty != null) { if (!string.IsNullOrEmpty(modelProperty.Description)) { title = modelProperty.Description; } } if (schema.PropertyType == typeof(DateTime)) { continue; } if (schema.PropertyType.IsValueType) { srcValue = srcValue == null ? "null" : srcValue.ToString(); targetValue = targetValue == null ? "null" : targetValue.ToString(); } if (srcValue != null) { if (targetValue == null && (compareObjectType == CompareObjectType.TargetPropertyNullIgnore || compareObjectType == CompareObjectType.EitherPropertyNullIgnore)) { continue; } if (!srcValue.Equals(targetValue)) { builder.AppendFormat("{0}:{1}->{2},", title, srcValue, targetValue); } } else { if (compareObjectType == CompareObjectType.SrcPropertyNullIgnore || compareObjectType == CompareObjectType.EitherPropertyNullIgnore) { continue; } if (targetValue != null) { builder.AppendFormat("{0}:{1}->{2},", title, srcValue, targetValue); } } } } if (builder.Length == 3) { return(string.Empty); } else { return(builder.ToString()); } }
/// <summary> /// 从一个对象实例中构造一个DataAdapter. /// 若是String的话, 则采用简单Json构造。 /// 符合正则: "(?<name>.*?)":"(?<value>.*?)" /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="value">对象值</param> /// <returns>DataAdapter实例</returns> public static BeeDataAdapter From <T>(T value) where T : class { BeeDataAdapter dataAdapter = new BeeDataAdapter(); if (value != null) { if (value is string) { string temp = value.ToString(); if (!string.IsNullOrEmpty(temp)) { Dictionary <string, object> dict = SerializeUtil.FromJson <Dictionary <string, object> >(temp); foreach (string item in dict.Keys) { dataAdapter[item] = dict[item]; } } } else if (value is DataRow) { var dataRow = value as DataRow; if (dataRow != null) { foreach (DataColumn column in dataRow.Table.Columns) { object dbValue = dataRow[column]; if (dbValue == DBNull.Value) { continue; } dataAdapter.Add(column.ColumnName, dataRow[column]); } } } else { IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(value.GetType()); foreach (PropertySchema item in entityProxy.GetPropertyList()) { object propertyValue = entityProxy.GetPropertyValue(value, item.Name); if (ReflectionUtil.IsNullableType(item.PropertyType) && propertyValue == null) { continue; } if (propertyValue is Enum) { propertyValue = ((Enum)propertyValue).ToString("D"); } if (item.PropertyType.UnderlyingSystemType == typeof(DateTime)) { if (string.Compare("modifytime", item.Name, true) == 0 || string.Compare("updatetime", item.Name, true) == 0) { propertyValue = DateTime.Now; } if ((DateTime)propertyValue == DateTime.MinValue) { if (string.Compare("createtime", item.Name, true) == 0) { propertyValue = DateTime.Now; } else { continue; } } } dataAdapter.Add(item.Name, propertyValue); } } } return(dataAdapter); }
private DataTable ConvertToDataTable(object cacheValue) { DataTable table = new DataTable(); if (cacheValue is DataTable) { table = cacheValue as DataTable; return(table); } IDictionary dictValue = cacheValue as IDictionary; if (dictValue != null) { object firstValue = null; foreach (object item in dictValue.Values) { firstValue = item; break; } if (firstValue != null) { if (Type.GetTypeCode(firstValue.GetType()) != TypeCode.Object) { table.Columns.Add("id"); table.Columns.Add("name"); foreach (object key in dictValue.Keys) { DataRow row = table.NewRow(); row["id"] = key; row["name"] = dictValue[key]; table.Rows.Add(row); } } else { table.Columns.Add("id"); IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(firstValue.GetType()); foreach (PropertySchema item in entityProxy.GetPropertyList()) { if (string.Compare(item.Name, "id", true) != 0) { table.Columns.Add(item.Name); } } foreach (object key in dictValue.Keys) { DataRow row = table.NewRow(); row["id"] = key; object itemValue = dictValue[key]; foreach (PropertySchema item in entityProxy.GetPropertyList()) { row[item.Name] = entityProxy.GetPropertyValue(itemValue, item.Name); } table.Rows.Add(row); } } } } #region Convert the List<> data to datatable IList list = cacheValue as IList; if (list != null && list.Count > 0) { object firstValue = list[0]; if (firstValue != null && Type.GetTypeCode(firstValue.GetType()) == TypeCode.Object) { IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(firstValue.GetType()); foreach (PropertySchema item in entityProxy.GetPropertyList()) { table.Columns.Add(item.Name); } foreach (object key in list) { DataRow row = table.NewRow(); foreach (PropertySchema item in entityProxy.GetPropertyList()) { row[item.Name] = entityProxy.GetPropertyValue(key, item.Name); } table.Rows.Add(row); } } } #endregion return(table); }