Exemple #1
0
        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());
        }
Exemple #2
0
        public static DataTable ForImport <T>(DataTable dataTable) where T : class
        {
            DataTable result = new DataTable();
            Dictionary <string, string> mapping        = new Dictionary <string, string>();
            Dictionary <string, string> mappingMapping = new Dictionary <string, string>();
            EntityProxy <T>             entityProxy    = EntityProxyManager.Instance.GetEntityProxy <T>();

            foreach (PropertySchema propertySchema in entityProxy.GetPropertyList())
            {
                string columnName = propertySchema.Name;

                string newColumnName = columnName;
                ModelPropertyAttribute modelPropertyAttribute
                    = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>();
                if (modelPropertyAttribute != null)
                {
                    if (!modelPropertyAttribute.Visible)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(modelPropertyAttribute.Description))
                    {
                        newColumnName = modelPropertyAttribute.Description;
                    }

                    if (!string.IsNullOrEmpty(modelPropertyAttribute.MappingName))
                    {
                        mappingMapping.Add(newColumnName, modelPropertyAttribute.MappingName);
                    }
                }

                mapping.Add(newColumnName, columnName);
                result.Columns.Add(columnName);
            }

            dataTable.Columns.Add("ImportResult");

            foreach (DataRow row in dataTable.Rows)
            {
                DataRow newRow = result.NewRow();

                foreach (string columnName in mapping.Keys)
                {
                    string itemResult = string.Empty;

                    if (!dataTable.Columns.Contains(columnName))
                    {
                        continue;
                    }

                    if (mappingMapping.ContainsKey(columnName))
                    {
                        DataTable data = DataMapping.Instance.GetMapping(mappingMapping[columnName]) as DataTable;

                        DataRow[] rows = data.Select("name='{0}'".FormatWith(row[columnName]));
                        if (rows.Length == 1)
                        {
                            itemResult = rows[0]["id"].ToString();
                        }
                        else
                        {
                            row["ImportResult"] = "无法匹配{0}:{1}".FormatWith(mappingMapping[columnName], row[columnName]);
                        }
                    }
                    else
                    {
                        itemResult = row[columnName].ToString().Trim();
                    }

                    newRow[mapping[columnName]] = itemResult;
                }

                result.Rows.Add(newRow);
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Get a new DataTable contains the new column name and the value in the datarow
        /// according to the OrmColumnAttribute and ModelPropertyAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static DataTable ForExport <T>(DataTable dataTable) where T : class
        {
            DataTable result = new DataTable();
            Dictionary <string, string> mapping        = new Dictionary <string, string>();
            Dictionary <string, string> mappingMapping = new Dictionary <string, string>();
            EntityProxy <T>             entityProxy    = EntityProxyManager.Instance.GetEntityProxy <T>();

            foreach (PropertySchema propertySchema in entityProxy.GetPropertyList())
            {
                string             columnName         = propertySchema.Name;
                OrmColumnAttribute ormColumnAttribute = propertySchema.GetCustomerAttribute <OrmColumnAttribute>();
                if (ormColumnAttribute != null && !string.IsNullOrEmpty(ormColumnAttribute.DbColumnName))
                {
                    columnName = ormColumnAttribute.DbColumnName;
                }

                string newColumnName = columnName;
                ModelPropertyAttribute modelPropertyAttribute
                    = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>();
                if (modelPropertyAttribute != null)
                {
                    if (!modelPropertyAttribute.Visible)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(modelPropertyAttribute.Description))
                    {
                        newColumnName = modelPropertyAttribute.Description;
                    }

                    if (!string.IsNullOrEmpty(modelPropertyAttribute.MappingName))
                    {
                        mappingMapping.Add(columnName, modelPropertyAttribute.MappingName);
                    }
                }

                mapping.Add(columnName, newColumnName);
                result.Columns.Add(newColumnName);
            }

            foreach (DataRow row in dataTable.Rows)
            {
                DataRow newRow = result.NewRow();

                foreach (string columnName in mapping.Keys)
                {
                    string itemResult = string.Empty;

                    if (mappingMapping.ContainsKey(columnName))
                    {
                        itemResult = DataMapping.Instance.Mapping(mappingMapping[columnName], row[columnName].ToString());
                    }
                    else
                    {
                        itemResult = row[columnName].ToString();
                    }

                    newRow[mapping[columnName]] = itemResult;
                }

                result.Rows.Add(newRow);
            }

            return(result);
        }
Exemple #4
0
        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>
        /// Common Process for any Object(Model) and Objects's Property
        /// </summary>
        /// <param name="TModel"></param>
        /// <returns>arrar of ModelAttribute and List<ObjectParameter> </returns>
        public object[] GetModalWithParameters(T TModel, string OperationName)
        {
            var allModelAttributes = TModel.GetType().GetCustomAttributes(true);

            ModelAttribute objModelAttribute = null;

            foreach (var attribute in allModelAttributes)
            {
                if (attribute.GetType() == typeof(ModelAttribute))
                {
                    objModelAttribute = attribute as ModelAttribute;
                    break;
                }
            }
            if (objModelAttribute == null)
            {
                return new object[] { }
            }
            ;

            //To generate the list of  SqlParameters
            var objectParameters = new List <ObjectParameter>();

            foreach (var objModelProperty in TModel.GetType().GetProperties())
            {
                var allPropertyAttributes = objModelProperty.GetCustomAttributes(true);

                ModelPropertyAttribute objModelPropertyAttribute = null;
                foreach (var attribute in allPropertyAttributes)
                {
                    if (attribute.GetType() == typeof(ModelPropertyAttribute))
                    {
                        objModelPropertyAttribute = attribute as ModelPropertyAttribute;
                        break;
                    }
                }


                var operationFlg = objModelPropertyAttribute != null &&
                                   (OperationName.Equals("DELETE") == objModelPropertyAttribute.IsDeleteParameter ||
                                    OperationName.Equals("INSERT") == objModelPropertyAttribute.IsInsertParameter ||
                                    OperationName.Equals("SELECT") == objModelPropertyAttribute.IsSelectParameter ||
                                    OperationName.Equals("UPDATE") == objModelPropertyAttribute.IsUpdateParameter);


                if (operationFlg)
                {
                    var proValue        = objModelProperty.GetValue(TModel, new object[] { });
                    var proTypeFullName = objModelProperty.PropertyType.FullName;
                    var proType         = Type.GetType((proTypeFullName.Contains("System.Nullable") && proTypeFullName.Split(new string[] { "[[" }, StringSplitOptions.RemoveEmptyEntries).Length > 1) ?
                                                       proTypeFullName.Split(new string[] { "[[" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(',')[0] : proTypeFullName);

                    objectParameters.Add(proValue != null ?
                                         new ObjectParameter(objModelPropertyAttribute.ParameterName, proValue) :
                                         new ObjectParameter(objModelPropertyAttribute.ParameterName, proType));
                }
            }

            return(new object[] { objModelAttribute, objectParameters });
        }
    }