Example #1
0
        private void BuildModelFiles(ErdCanvasModel[] canvases, Dispatcher dispatcher)
        {
            string basePath = Path.Combine(EntityModelScript.Setup.ClientModelOutputDirectory, "Base");

            string userPath = Path.Combine(EntityModelScript.Setup.ClientModelOutputDirectory, "User");

            Paths.CreateDirectory(basePath);

            Paths.CreateDirectory(userPath);

            foreach (ErdCanvasModel canvas in canvases)
            {
                foreach (TableModel table in canvas.SegmentTables)
                {
                    dispatcher.Invoke(() => { EventParser.ParseMessage(this, new ParseMessageEventArguments {
                            Message = string.Format("Building Model {0}", table.TableName)
                        }); });

                    string modelBase = EntityModelScript.ScriptServerModelBase(table);

                    string className = EntityModelScript.GetClassName(table);

                    File.WriteAllText(Path.Combine(basePath, $"{className}_Base.cs"), modelBase);

                    string userFileName = Path.Combine(userPath, $"{className}.cs");

                    if (!File.Exists(userFileName))
                    {
                        string model = EntityModelScript.ScriptServerModel(table);

                        File.WriteAllText(userFileName, model);
                    }
                }
            }
        }
Example #2
0
        private void BuildRepositoryBase(string repositoryName, string dataContextName, string basePath, TableModel[] tables, IncludeTableModel[] includeTables)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(EntityModelScript.Setup.RepositoryUsing);

            result.AppendLine();

            result.AppendLine($"namespace {EntityModelScript.Setup.RepositoryNamespace}");
            result.AppendLine("{");

            result.AppendLine($"    public abstract class {repositoryName}Repository_Base");
            result.AppendLine("    {");
            result.AppendLine($"        public {dataContextName} dataContext;");

            result.AppendLine();
            result.AppendLine($"        public {repositoryName}Repository_Base()");
            result.AppendLine("        {");
            result.AppendLine($"            this.dataContext = new {dataContextName}();");
            result.AppendLine("        }");

            foreach (TableModel table in tables)
            {
                string tableClassName = EntityModelScript.GetClassName(table);

                StringBuilder argumentsString = new StringBuilder();

                StringBuilder linqString = new StringBuilder();

                foreach (ColumnObjectModel keyColumn in table.Columns.Where(pk => pk.InPrimaryKey))
                {
                    string[] columnValues = EntityModelScript.GetColumnDotNetDescriptor(keyColumn);

                    argumentsString.Append($"{columnValues[0]} {columnValues[1]}, ");

                    linqString.Append($"pk.{columnValues[1]} == {columnValues[1]} && ");
                }

                if (linqString.Length > 4 && argumentsString.Length > 2)
                {
                    result.AppendLine();
                    result.AppendLine($"        public {tableClassName} Get{tableClassName}({(argumentsString.ParseToString().Substring(0, (argumentsString.Length - 2)))})");
                    result.AppendLine("        {");
                    result.AppendLine($"            return this.dataContext.{tableClassName}.FirstOrDefault(pk => {linqString.ParseToString().Substring(0, (linqString.Length - 4))});");
                    result.AppendLine("        }");
                }
            }

            foreach (IncludeTableModel table in includeTables)
            {
                string tableClassName = EntityModelScript.GetClassName(table);

                StringBuilder argumentsString = new StringBuilder();

                StringBuilder linqString = new StringBuilder();

                List <ColumnObjectModel> tableColumns = Integrity.GetObjectModel(table.TableName);

                if (tableColumns == null)
                {
                    continue;
                }

                foreach (ColumnObjectModel keyColumn in tableColumns.Where(pk => pk.InPrimaryKey))
                {
                    string[] columnValues = EntityModelScript.GetColumnDotNetDescriptor(keyColumn);

                    argumentsString.Append($"{columnValues[0]} {columnValues[1]}, ");

                    linqString.Append($"pk.{columnValues[1]} == {columnValues[1]} && ");
                }

                if (linqString.Length > 4 && argumentsString.Length > 2)
                {
                    result.AppendLine();
                    result.AppendLine($"        public {tableClassName} Get{tableClassName}({(argumentsString.ParseToString().Substring(0, (argumentsString.Length - 2)))})");
                    result.AppendLine("        {");
                    result.AppendLine($"            return this.dataContext.{tableClassName}.FirstOrDefault(pk => {linqString.ParseToString().Substring(0, (linqString.Length - 4))});");
                    result.AppendLine("        }");
                }
            }

            result.AppendLine("    }");
            result.AppendLine("}");

            File.WriteAllText(Path.Combine(basePath, $"{repositoryName}Repository_Base.cs"), result.ToString());
        }
