private void LoadModel(Model model) { var helper = new Data.Discription.ORM.ORMHelper(); if (model.Properties != null && model.Properties.Count > 0) { return; } GetModelProperties(model); model.AutoSysId = !(model.Properties.Count(p => p.IsCheck && String.IsNullOrEmpty(p.IXGroup)) == 1); if (model.AutoSysId == false) { model.IdProperty = model.Properties.First(p => p.IsCheck && String.IsNullOrEmpty(p.IXGroup));; var Primary = model.IdProperty; if ((Primary.AutoGenerationType == Data.Discription.ORM.GenerationType.OnInSert && (Primary.PropertyType == PropertyType.Int || Primary.PropertyType == PropertyType.Long))) { Primary.PropertyType = PropertyType.IdentifyId; } else if (Primary.AutoGenerationType == GenerationType.OnInSert && String.IsNullOrEmpty(Primary.Format) == false && Primary.PropertyType == PropertyType.String) { Primary.PropertyType = PropertyType.SerialNo; } Primary.AllowDBNull = false; } model.ShowProperty = model.Properties.FirstOrDefault(p => p.Name.ToLower().IndexOf("name") >= 0 || p.PropertyName.ToLower().IndexOf("name") >= 0); if (model.ShowProperty == null) { model.ShowProperty = model.IdProperty; } if (model.ShowProperty == null) { model.ShowProperty = model.Properties.FirstOrDefault(); } LoadRelation(model); }
public void GetModelProperties(Model model) { if (model.Properties == null) { model.Properties = new List <Property>(); } Type type = System.Reflection.Assembly.LoadFile(model.Module.AssemblyFile).GetType(model.ClassName); Soway.Data.Discription.ORM.ORMHelper helper = new Data.Discription.ORM.ORMHelper(); // List<Property> items = new List<Property>(); foreach (System.Reflection.PropertyInfo propertyInfo in type.GetProperties())//.OrderBy(p=>(typeof(ICollection).IsAssignableFrom(p.PropertyType)))) { Property property = new Property(); property.PropertyName = propertyInfo.Name; if (model.Properties.Count(p => p.PropertyName == propertyInfo.Name) > 0) { property = model.Properties.First(p => p.Name == propertyInfo.Name); } else { model.Properties.Add(property); } property.PropertyName = propertyInfo.Name; property.Name = propertyInfo.Name; property.CanGet = propertyInfo.CanRead; property.CanSet = propertyInfo.CanWrite; var dis = propertyInfo.GetCustomAttributes(typeof(Soway.Data.Discription.Display.ShowDescriptionAttribute), true); if (dis.Length > 0) { property.Name = (dis[0] as Soway.Data.Discription.Display.ShowDescriptionAttribute).DisplayName; } if ( model.Properties.Count(p => p.Name == property.Name) > 0) { property.Name = propertyInfo.Name; } property.PropertyType = Soway.Data.PropertyTypeAdaper.GetPropertyType(propertyInfo.PropertyType); property.IsArray = typeof(ICollection).IsAssignableFrom(propertyInfo.PropertyType); var cols = helper.GetColNameAttributes(propertyInfo); if (property.PropertyType == PropertyType.BusinessObject) { property.Model = GetModel(propertyInfo.PropertyType); if (cols.Count == 1) { property.DBName = helper.GetColName(type, propertyInfo.Name); property.IsCheck = cols[0].IsKey; property.IXGroup = cols[0].KeyGroupName; property.AutoGenerationType = cols[0].IsAutoGenerate; if (cols[0].EncrpytType == Data.Discription.ORM.EncryptType.RadomDECS) { property.PropertyType = PropertyType.RadomDECS; } else if (cols[0].EncrpytType == Data.Discription.ORM.EncryptType.MD5) { property.PropertyType = PropertyType.MD5; } if (cols[0].IsIdentify) { property.PropertyType = PropertyType.IdentifyId; property.IsCheck = true; } property.IsMultiMap = false; property.Format = cols[0].FormatStr; } else { property.Format = cols[0].FormatStr; property.IsMultiMap = true; foreach (var col in cols) { property.DBMaps.Add(new MultiDBMap() { PropertyName = col.PropertyName, DBColName = helper.GetColName(col, propertyInfo) }); } } } else { if (cols[0].NoMap == false) { property.DBName = helper.GetColName(type, propertyInfo.Name); } property.IsCheck = cols[0].IsKey; property.IXGroup = cols[0].KeyGroupName; property.AutoGenerationType = cols[0].IsAutoGenerate; if (cols[0].IsIdentify) { if (propertyInfo.PropertyType != typeof(Guid)) { property.PropertyType = PropertyType.IdentifyId; } property.IsCheck = true; } if (cols[0].EncrpytType == Data.Discription.ORM.EncryptType.RadomDECS) { property.PropertyType = PropertyType.RadomDECS; } else if (cols[0].EncrpytType == Data.Discription.ORM.EncryptType.MD5) { property.PropertyType = PropertyType.MD5; } property.Format = cols[0].FormatStr; property.IsMultiMap = false; if (property.PropertyType == PropertyType.Enum) { property.Model = GetModel(propertyInfo.PropertyType); } else { property.Model = null; } } } // model.Properties = items; // return items; }
public Model GetModel(Type businuessType) { //如果是集合,则返回集合的的项类型 if (typeof(ICollection).IsAssignableFrom(businuessType)) { if (businuessType.GetElementType() == null) { if (businuessType.GetGenericArguments().Count() >= 1) { businuessType = businuessType.GetGenericArguments()[0]; } else { return(null); } } else { businuessType = businuessType.GetElementType(); } } //可以存储的(1,为类,而且有table定义;2.为枚举) if (((businuessType.IsClass) && businuessType.GetCustomAttributes(typeof(Soway.Data.Discription.ORM.TableAttribute), true).Length > 0) || businuessType.IsEnum) { if (models.ContainsKey(businuessType.ToString()) == false) { //System.Diagnostics.Trace.WriteLine(String.Format("Create Model of {0},Count:{1}",businuessType.Name,models.Keys.Count)); Model model = new Model(); models.Add(businuessType.ToString(), model); Soway.Data.Discription.ORM.ORMHelper helper = new Data.Discription.ORM.ORMHelper(); model.Name = businuessType.Name; model.ClassName = businuessType.FullName; model.Module = GetModule(businuessType.Assembly); if (businuessType.IsEnum) { model.ModelType = ModelType.Enum; } else if (businuessType.IsAbstract)// ||businuessType.IsInterface) { model.ModelType = ModelType.AbstractClass; } else { model.ModelType = ModelType.Class; model.Operations = new List <Operation>(); model.Operations.Add(new Operation() { Name = "删除", BaseOperationType = BaseOperationType.Delete, ArgModel = model }); } if (model.ModelType != ModelType.Enum) { model.BaseModel = GetModel(businuessType.BaseType); var table = helper.GetTableAttribute(businuessType); model.DataTableName = helper.GetTable(businuessType).Replace("[", "").Replace("]", ""); model.IsView = table.IsView; LoadModel(model); } else { foreach (var i in Enum.GetValues(businuessType)) { model.EnumValues.Add(new EnumValues() { String = i.ToString(), Value = (int)i }); } model.ShowProperty = model.Properties.FirstOrDefault(p => p.PropertyName == "String"); } } return(models[businuessType.ToString()]); } else { return(null); } }