Пример #1
0
        private void GenerateNavigation(EntityMeta entityMeta)
        {
            Console.WriteLine(string.Format("Generate navigation for entity {0}.", entityMeta.Name));
            if (string.IsNullOrEmpty(entityMeta.ScaffoldGroupTitle))
            {
                return;
            }

            var group = dbConfig.ModuleGroups.Include(g => g.App).Where(g => g.App.Id == appId && g.Title.Equals(entityMeta.ScaffoldGroupTitle)).SingleOrDefault();

            if (group == null)
            {
                group = new ModuleGroup()
                {
                    App = app, Title = entityMeta.ScaffoldGroupTitle, OrderNo = entityMeta.ScaffoldGroupOrderNo
                };
                dbConfig.ModuleGroups.Add(group);
                dbConfig.SaveChanges();
            }

            var module = dbConfig.Modules.Include(m => m.Group).Where(m => m.Title == entityMeta.Name && m.ModuleType == ModuleType.AutoGenerated).SingleOrDefault();

            if (module == null)
            {
                module = new Module()
                {
                    Group = group, ModuleType = ModuleType.AutoGenerated, Title = entityMeta.Name, OrderNo = entityMeta.ScaffoldModuleOrderNo, ScaffoldEntity = entityMeta.Name
                };
                dbConfig.Modules.Add(module);
                dbConfig.SaveChanges();
            }
        }
Пример #2
0
        private StringBuilder GenerateLookUpDataSources(EntityMeta entityMeta)
        {
            Console.WriteLine("INFO: Generating lookup data sources for entity {0}", entityMeta.Name);
            var ds = new StringBuilder();

            var lookUps       = entityMeta.EntityProps.Where(p => p.PropType == PropType.LookUp).Select(x => x.LookUpEntity).Distinct().ToList();
            var childEntities = entityMeta.ChildProps.Where(p => p.PropType == PropType.Collection).Select(x => x.CollectionEntity).ToList();

            foreach (var childEntity in childEntities)
            {
                lookUps.AddRange(childEntity.EntityProps.Where(p => p.PropType == PropType.LookUp).Select(x => x.LookUpEntity).Where(l => l != entityMeta).Distinct().ToList());
            }
            if (lookUps.Count == 0)
            {
                return(ds);
            }
            var result = lookUps.Distinct();

            foreach (var lookUp in result)
            {
                ds = ds.AppendFormat("  <ef:EntityDataSource ID=\"{0}DataSource\" runat=\"server\" ContextTypeName=\"{1}\" EntitySetName=\"{2}\" />{3}",
                                     lookUp.EntitySetName.ToCamelCase(), dbApp.GetType().FullName, lookUp.EntitySetName, Environment.NewLine);
            }
            return(ds);
        }
Пример #3
0
        private void ConvertPropertyType(EntityProp entityProp, EntityMeta entityMeta, Type type)
        {
            switch (Type.GetTypeCode(type))
            {
            case TypeCode.String:
            case TypeCode.Char:
                entityProp.PropType = PropType.Text;
                break;

            case TypeCode.Byte:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Single:
                entityProp.PropType = PropType.Number;
                break;

            case TypeCode.DateTime:
                entityProp.PropType = PropType.Date;
                break;

            case TypeCode.Boolean:
                entityProp.PropType = PropType.Boolean;
                break;

            default:
                if (type.IsEnum)
                {
                    entityProp.PropType = PropType.Enum;
                }
                else if (typeof(IEnumerable).IsAssignableFrom(type) && type.IsGenericType)
                {
                    entityProp.PropType = PropType.Collection;
                }
                else if (type.IsClass && type.Namespace == entityMeta.Namespace)
                {
                    entityProp.PropType = PropType.LookUp;
                }
                else if (type.BaseType == typeof(ValueType))
                {
                    entityProp.PropType = PropType.Text;
                }
                else
                {
                    entityProp.PropType = PropType.Unsupported;
                }
                break;
            }
        }
Пример #4
0
        private void ProcessEntity(string entitySetName, EntityMeta entityMeta)
        {
            string entityDirectory = string.Concat(outputDirectory, Path.DirectorySeparatorChar, entityMeta.Name);

            if (!Directory.Exists(entityDirectory))
            {
                Directory.CreateDirectory(entityDirectory);
            }

            GenerateListPage(entityMeta, entityDirectory);
            GenerateEditPage(entityMeta, entityDirectory);
            GenerateNavigation(entityMeta);
        }
