Exemplo n.º 1
0
        private void ProcessEnum()
        {
            var builder = new StringBuilderIndented();

            builder
            .GenerateFileMessage();

            foreach (var enumItem in TSEnums)
            {
                builder
                .AppendLine($"export enum {enumItem.Name} {{")
                .IncrementIndent();

                foreach (var values in enumItem.Values)
                {
                    builder
                    .AppendLine($"{values.Key} = {values.Value},");
                }

                builder
                .DecrementIndent()
                .AppendLine("}")
                .AppendLine();
            }

            Writer.WriteFile(Config.TypeScript.Entity.Path.ToLower(), "enum.ts", builder, true);
        }
        public void WriteEntity(DataBaseTable item)
        {
            var path = Path.Combine(Config.CSharp.EntityFramework.Path, item.Schema);
            var file = $"{item.Name}.cs";

            var nameSpace = new List <string> {
                "System", "System.Collections.Generic"
            };

            nameSpace.AddRange(item.RelationShips.Where(c => c.TableFK != item.Schema).Select(c => $"{Config.CSharp.EntityFramework.NameSpace}.{c.SchemaFK}").Distinct());
            nameSpace.AddRange(item.RelationShips.Where(c => c.TablePK != item.Schema).Select(c => $"{Config.CSharp.EntityFramework.NameSpace}.{c.SchemaPK}").Distinct());
            nameSpace.AddRange(item.Columns.Where(c => c.Schema != item.Schema).Select(c => c.Schema));
            nameSpace.AddRange(item.ProcessRemapSchema(Config));


            var itemNameSpace = $"{Config.CSharp.EntityFramework.NameSpace}.{item.Schema}";

            nameSpace.RemoveAll(c => c == itemNameSpace);

            var builder = new StringBuilderIndented();

            builder
            .GenerateFileMessage()
            .ClassInit(item.Name, null, itemNameSpace, ClassVisility.Public, nameSpace.ToArray());

            foreach (var property in item.Columns)
            {
                var propertyType = property.ProcessRemapProperty(Config);

                builder
                .AppendLine($"public {propertyType} {property.Name} {{ get; set; }}")
                .AppendLine();
            }

            foreach (var relationShip in item.RelationShips.Where(c => c.Type == DataBaseRelationShipType.Single))
            {
                var relationShipName = ProcessRelationShipName(relationShip, relationShip.TablePK);

                builder
                .AppendLine($"public virtual {relationShip.TablePK} {relationShipName} {{ get; set; }}")
                .AppendLine();
            }

            foreach (var relationShip in item.RelationShips.Where(c => c.Type == DataBaseRelationShipType.Many))
            {
                var relationShipName = ProcessRelationShipName(relationShip, relationShip.TableFK);

                builder
                .AppendLine($"public virtual ICollection<{relationShip.TableFK}> {relationShipName} {{ get; set; }}")
                .AppendLine();
            }

            builder
            .ClassEnd();

            Writer.WriteFile(path, file, builder, true);
        }
Exemplo n.º 3
0
        public void WriteEntity(DataBaseTable item)
        {
            var path = Path.Combine(Config.CSharp.Entity.Path, item.Schema, "Auto");
            var file = $"{item.Name}Entity.cs";

            var nameSpace = new List <string> {
                "System", "XCommon.Patterns.Repository.Entity", "System.Runtime.Serialization"
            };

            nameSpace.AddRange(item.Columns.Where(c => c.Schema != item.Schema).Select(c => c.Schema));
            nameSpace.AddRange(item.ProcessRemapSchema(Config));

            var builder = new StringBuilderIndented();

            builder
            .GenerateFileMessage()
            .ClassInit($"{item.Name}Entity", "EntityBase", $"{Config.CSharp.Entity.NameSpace}.{item.Schema}", ClassVisility.Public, true, nameSpace.ToArray());

            foreach (var column in item.Columns)
            {
                var propertyType = column.ProcessRemapProperty(Config);

                builder
                .AppendLine($"public {propertyType} {column.Name} {{ get; set; }}")
                .AppendLine();
            }

            builder
            .AppendLine()
            .AppendLine("[IgnoreDataMember]")
            .AppendLine("public override Guid Key")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine("get")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine($"return {item.PKName};")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine("set")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine($"{item.PKName} = value;")
            .DecrementIndent()
            .AppendLine("}")
            .DecrementIndent()
            .AppendLine("}")
            .ClassEnd();

            Writer.WriteFile(path, file, builder, true);
        }
        public void WriteEntityMap(DataBaseTable item)
        {
            if (Config.CSharp.UsingApplicationBase && Config.CSharp.ApplicationClasses.Any(c => c.Name == item.Name && c.Schema == item.Schema))
            {
                return;
            }

            var path = Path.Combine(Config.CSharp.EntityFramework.Path, item.Schema, "Map");
            var file = $"{item.Name}Map.cs";

            var builder = new StringBuilderIndented();

            builder
            .GenerateFileMessage()
            .ClassInit($"{item.Name}Map", null, $"{Config.CSharp.EntityFramework.NameSpace}.{item.Schema}.Map", ClassVisibility.Internal, "System", "Microsoft.EntityFrameworkCore")
            .AppendLine("internal static void Map(ModelBuilder modelBuilder, bool unitTest)")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine($"modelBuilder.Entity<{item.Name}>(entity =>")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine($"entity.HasKey(e => e.{item.PKName});")
            .AppendLine()
            .AppendLine("if (!unitTest)")
            .IncrementIndent()
            .AppendLine($"entity.ToTable(\"{item.Name}\", \"{item.Schema}\");")
            .DecrementIndent()
            .AppendLine("else")
            .IncrementIndent()
            .AppendLine($"entity.ToTable(\"{item.Schema}{item.Name}\");")
            .DecrementIndent()
            .AppendLine();

            ProcessMapColumns(builder, item);
            ProcessMapRelationShips(builder, item);

            builder
            .DecrementIndent()
            .AppendLine("});")
            .DecrementIndent()
            .AppendLine("}")
            .ClassEnd();

            Writer.WriteFile(path, file, builder, true);
        }
