public static List <T> ConvertDataToObject <T>(DataTable dataTable) { List <T> list = new List <T>(); Type type = typeof(T); if (Type.GetTypeCode(type) == TypeCode.Object) { IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(typeof(T)); Dictionary <string, string> mapping = new Dictionary <string, string>(StringComparer.CurrentCultureIgnoreCase); foreach (PropertySchema item in entityProxy.GetPropertyList()) { OrmColumnAttribute ormColumnAttribute = item.GetCustomerAttribute <OrmColumnAttribute>(); if (ormColumnAttribute != null && !string.IsNullOrEmpty(ormColumnAttribute.DbColumnName)) { mapping.Add(ormColumnAttribute.DbColumnName, item.Name); } } list = ConvertDataToObject <T>(dataTable, mapping); } else { foreach (DataRow row in dataTable.Rows) { list.Add((T)Convert(row[0], type)); } } return(list); }
protected virtual string GetQuerySelectClause(Type modelType) { IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(modelType); string tableName = OrmUtil.GetTableName(modelType); TableSchema tableSchema = DbSession.Current.GetTableSchema(tableName); StringBuilder selectClause = new StringBuilder(); foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) { string columnName = propertySchema.Name; ModelPropertyAttribute modelPropertyAttribute = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>(); if (modelPropertyAttribute != null) { if (!modelPropertyAttribute.Visible) { continue; } } OrmColumnAttribute ormColumnAttribute = propertySchema.GetCustomerAttribute <OrmColumnAttribute>(); if (ormColumnAttribute != null && !string.IsNullOrEmpty(ormColumnAttribute.DbColumnName)) { columnName = ormColumnAttribute.DbColumnName; } if (tableSchema != null && !tableSchema.ContainsColumn(columnName)) { continue; } selectClause.AppendFormat("{0},", columnName); } selectClause.Remove(selectClause.Length - 1, 1); return(selectClause.ToString()); }
/// <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); }
/* * * private List<BeeDataAdapter> GetSearchInfo(BeeDataAdapter dataAdapter) * { * List<BeeDataAdapter> result = new List<BeeDataAdapter>(); * * EntityProxy<T> entityProxy = EntityProxyManager.Instance.GetEntityProxy<T>(); * foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) * { * ModelPropertyAttribute modelPropertyAttribute * = propertySchema.GetCustomerAttribute<ModelPropertyAttribute>(); * if (modelPropertyAttribute != null) * { * if (!modelPropertyAttribute.Visible) * { * continue; * } * * if (!modelPropertyAttribute.Queryable) * { * continue; * } * * BeeDataAdapter dataItem = new BeeDataAdapter(); * * string descriptionInfo = modelPropertyAttribute.Description; * if (string.IsNullOrEmpty(descriptionInfo)) * { * descriptionInfo = propertySchema.Name; * } * * dataItem.Add("name", propertySchema.Name); * dataItem.Add("Type", propertySchema.PropertyType); * dataItem.Add("QueryType", modelPropertyAttribute.QueryType); * dataItem.Add("Description", descriptionInfo); * dataItem.Add("MappingName", modelPropertyAttribute.MappingName); * * result.Add(dataItem); * } * } * * return result; * } * * private List<BeeDataAdapter> GetHeaderInfo() * { * List<BeeDataAdapter> result = new List<BeeDataAdapter>(); * EntityProxy<T> entityProxy = EntityProxyManager.Instance.GetEntityProxy<T>(); * * ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute<ModelAttribute>(); * * foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) * { * ModelPropertyAttribute modelPropertyAttribute * = propertySchema.GetCustomerAttribute<ModelPropertyAttribute>(); * * BeeDataAdapter dataAdapter = new BeeDataAdapter(); * string descriptionInfo; * if (modelPropertyAttribute != null) * { * if (!modelPropertyAttribute.Visible) * { * continue; * } * * descriptionInfo = modelPropertyAttribute.Description; * if (string.IsNullOrEmpty(descriptionInfo)) * { * descriptionInfo = propertySchema.Name; * } * * dataAdapter.Add("description", descriptionInfo); * dataAdapter.Add("name", propertySchema.Name); * * if (modelPropertyAttribute.ColumnWidth != 0) * { * dataAdapter.Add("width", modelPropertyAttribute.ColumnWidth.ToString()); * } * * if (!string.IsNullOrEmpty(modelPropertyAttribute.Align)) * { * dataAdapter.Add("align", modelPropertyAttribute.Align); * } * * if (modelPropertyAttribute.OrderableFlag) * { * dataAdapter.Add("orderField", propertySchema.Name); * } * * } * else * { * dataAdapter.Add("description", propertySchema.Name); * dataAdapter.Add("Name", propertySchema.Name); * } * * result.Add(dataAdapter); * } * * * return result; * } * * private List<BeeDataAdapter> GetDetailInfo() * { * List<BeeDataAdapter> result = new List<BeeDataAdapter>(); * * EntityProxy<T> entityProxy = EntityProxyManager.Instance.GetEntityProxy<T>(); * * ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute<ModelAttribute>(); * string identityColumn = OrmUtil.GetIdentityColumnName<T>(); * * foreach (PropertySchema propertySchema in entityProxy.GetPropertyList()) * { * ModelPropertyAttribute modelPropertyAttribute * = propertySchema.GetCustomerAttribute<ModelPropertyAttribute>(); * BeeDataAdapter dataAdapter = new BeeDataAdapter(); * string descriptionInfo; * if (modelPropertyAttribute != null) * { * descriptionInfo = modelPropertyAttribute.Description; * if (string.IsNullOrEmpty(descriptionInfo)) * { * descriptionInfo = propertySchema.Name; * } * } * else * { * descriptionInfo = propertySchema.Name; * } * * dataAdapter.Add("description", descriptionInfo); * dataAdapter.Add("name", propertySchema.Name); * bool readOnly = false; * * if (string.Compare(identityColumn, propertySchema.Name, true) == 0) * { * readOnly = true; * } * * dataAdapter.Add("readonly", readOnly); * dataAdapter.Add("mappingname", modelPropertyAttribute.MappingName); * * result.Add(dataAdapter); * } * * return result; * } * */ private BeeAutoModelInfo InitBeeAutoModelInfo() { BeeAutoModelInfo result = new BeeAutoModelInfo(); List <BeeDataAdapter> headerInfo = new List <BeeDataAdapter>(); List <BeeDataAdapter> searchInfo = new List <BeeDataAdapter>(); List <BeeDataAdapter> detailInfo = new List <BeeDataAdapter>(); Dictionary <string, string> dataMappingInfo = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); ModelAttribute modelAttribute = entityProxy.GetCustomerAttribute <ModelAttribute>(); string identityColumn = OrmUtil.GetIdentityColumnName <T>(); TableSchema tableSchema = null; using (DbSession dbSession = GetDbSession()) { tableSchema = dbSession.GetTableSchema(OrmUtil.GetTableName <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; } if (tableSchema != null && !tableSchema.ContainsColumn(columnName)) { continue; } BeeDataAdapter headerItem = GetHeaderItem(propertySchema); if (headerItem != null) { headerInfo.Add(headerItem); } BeeDataAdapter searchItem = GetSearchItem(propertySchema); if (searchItem != null) { searchInfo.Add(searchItem); } BeeDataAdapter detailItem = GetDetailItem(propertySchema, identityColumn); if (detailItem != null) { detailInfo.Add(detailItem); } ModelPropertyAttribute modelPropertyAttribute = propertySchema.GetCustomerAttribute <ModelPropertyAttribute>(); if (modelPropertyAttribute != null && !string.IsNullOrEmpty(modelPropertyAttribute.MappingName)) { dataMappingInfo.Add(propertySchema.Name, modelPropertyAttribute.MappingName); } if (propertySchema.PropertyType.IsEnum && !dataMappingInfo.ContainsKey("mappingname")) { dataMappingInfo.Add(propertySchema.Name, propertySchema.PropertyType.ToString()); } } result.DetailInfo = detailInfo; result.HeaderInfo = headerInfo; result.SearchInfo = searchInfo; result.DataMappingInfo = dataMappingInfo; return(result); }