Пример #5
0
        private StringBuilder GenerateDetailTabPage(EntityMeta entityMeta, EntityProp entityProp)
        {
            var result = new StringBuilder();

            result = result.AppendFormat("<dx:TabPage Text=\"{0}\" Visible=\"true\">{1}", entityProp.Name, Environment.NewLine);
            result = result.AppendFormat("    <ContentCollection>{0}", Environment.NewLine);
            result = result.AppendFormat("        <dx:ContentControl runat=\"server\">{0}", Environment.NewLine);
            result = result.Append(GenerateDetailGridView(entityMeta, entityProp));
            result = result.AppendFormat("        </dx:ContentControl>{0}", Environment.NewLine);
            result = result.AppendFormat("    </ContentCollection>{0}", Environment.NewLine);
            result = result.AppendFormat("</dx:TabPage>{0}", Environment.NewLine);

            return(result);
        }
Пример #6
0
        public void Inspect(Type entityType, string entitySetName)
        {
            var entityMeta = new EntityMeta();

            entityMeta.Name          = entityType.Name;
            entityMeta.Namespace     = entityType.Namespace;
            entityMeta.EntitySetName = entitySetName;
            entityMeta.IsScaffold    = !(entityType.GetCustomAttributes(typeof(ScaffoldTableAttribute), false).Where(p => (p as ScaffoldTableAttribute).Scaffold == false).Count() > 0);
            var customAttr = entityType.GetCustomAttributes(typeof(ScaffoldAttribute), false).Where(p => !string.IsNullOrEmpty((p as ScaffoldAttribute).Group)).SingleOrDefault();

            if (customAttr != null)
            {
                var scaffoldAttr = customAttr as ScaffoldAttribute;
                var group        = scaffoldAttr.Group.Split(new char[] { '|' });
                entityMeta.ScaffoldGroupTitle = group[0];
                if (group.Length > 1)
                {
                    entityMeta.ScaffoldGroupOrderNo = int.Parse(group[1]);
                }
                entityMeta.ScaffoldModuleOrderNo = scaffoldAttr.OrderNo;
                entityMeta.ScaffoldEditMode      = scaffoldAttr.EditMode;
            }
            else
            {
                if (entityMeta.IsScaffold)
                {
                    entityMeta.ScaffoldGroupTitle   = "Ungrouped";
                    entityMeta.ScaffoldGroupOrderNo = 999;
                }
            }

            entityMeta.EntityProps = new List <EntityProp>();
            foreach (var propertyInfo in entityType.GetProperties())
            {
                InspectProp(entityMeta, propertyInfo);
            }

            if (string.IsNullOrEmpty(entityMeta.LookUpDisplay))
            {
                entityMeta.LookUpDisplay = entityMeta.EntityProps.Where(p => p.Name == "Name").Select(p => p.Name).SingleOrDefault();
            }

            entitiesMeta[entityMeta.Name] = entityMeta;
        }
Пример #7
0
        private StringBuilder GenerateDetailDataSourceCodeBehind(EntityMeta entityMeta, EntityProp entityProp)
        {
            Console.WriteLine("INFO: Generating detail data sources code-behind for property {0}", entityProp.Name);
            var ds = new StringBuilder();

            ds = ds.Append(Environment.NewLine);
            ds = ds.AppendFormat("        protected void {0}Grid_DataSelect(object sender, EventArgs e){1}", entityProp.CollectionEntity.EntitySetName.ToCamelCase(), Environment.NewLine);
            ds = ds.Append("        {" + Environment.NewLine);
            ds = ds.AppendFormat("            Session[\"{0}{1}\"] = (sender as ASPxGridView).GetMasterRowKeyValue();{2}", entityMeta.Name, entityMeta.PrimaryKey, Environment.NewLine);
            ds = ds.Append("        }" + Environment.NewLine);

            ds = ds.Append(Environment.NewLine);
            ds = ds.AppendFormat("        protected void {0}Grid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e){1}", entityProp.CollectionEntity.EntitySetName.ToCamelCase(), Environment.NewLine);
            ds = ds.Append("        {" + Environment.NewLine);
            ds = ds.AppendFormat("            e.NewValues[\"{0}{1}\"] = (sender as ASPxGridView).GetMasterRowKeyValue();{2}", entityMeta.Name, entityMeta.PrimaryKey, Environment.NewLine);
            ds = ds.Append("        }" + Environment.NewLine);

            return(ds);
        }