Exemplo n.º 5
0
        private void ProcessTypes()
        {
            foreach (var file in TSClass.Select(c => c.FileName).Distinct().OrderBy(c => c))
            {
                var fileName    = file.GetSelector();
                var importItems = TSClass.Where(c => c.FileName == file).SelectMany(c => c.Imports).ToList();

                var builder = new StringBuilderIndented();

                builder
                .GenerateFileMessage();

                if (importItems.Any())
                {
                    var imported = false;

                    foreach (var importFile in importItems.Where(c => c.File != file).Select(c => c.File).Distinct())
                    {
                        imported = true;

                        var classes = importItems.Where(c => c.File == importFile).Select(c => c.Class).Distinct().ToArray();
                        var import  = string.Join(", ", classes);

                        builder
                        .AppendLine($"import {{ {import} }} from \"./{importFile.Replace(".ts", string.Empty)}\";");
                    }

                    if (imported)
                    {
                        builder
                        .AppendLine();
                    }
                }

                foreach (var className in TSClass.Where(c => c.FileName == file).Select(c => c.Class).Distinct().OrderBy(c => c))
                {
                    var tsClass = TSClass
                                  .Where(c => c.FileName == file && c.Class == className)
                                  .FirstOrDefault();

                    var generics = tsClass.Properties
                                   .Where(c => c.Generic)
                                   .Select(c => c.Type)
                                   .ToList();

                    var classNamePrint = tsClass.Class;

                    if (generics.Count > 0)
                    {
                        classNamePrint  = classNamePrint.Substring(0, classNamePrint.IndexOf('`'));
                        classNamePrint += "<";
                        classNamePrint += string.Join(", ", generics);
                        classNamePrint += ">";
                    }

                    builder
                    .AppendLine($"export interface I{classNamePrint} {{")
                    .IncrementIndent();

                    foreach (var property in tsClass.Properties)
                    {
                        var nullable = property.Nullable ? "?" : "";

                        builder
                        .AppendLine($"{property.Name}{nullable}: {property.Type}; ");
                    }

                    builder
                    .DecrementIndent()
                    .AppendLine("}")
                    .AppendLine();
                }

                if (GeneratedEntities.Contains(fileName))
                {
                    throw new Exception($"Two file with same name cannot be generated: {fileName}");
                }

                GeneratedEntities.Add(fileName);
                Writer.WriteFile(Config.TypeScript.Entity.Path.ToLower(), fileName, builder, true);
            }
        }
        public void WriteContext()
        {
            CleanFolder();

            var path = Config.CSharp.EntityFramework.Path;
            var file = $"{Config.CSharp.EntityFramework.ContextName}.cs";

            var nameSpaces = new List <string> {
                "System", "Microsoft.EntityFrameworkCore", "Microsoft.EntityFrameworkCore.Diagnostics", "XCommon.Patterns.Ioc", "XCommon.Application"
            };

            nameSpaces.AddRange(Config.DataBaseItems.Select(c => $"{Config.CSharp.EntityFramework.NameSpace}.{c.Name}"));
            nameSpaces.AddRange(Config.DataBaseItems.Select(c => $"{Config.CSharp.EntityFramework.NameSpace}.{c.Name}.Map"));

            var builder = new StringBuilderIndented();

            builder
            .GenerateFileMessage()
            .ClassInit(Config.CSharp.EntityFramework.ContextName, "DbContext", Config.CSharp.EntityFramework.NameSpace, ClassVisility.Public, nameSpaces.ToArray())
            .AppendLine()
            .AppendLine("private IApplicationSettings AppSettings => Kernel.Resolve<IApplicationSettings>();")
            .AppendLine();

            foreach (var item in Config.DataBaseItems.SelectMany(c => c.Tables))
            {
                builder
                .AppendLine($"public DbSet<{item.Name}> {item.Name} {{ get; set; }}")
                .AppendLine();
            }

            builder
            .AppendLine("protected override void OnConfiguring(DbContextOptionsBuilder options)")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine("if (AppSettings.UnitTest)")
            .AppendLine("{")
            .IncrementIndent()
            .AppendLine($"options")
            .IncrementIndent()
            .AppendLine($".UseInMemoryDatabase(\"{Config.CSharp.EntityFramework.ContextName}\")")
            .AppendLine(".ConfigureWarnings(config => config.Ignore(InMemoryEventId.TransactionIgnoredWarning));")
            .DecrementIndent()
            .AppendLine()
            .AppendLine("return;")
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine()
            .AppendLine($"options.UseSqlServer(AppSettings.DataBaseConnectionString);")
            .AppendLine()
            .DecrementIndent()
            .AppendLine("}")
            .AppendLine()
            .AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)")
            .AppendLine("{");

            using (builder.Indent())
            {
                builder
                .AppendLine();

                foreach (var item in Config.DataBaseItems.SelectMany(c => c.Tables))
                {
                    builder.AppendLine($"{item.Name}Map.Map(modelBuilder, AppSettings.UnitTest);");
                }
            }

            builder
            .AppendLine("}")
            .ClassEnd();

            Writer.WriteFile(path, file, builder, true);
        }