Example #3
0
        private void BuildContextFile(string fileName, TableModel[] tablesUsed, IncludeTableModel[] includeTables)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(EntityModelScript.Setup.DataContextUsing);

            result.AppendLine();

            result.AppendLine($"namespace {EntityModelScript.Setup.DataContextNamespace}");

            result.AppendLine("{");

            result.AppendLine($"    public class {fileName} : DbContext");
            result.AppendLine("    {");

            string getset = "{ get; set; }";

            List <string> tablesUsedList = new List <string>();

            foreach (TableModel table in tablesUsed)
            {
                string nameToUse = EntityModelScript.Setup.UseFriendlyNames ? table.FriendlyName.MakeAlphaNumeric() : table.TableName;

                result.AppendLine($"        public DbSet<{EntityModelScript.GetClassName(table)}> {nameToUse} {getset}");

                tablesUsedList.Add(table.TableName);
            }

            foreach (IncludeTableModel included in includeTables)
            {
                if (tablesUsedList.Contains(included.TableName))
                {
                    continue;
                }

                string nameToUse = EntityModelScript.Setup.UseFriendlyNames ? included.FriendlyName.MakeAlphaNumeric() : included.TableName;

                result.AppendLine($"        public DbSet<{EntityModelScript.GetClassName(included)}> {nameToUse} {getset}");
            }

            result.AppendLine();
            result.AppendLine($"        {string.Format(EntityModelScript.Setup.DataContextConstructor, fileName)}");
            result.AppendLine("        {");
            result.AppendLine("        }");
            result.AppendLine();

            result.AppendLine("        protected override void OnModelCreating(DbModelBuilder modelBuilder)");
            result.AppendLine("        {");

            foreach (TableModel table in tablesUsed)
            {
                result.AppendLine($"            modelBuilder.Configurations.Add(new {EntityModelScript.GetClassName(table)}Mapping());");
            }

            foreach (IncludeTableModel included in includeTables)
            {
                if (tablesUsedList.Contains(included.TableName))
                {
                    continue;
                }

                result.AppendLine($"            modelBuilder.Configurations.Add(new {EntityModelScript.GetClassName(included)}Mapping());");
            }

            result.AppendLine("        }");

            result.AppendLine("    }");
            result.AppendLine("}");

            Paths.CreateDirectory(Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "DataContext"));

            string outputPath = Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "DataContext", $"{fileName}.cs");

            File.WriteAllText(outputPath, result.ToString());
        }
Example #4
0
        private void RunMappingScriptor(ErdCanvasModel[] canvases, Dispatcher dispatcher)
        {
            try
            {
                string dispatchNote = "Build Mappings Script for: {0}";

                Paths.CreateDirectory(Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "Mappings"));

                foreach (ErdCanvasModel canvas in canvases)
                {
                    foreach (TableModel table in canvas.SegmentTables)
                    {
                        dispatcher.Invoke(() => { EventParser.ParseMessage(this, new ParseMessageEventArguments {
                                Message = string.Format(dispatchNote, table.TableName)
                            }); });

                        string mapptingPath = Path.Combine(EntityModelScript.Setup.ServerOutputBaseDirectory, "Mappings", $"{EntityModelScript.GetClassName(table)}Mapping.cs");

                        string mappingResult = EntityModelScript.ScriptServerModelMapping(table);

                        File.WriteAllText(mapptingPath, mappingResult);
                    }
                }
            }
            catch (Exception err)
            {
                throw;
            }
        }
        public static string ScriptServerModelMapping(TableModel table)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(Setup.MappingClassUsing);

            result.AppendLine();

            result.AppendLine($"namespace {Setup.MappingClassNamespace}");

            result.AppendLine("{");

            result.AppendLine($"    public class {EntityModelScript.GetClassName(table)}Mapping : EntityTypeConfiguration<{EntityModelScript.GetClassName(table)}>");
            result.AppendLine("    {");

            result.AppendLine($"        public {EntityModelScript.GetClassName(table)}Mapping()");
            result.AppendLine("        {");

            result.AppendLine($"            ToTable(\"{table.TableName}\");");
            result.AppendLine();

            ColumnObjectModel[] pkColumns = table.Columns.Where(pk => pk.InPrimaryKey).ToArray();

            if (pkColumns.HasElements() && pkColumns.Length == 1)
            {
                result.AppendLine($"            HasKey(k => k.{EntityModelScript.GetColumnName(pkColumns[0])});");
            }
            else if (pkColumns.HasElements())
            {
                result.Append("            HasKey(k => new { ");

                for (int x = 0; x < pkColumns.Length; ++x)
                {
                    if (x == (pkColumns.Length - 1))
                    {
                        result.Append($"k.{EntityModelScript.GetColumnName(pkColumns[x])} ");
                    }
                    else
                    {
                        result.Append($"k.{EntityModelScript.GetColumnName(pkColumns[x])}, ");
                    }
                }

                result.AppendLine(" });");
            }

            result.AppendLine();

            for (int x = 0; x < table.Columns.Length; x++)
            {
                ColumnObjectModel column = table.Columns[x];

                string lambda = $"col{x}";

                result.AppendLine($"            Property({lambda} => {lambda}.{EntityModelScript.GetColumnName(column)}).HasColumnName(\"{column.ColumnName}\");");
            }

            result.AppendLine("        }");

            result.AppendLine("    }");

            result.Append("}");

            return(result.ToString());
        }
 private static string GetModelClassString(TableModel table)
 {
     return(string.Format(EntityModelScript.Setup.ModelClassString, EntityModelScript.GetClassName(table)));
 }