Пример #8
0
        private StringBuilder GenerateGridColumns(EntityMeta entityMeta, EntityMeta parentEntity = null)
        {
            Console.WriteLine("INFO: Generating grid columns for entity {0}", entityMeta.Name);
            var cl = new StringBuilder();

            for (int i = 0; i < entityMeta.EntityProps.Count; i++)
            {
                var entityProp = entityMeta.EntityProps[i];
                if (entityProp.PropType != PropType.Unsupported && entityProp.IsScaffold)
                {
                    if (entityProp.IsForeignKey)
                    {
                        continue;
                    }
                    else if (entityProp.PropType == PropType.Text || entityProp.PropType == PropType.Number)
                    {
                        bool visible = !(parentEntity != null && entityProp.Name == string.Concat(parentEntity.Name, parentEntity.PrimaryKey));
                        cl = cl.AppendFormat("<dx:GridViewDataTextColumn FieldName=\"{0}\" VisibleIndex=\"{1}\" Visible=\"{2}\"></dx:GridViewDataTextColumn>{3}",
                                             entityProp.Name, i, visible, Environment.NewLine);
                    }
                    else if (entityProp.PropType == PropType.LookUp)
                    {
                        if (entityProp.LookUpEntity == null || (parentEntity != null && entityProp.LookUpEntity == parentEntity))
                        {
                            continue;
                        }

                        var textField = string.IsNullOrEmpty(entityProp.LookUpEntity.LookUpDisplay) ? entityProp.LookUpEntity.PrimaryKey : entityProp.LookUpEntity.LookUpDisplay;
                        cl = cl.AppendFormat("<dx:GridViewDataComboBoxColumn FieldName=\"{0}{1}\" VisibleIndex=\"{2}\">" + Environment.NewLine +
                                             "  <PropertiesComboBox DataSourceID=\"{3}DataSource\" TextField=\"{4}\" ValueField=\"{5}\"></PropertiesComboBox>" + Environment.NewLine +
                                             "</dx:GridViewDataComboBoxColumn>" + Environment.NewLine, entityProp.Name, entityProp.LookUpEntity.PrimaryKey, i,
                                             entityProp.LookUpEntity.EntitySetName, textField, entityProp.LookUpEntity.PrimaryKey);
                    }
                }
            }
            if (cl.Length > 0)
            {
                cl = cl.Replace(Environment.NewLine, "", cl.Length - Environment.NewLine.Length, Environment.NewLine.Length);
            }

            return(cl);
        }
Пример #9
0
        private StringBuilder GenerateDetailDataSource(EntityMeta entityMeta, EntityProp entityProp)
        {
            Console.WriteLine("INFO: Generating detail data source for entity {0}", entityProp.CollectionEntity.Name);
            StringBuilder result = new StringBuilder();

            result = result.AppendFormat("<ef:EntityDataSource ID=\"{0}DataSource\" runat=\"server\" ContextTypeName=\"{1}\" EntitySetName=\"{2}\" EnableInsert=\"true\" EnableUpdate=\"true\" EnableDelete=\"true\"{3}",
                                         entityProp.CollectionEntity.EntitySetName.ToCamelCase(), dbApp.GetType().FullName, entityProp.CollectionEntity.EntitySetName, Environment.NewLine);
            result = result.AppendFormat("    AutoGenerateWhereClause=\"True\">{0}", Environment.NewLine);
            result = result.AppendFormat("    <WhereParameters>{0}", Environment.NewLine);
            EntityProp primaryKeyProp = entityMeta.EntityProps.Where(p => p.IsPrimaryKey == true).SingleOrDefault();

            if (primaryKeyProp == null)
            {
                throw new ApplicationException(string.Format("{0} does not have primary key", entityMeta.Name));
            }
            result = result.AppendFormat("        <asp:SessionParameter SessionField=\"{0}{1}\" Name=\"{0}{1}\" DbType=\"Int64\"/>{2}",
                                         entityMeta.Name, entityMeta.PrimaryKey, Environment.NewLine);
            result = result.AppendFormat("    </WhereParameters>{0}", Environment.NewLine);
            result = result.AppendFormat("</ef:EntityDataSource>{0}", Environment.NewLine);

            return(result);
        }
