public List <EntityInfo> Load(IEnumerable <MSSQLTable <TableContent, ColumnContent> > tables) { var entities = _entityLoader.Load(tables); //TODO: Remove it after implement interfaces in entities var iremovable = TypeUsageInfo.CreateInterface("IRemovable", "ProfiCraftsman.Contracts"); var ihasid = TypeUsageInfo.CreateInterface("IHasId", "ProfiCraftsman.Contracts", new[] { typeof(int).ToUsageInfo() }); var iintervalfields = TypeUsageInfo.CreateInterface("IIntervalFields", "ProfiCraftsman.Contracts"); var isystemfields = TypeUsageInfo.CreateInterface("ISystemFields", "ProfiCraftsman.Contracts"); var ihastitle = TypeUsageInfo.CreateInterface("IHasTitle", "ProfiCraftsman.Contracts", new[] { typeof(int).ToUsageInfo() }); foreach (var entity in entities) { entity.InheritsFrom(iremovable); entity.InheritsFrom(ihasid); #region IntervalFields { var fromDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "FromDate"); var toDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "ToDate"); if (fromDate != null && toDate != null) { entity.InheritsFrom(iintervalfields); AddExplicitProperty(fromDate, iintervalfields, entity); AddExplicitProperty(toDate, iintervalfields, entity); } } #endregion var tableContent = tables. First(table => table.Name == entity.TableName && table.Schema == entity.TableSchemaName).Content; if (!string.IsNullOrEmpty(tableContent.TitleFieldName)) { entity.InheritsFrom(ihastitle); AddHasTitleProperty(ihastitle, entity, tableContent); } #region SystemFields { var createDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "CreateDate"); var changeDate = entity.SimpleProperties.FirstOrDefault(p => p.Name == "ChangeDate"); if (createDate != null && changeDate != null) { entity.InheritsFrom(isystemfields); AddCreateDateProperty(createDate, isystemfields, entity); AddChangeDateProperty(createDate, changeDate, isystemfields, entity); } } #endregion } return(entities); }
public List <ModelInfo> Generate(IEnumerable <ITable <TableContent, ColumnContent> > tables, IEnumerable <EntityInfo> entities) { var result = new List <ModelInfo>(); var tableArray = tables.ToArray(); var baseModel = TypeUsageInfo.CreateClass("BaseModel", string.Empty); var iintervalfields = TypeUsageInfo.CreateInterface("IIntervalModelFields", string.Empty); foreach (var entity in entities) { var table = tableArray.First(t => t.Name == entity.TableName && t.Schema == entity.TableSchemaName); var modelColumns = table.Columns.Where(c => c.Content.InModel).ToArray(); if (modelColumns.Length == 0) { continue; } var properties = modelColumns.Select(c => new PropertyModelInfo(entity.SimpleProperties.First(p => p.ColumnName == c.Name), c)).ToList(); var model = new ModelInfo(entity, table, properties); model.Attributes.Add(AttributeDictionary.DataContract); model.InheritsFrom(baseModel); #region IntervalFields { var fromDate = properties.FirstOrDefault(p => p.Name == "fromDate"); var toDate = properties.FirstOrDefault(p => p.Name == "toDate"); if (fromDate != null && toDate != null) { entity.InheritsFrom(iintervalfields); EntitiesManager.AddExplicitProperty(fromDate, iintervalfields, entity); EntitiesManager.AddExplicitProperty(toDate, iintervalfields, entity); } } #endregion foreach (var property in model.ModelProperties) { if (property.Content.IsModelRequired) { property.Attributes.Add(AttributeDictionary.Required); } property.Attributes.Add(AttributeDictionary.DataMember); property.Description = string.Format("Model property for <see cref=\"{0}.{1}\"/> entity", entity.Name, property.Property.Name); } result.Add(model); } return(result); }