private void GenerateEntity(CSharpConfig config, ItemGroup group, Item item) { string path = Path.Combine(config.EntrityPath, group.Name, "Auto"); string file = $"{item.Name}Entity.cs"; var nameSpace = new List<string> { "System", "XCommon.Patterns.Repository.Entity", "System.Runtime.Serialization" }; nameSpace.AddRange(item.Properties.Where(c => c.NameGroup != group.Name).Select(c => c.NameGroup)); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit($"{item.Name}Entity", "EntityBase", $"{config.EntrityNameSpace}.{group.Name}", ClassVisility.Public, true, nameSpace.ToArray()); foreach (var column in item.Properties) { builder .AppendLine($"public {column.Type} {column.Name} {{ get; set; }}") .AppendLine(); } builder .AppendLine() .AppendLine("[IgnoreDataMember]") .AppendLine("public override Guid Key") .AppendLine("{") .IncrementIndent() .AppendLine("get") .AppendLine("{") .IncrementIndent() .AppendLine($"return {item.NameKey};") .DecrementIndent() .AppendLine("}") .AppendLine("set") .AppendLine("{") .IncrementIndent() .AppendLine($"{item.NameKey} = value;") .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); }
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(); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage(); if (importItems.Any()) { bool 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(); string 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) { string 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); WriteFile(Config.Path.ToLower(), fileName, builder); } }
private void ProcessEnum() { StringBuilderIndented 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(); } WriteFile(Config.Path.ToLower(), "enum.ts", builder); }
private void GenerateContext(CSharpConfig config) { string path = config.DataBase.Path; string file = $"{config.DataBase.ContextName}.cs"; List<string> nameSpaces = new List<string> { "System", "Microsoft.EntityFrameworkCore", "Microsoft.EntityFrameworkCore.Infrastructure", "XCommon.Patterns.Ioc", "XCommon.Application" }; nameSpaces.AddRange(config.DataBaseItems.Select(c => $"{config.DataBase.NameSpace}.{c.Name}")); nameSpaces.AddRange(config.DataBaseItems.Select(c => $"{config.DataBase.NameSpace}.{c.Name}.Map")); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit(config.DataBase.ContextName, "DbContext", config.DataBase.NameSpace, ClassVisility.Public, nameSpaces.ToArray()) .AppendLine() .AppendLine("private IApplicationSettings AppSettings => Kernel.Resolve<IApplicationSettings>();") .AppendLine(); foreach (var item in config.DataBaseItems.SelectMany(c => c.Items)) { 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.DataBase.ContextName}\")") .AppendLine(".ConfigureWarnings(config => config.Ignore(InMemoryEventId.TransactionIgnoredWarning));") .DecrementIndent() .AppendLine() .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine($"options.UseSqlServer(AppSettings.ConnectionString);") .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.Items)) { builder.AppendLine($"{item.Name}Map.Map(modelBuilder, AppSettings.UnitTest);"); } } builder .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); }
private void GenerateEntityMap(CSharpConfig config) { foreach (var group in config.DataBaseItems) { foreach (var item in group.Items) { string path = Path.Combine(config.DataBase.Path, group.Name, "Map"); string file = $"{item.Name}Map.cs"; StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit($"{item.Name}Map", null, $"{config.DataBase.NameSpace}.{group.Name}.Map", ClassVisility.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.NameKey});") .AppendLine() .AppendLine("if (!unitTest)") .IncrementIndent() .AppendLine($"entity.ToTable(\"{item.Name}\", \"{group.Name}\");") .DecrementIndent() .AppendLine("else") .IncrementIndent() .AppendLine($"entity.ToTable(\"{group.Name}{item.Name}\");") .DecrementIndent() .AppendLine(); ProcessMapColumns(builder, item); ProcessMapRelationShips(config, builder, item); builder .DecrementIndent() .AppendLine("});") .DecrementIndent() .AppendLine("}") .ClassEnd(); WriteFile(path, file, builder); } } }
private void GenerateEntity(CSharpConfig config) { foreach (var group in config.DataBaseItems) { foreach (var item in group.Items) { string path = Path.Combine(config.DataBase.Path, group.Name); string file = $"{item.Name}.cs"; var nameSpace = new List<string> { "System", "System.Collections.Generic" }; nameSpace.AddRange(item.Relationships.Where(c => c.ItemGroupFK != group.Name).Select(c => $"{config.DataBase.NameSpace}.{c.ItemGroupFK}").Distinct()); nameSpace.AddRange(item.Relationships.Where(c => c.ItemGroupPK != group.Name).Select(c => $"{config.DataBase.NameSpace}.{c.ItemGroupPK}").Distinct()); nameSpace.AddRange(item.Properties.Where(c => c.NameGroup != group.Name).Select(c => c.NameGroup)); StringBuilderIndented builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit(item.Name, null, $"{config.DataBase.NameSpace}.{group.Name}", ClassVisility.Public, nameSpace.ToArray()); foreach (var property in item.Properties) { builder .AppendLine($"public {property.Type} {property.Name} {{ get; set; }}") .AppendLine(); } foreach (var relationShip in item.Relationships.Where(c => c.RelationshipType == ItemRelationshipType.Single)) { string relationShipName = ProcessRelationShipName(config, relationShip, relationShip.ItemPK); builder .AppendLine($"public virtual {relationShip.ItemPK} {relationShipName} {{ get; set; }}") .AppendLine(); } foreach (var relationShip in item.Relationships.Where(c => c.RelationshipType == ItemRelationshipType.Many)) { string relationShipName = ProcessRelationShipName(config, relationShip, relationShip.ItemFK); builder .AppendLine($"public virtual ICollection<{relationShip.ItemFK}> {relationShipName} {{ get; set; }}") .AppendLine(); } builder .ClassEnd(); WriteFile(path, file, builder); } } }