Пример #10
0
        private StringBuilder GenerateDetailGridView(EntityMeta entityMeta, EntityProp entityProp)
        {
            Console.WriteLine("INFO: Generating child grid view for entity {0}", entityProp.CollectionEntity.Name);
            var result = new StringBuilder();

            if (entityProp.PropType != PropType.Collection)
            {
                Console.WriteLine("WARNING: {0} is not collection, skip it.", entityProp.Name);
                return(result);
            }
            result = result.Append(File.ReadAllText(string.Concat(templateDirectory, Path.DirectorySeparatorChar, "_detailGridView.aspx.txt")));
            var detailEntity = entityProp.CollectionEntity.EntitySetName.ToCamelCase();

            result = result.Replace("{detailGrid}", string.Concat(detailEntity, "Grid"));
            result = result.Replace("{detailGridDataSource}", string.Concat(detailEntity, "DataSource"));
            result = result.Replace("{detailKeyFieldName}", entityProp.CollectionEntity.PrimaryKey);
            result = result.Replace("{detailColumnList}", GenerateGridColumns(entityProp.CollectionEntity, entityMeta).ToString());

            ReplaceVariablesFromConfig(new StringBuilder[] { result });

            return(result);
        }
Пример #11
0
        private void InspectProp(EntityMeta entityMeta, PropertyInfo propertyInfo)
        {
            Console.WriteLine("INFO: Inspecting entity {0}, property {1}", entityMeta.Name, propertyInfo.Name);
            var entityProp = new EntityProp();

            entityProp.Name = propertyInfo.Name;
            ConvertPropertyType(entityProp, entityMeta, propertyInfo.PropertyType);
            entityProp.ClrPropType  = propertyInfo.PropertyType.Name;
            entityProp.IsPrimaryKey = propertyInfo.Name.ToLower() == "id" || propertyInfo.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0;

            if (entityProp.IsPrimaryKey)
            {
                entityMeta.PrimaryKey = entityProp.Name;
            }
            else if (propertyInfo.GetCustomAttributes(typeof(LookUpDisplayAttribute), false).Length > 0)
            {
                entityMeta.LookUpDisplay = propertyInfo.Name;
            }

            if (propertyInfo.GetCustomAttributes(typeof(ScaffoldColumnAttribute), false).Length > 0)
            {
                var attr = propertyInfo.GetCustomAttributes(typeof(ScaffoldColumnAttribute), false)[0];
                entityProp.IsScaffold = (attr as ScaffoldColumnAttribute).Scaffold;
            }

            entityProp.IsScaffold = propertyInfo.GetCustomAttributes(typeof(ScaffoldColumnAttribute), false).Where(p => (p as ScaffoldColumnAttribute).Scaffold == false).SingleOrDefault() == null;
            entityProp.IsScaffold = propertyInfo.GetCustomAttributes(typeof(NotMappedAttribute), false).SingleOrDefault() == null;

            if (entityProp.IsPrimaryKey)
            {
                entityMeta.EntityProps.Insert(0, entityProp);
            }
            else
            {
                entityMeta.EntityProps.Add(entityProp);
            }
        }
Пример #12
0
        private StringBuilder GenerateLookUpDataSourcesCodeBehind(EntityMeta entityMeta)
        {
            Console.WriteLine("INFO: Generating lookup data sources code-behind for entity {0}", entityMeta.Name);

            var ds = new StringBuilder();

            IList <EntityMeta> lookUps = entityMeta.EntityProps.Where(p => p.PropType == PropType.LookUp).Select(x => x.LookUpEntity).Distinct().ToList();

            foreach (var lookUp in lookUps)
            {
                ds = ds.AppendFormat("        /// <summary>" + Environment.NewLine +
                                     "        /// {0}DataSource control." + Environment.NewLine +
                                     "        /// </summary>" + Environment.NewLine +
                                     "        /// <remarks>" + Environment.NewLine +
                                     "        /// Auto-generated field." + Environment.NewLine +
                                     "        /// To modify move field declaration from designer file to code-behind file." + Environment.NewLine +
                                     "        /// </remarks>" + Environment.NewLine +
                                     "        protected global::Microsoft.AspNet.EntityDataSource.EntityDataSource {1}DataSource;" + Environment.NewLine +
                                     Environment.NewLine,
                                     lookUp.EntitySetName.ToCamelCase(), lookUp.EntitySetName.ToCamelCase());
            }

            return(ds);
        }
Пример #13
0
 private void GenerateEditPage(EntityMeta entityMeta, string entityDirectory)
 {
 }
Пример #14
0
        private void GenerateListPage(EntityMeta entityMeta, string entityDirectory)
        {
            Console.WriteLine("INFO: Generating List page for {0}", entityMeta.Name);

            string templateName = "List";

            if (entityMeta.ChildProps.Count == 1)
            {
                templateName = "MasterDetail";
            }
            else if (entityMeta.ChildProps.Count > 1)
            {
                templateName = "MultipleDetails";
            }

            var aspx     = new StringBuilder(File.ReadAllText(string.Concat(templateDirectory, Path.DirectorySeparatorChar, string.Concat(templateName, ".aspx.txt"))));
            var cs       = new StringBuilder(File.ReadAllText(string.Concat(templateDirectory, Path.DirectorySeparatorChar, string.Concat(templateName, ".aspx.cs.txt"))));
            var designer = new StringBuilder(File.ReadAllText(string.Concat(templateDirectory, Path.DirectorySeparatorChar, string.Concat(templateName, ".aspx.designer.cs.txt"))));

            // replace variables from app.config
            ReplaceVariablesFromConfig(new StringBuilder[] { aspx, cs, designer });

            aspx     = aspx.Replace("{entity}", entityMeta.Name);
            cs       = cs.Replace("{entity}", entityMeta.Name);
            designer = designer.Replace("{entity}", entityMeta.Name);

            aspx = aspx.Replace("{columnList}", GenerateGridColumns(entityMeta).ToString());
            if (entityMeta.ChildProps.Count == 1)
            {
                aspx     = aspx.Replace("{detailGrid}", GenerateDetailGridView(entityMeta, entityMeta.ChildProps[0]).ToString());
                aspx     = aspx.Replace("{detailDataSource}", GenerateDetailDataSource(entityMeta, entityMeta.ChildProps[0]).ToString());
                cs       = cs.Replace("{detailDataSource}", GenerateDetailDataSourceCodeBehind(entityMeta, entityMeta.ChildProps[0]).ToString());
                designer = designer.Replace("{detailDataSource}", GenerateDetailDataSourceDesigner(entityMeta.ChildProps[0]).ToString());
            }
            else if (entityMeta.ChildProps.Count > 1)
            {
                var childPages = new StringBuilder();
                foreach (var childProp in entityMeta.ChildProps)
                {
                    childPages = childPages.Append(GenerateDetailTabPage(entityMeta, childProp));
                }
                aspx = aspx.Replace("{tabPages}", childPages.ToString());

                var detailDataSources           = new StringBuilder();
                var detailDataSourcesCodeBehind = new StringBuilder();
                var detailDataSourcesDesigner   = new StringBuilder();
                foreach (var childProp in entityMeta.ChildProps)
                {
                    detailDataSources           = detailDataSources.Append(GenerateDetailDataSource(entityMeta, childProp));
                    detailDataSourcesCodeBehind = detailDataSourcesCodeBehind.Append(GenerateDetailDataSourceCodeBehind(entityMeta, childProp));
                    detailDataSourcesDesigner   = detailDataSourcesDesigner.Append(GenerateDetailDataSourceDesigner(childProp));
                }
                aspx     = aspx.Replace("{detailDataSources}", detailDataSources.ToString());
                cs       = cs.Replace("{detailDataSources}", detailDataSourcesCodeBehind.ToString());
                designer = designer.Replace("{detailDataSources}", detailDataSourcesDesigner.ToString());
            }

            aspx     = aspx.Replace("{lookUpDataSources}", GenerateLookUpDataSources(entityMeta).ToString());
            designer = designer.Replace("{lookUpDataSources}", GenerateLookUpDataSourcesCodeBehind(entityMeta).ToString());

            aspx = aspx.Replace("{keyFieldName}", entityMeta.PrimaryKey);
            aspx = aspx.Replace("{contextTypeName}", dbApp.GetType().FullName);
            aspx = aspx.Replace("{entitySetName}", entityMeta.EntitySetName);

            // write result
            File.WriteAllText(string.Concat(entityDirectory, Path.DirectorySeparatorChar, "List.aspx"), aspx.ToString());
            File.WriteAllText(string.Concat(entityDirectory, Path.DirectorySeparatorChar, "List.aspx.cs"), cs.ToString());
            File.WriteAllText(string.Concat(entityDirectory, Path.DirectorySeparatorChar, "List.aspx.designer.cs"), designer.ToString());
        }