private void GenerateFactoryQuery(StringBuilderIndented builder) { builder .AppendLine("private static void RegisterQuery()") .AppendLine("{") .IncrementIndent(); foreach (var table in Config.DataBaseItems.SelectMany(c => c.Tables)) { builder .AppendLine($"Kernel.Map<ISpecificationQuery<{table.Name}, {table.Name}Filter>>().To<{table.Name}Query>();"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); }
public void WriteQuery(DataBaseTable item) { var path = Path.Combine(Config.CSharp.Repository.Concrecte.Path, item.Schema, "Query"); var file = $"{item.Name}Query.cs"; var nameSpace = new List <string> { "System", "System.Linq", "System.Collections.Generic", "XCommon.Patterns.Specification.Query", "XCommon.Patterns.Specification.Query.Extensions", "XCommon.Extensions.String", "XCommon.Extensions.Checks" }; if (Config.CSharp.EntityFramework != null) { nameSpace.Add($"{Config.CSharp.EntityFramework.NameSpace}.{item.Schema}"); } nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}.Filter"); var builder = new StringBuilderIndented(); var columnOrder = item.Columns.Any(c => c.Name == "Name") ? "Name" : item.PKName; builder .ClassInit($"{item.Name}Query", $"SpecificationQuery<{item.Name}, {item.Name}Filter>", $"{Config.CSharp.Repository.Concrecte.NameSpace}.{item.Schema}.Query", ClassVisility.Public, nameSpace.ToArray()) .AppendLine($"public override IQueryable<{item.Name}> Build(IQueryable<{item.Name}> source, {item.Name}Filter filter)") .AppendLine("{") .IncrementIndent() .AppendLine($"var spefications = NewSpecificationList()") .IncrementIndent() .AppendLine($".And(e => e.{item.PKName} == filter.Key, f => f.Key.HasValue)") .AppendLine($".And(e => filter.Keys.Contains(e.{item.PKName}), f => f.Keys.IsValidList())") .AppendLine($".OrderBy(e => e.{columnOrder})") .AppendLine(".Take(filter.PageNumber, filter.PageSize);") .DecrementIndent() .AppendLine() .AppendLine("return CheckSpecifications(spefications, source, filter);") .DecrementIndent() .AppendLine("}") .ClassEnd(); Writer.WriteFile(path, file, builder, false); }
public void WriteContract(DataBaseTable item) { var path = Path.Combine(Config.CSharp.Repository.Contract.Path, item.Schema); var file = $"I{item.Name}Business.cs"; var nameSpace = new List <string> { "System", "XCommon.Patterns.Repository" }; nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}"); nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}.Filter"); var builder = new StringBuilderIndented(); builder .InterfaceInit($"I{item.Name}Business", $"IRepositoryEF<{item.Name}Entity, {item.Name}Filter>", $"{Config.CSharp.Repository.Contract.NameSpace}.{item.Schema}", ClassVisility.Public, nameSpace.ToArray()) .InterfaceEnd(); Writer.WriteFile(path, file, builder, false); }
public void WriteConcrete(DataBaseTable item) { var path = Path.Combine(Config.CSharp.Repository.Concrecte.Path, item.Schema); var file = $"{item.Name}Business.cs"; var parentClass = $"RepositoryEFBase<{item.Name}Entity, {item.Name}Filter, {item.Name}, {Config.CSharp.EntityFramework.ContextName}>"; var nameSpace = new List <string> { "System", " XCommon.Patterns.Repository" }; nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}"); nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}.Filter"); nameSpace.Add($"{Config.CSharp.EntityFramework.NameSpace}"); nameSpace.Add($"{Config.CSharp.EntityFramework.NameSpace}.{item.Schema}"); if (Config.CSharp.Repository.Contract != null && Config.CSharp.Repository.Contract.Execute) { nameSpace.Add($"{Config.CSharp.Repository.Contract.NameSpace}.{item.Schema}"); parentClass = $"RepositoryEFBase<{item.Name}Entity, {item.Name}Filter, {item.Name}, {Config.CSharp.EntityFramework.ContextName}>, I{item.Name}Business"; } var appClass = Config.CSharp.ApplicationClasses.FirstOrDefault(c => c.Name == item.Name && c.Schema == item.Schema); if (Config.CSharp.UsingApplicationBase && appClass != null) { nameSpace = new List <string> { $"{Config.CSharp.Repository.Contract.NameSpace}.{item.Schema}" }; parentClass = $"{appClass.NamespaceBusiness}.{item.Name}Business, I{item.Name}Business"; } var builder = new StringBuilderIndented(); builder .ClassInit($"{item.Name}Business", parentClass, $"{Config.CSharp.Repository.Concrecte.NameSpace}.{item.Schema}", ClassVisibility.Public, nameSpace.ToArray()) .ClassEnd(); Writer.WriteFile(path, file, builder, false); }
public void WriteEntityMap(DataBaseTable item) { 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", 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.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); }
private void ProcessMapRelationShips(StringBuilderIndented builder, DataBaseTable item) { foreach (var relationShip in item.RelationShips.Where(c => c.Type == DataBaseRelationShipType.Single)) { var relationShipNamePK = ProcessRelationShipName(relationShip, relationShip.TablePK); var relationShipNameFK = ProcessRelationShipName(relationShip, relationShip.TableFK); if (Config.CSharp.UsingApplicationBase && Config.CSharp.ApplicationClasses.Any(c => c.Name == relationShipNamePK)) { return; } builder .AppendLine("entity") .IncrementIndent() .AppendLine($".HasOne(d => d.{relationShipNamePK})") .AppendLine($".WithMany(p => p.{relationShipNameFK})") .AppendLine($".HasForeignKey(d => d.{relationShip.ColumnFK});") .DecrementIndent() .AppendLine(); } }
public void Run() { var builder = new StringBuilderIndented(); builder .AppendLine("import { Injectable } from '@angular/core';"); if (Config.TypeScript.Resource.LazyLoad) { builder .AppendLine("import { Http, Response } from '@angular/http';") .AppendLine("import { Observable } from 'rxjs/Observable';"); } builder .AppendLine($"import {{ Map }} from '{Config.TypeScript.Resource.EntityPath}';") .AppendLine(); Resources = new List <GeneratorResourceEntity>(); GetResouces(); BuildLanguageSuported(builder); BuildInterface(builder); if (!Config.TypeScript.Resource.LazyLoad) { BuildClass(builder); } BuidService(builder); var file = Config.TypeScript.Resource.File.GetSelector() + ".service.ts"; var path = Config.TypeScript.Resource.Path.ToLower(); Writer.WriteFile(path, file, builder, true); Index.Run(path); }
private void BuildLazyService(StringBuilderIndented builder) { builder .AppendLine("if (this.Resources[this.Language.Name]) {") .IncrementIndent() .AppendLine("let resource: IResource = this.Resources[this.Language.Name];") .AppendLine(); foreach (var resource in Resources) { builder .AppendLine($"this.{resource.ResourceName} = resource.{resource.ResourceName};"); } builder .AppendLine() .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine($"this.http.get(`{Config.TypeScript.Resource.RequestAddress}/${{this.Language.Name}}`)") .IncrementIndent() .AppendLine(".map((response: Response, index: number) => response.json())") .AppendLine(".subscribe((resource: IResource) => {") .IncrementIndent() .AppendLine("this.Resources[resource.Culture] = resource;") .AppendLine(); foreach (var resource in Resources) { builder .AppendLine($"this.{resource.ResourceName} = resource.{resource.ResourceName};"); } builder .DecrementIndent() .AppendLine("});"); }
private void BuildClass(StringBuilderIndented builder) { foreach (var culture in Config.TypeScript.Resource.Cultures) { foreach (var resource in Resources) { builder .AppendLine($"class {resource.ResourceName}{GetCultureName(culture.Name)} implements I{resource.ResourceName} {{") .IncrementIndent(); foreach (var property in resource.Values.Where(c => c.Culture == culture.Name).SelectMany(c => c.Properties)) { builder .AppendLine($"{property.Key}: string = '{property.Value}';"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); } } }
public void Run(string path, List <string> services) { path = path.ToLower(); foreach (var service in services) { var file = $"{service.GetSelector()}.service.ts".ToLower(); var builder = new StringBuilderIndented(); builder .AppendLine("import { Injectable } from '@angular/core'; ") .AppendLine("import { Http, Response } from '@angular/http'; ") .AppendLine() .AppendLine("@Injectable()") .AppendLine($"export class {service.GetName()}Service {{") .IncrementIndent() .AppendLine("constructor(private http: Http) { }") .DecrementIndent() .AppendLine("}"); Writer.WriteFile(path, file, builder, false); } }
private void Html(string path, string selector, bool outlet) { var file = $"{selector}.html"; var builder = new StringBuilderIndented(); builder .AppendLine($"<div class=\"{selector}\">") .IncrementIndent() .AppendLine($"<h1>Hey! I\"m {selector}</h1>"); if (outlet) { builder .AppendLine("<router-outlet></router-outlet>"); } builder .DecrementIndent() .AppendLine("</div>"); Writer.WriteFile(path, file, builder, false); }
public void WriteFactoryCustom() { var path = Path.Combine(Config.CSharp.Factory.Path); var file = $"Register.cs"; if (File.Exists(Path.Combine(path, file))) { return; } var nameSpace = new List<string> { "XCommon.Patterns.Ioc" }; var builder = new StringBuilderIndented(); builder .ClassInit("Register", null, $"{Config.CSharp.Factory.NameSpace}", ClassVisibility.Public, true, nameSpace.ToArray()) .AppendLine("public static void RegisterCustom(bool production, bool unitTest)") .AppendLine("{") .AppendLine("}") .ClassEnd(); Writer.WriteFile(path, file, builder, false); }
private void ProcessMapColumns(StringBuilderIndented builder, DataBaseTable item) { foreach (var property in item.Columns) { builder .Append($"entity.Property(e => e.{property.Name})"); using (builder.Indent()) { if (!property.Nullable) { builder .AppendLine() .Append(".IsRequired()"); } if (property.Type == "string" && property.Size.IsNotEmpty() && property.Size != "MAX") { builder .AppendLine() .Append($".HasMaxLength({property.Size})"); } if (property.PK) { builder .AppendLine() .Append(".ValueGeneratedNever()"); } } builder .Append(";") .AppendLine() .AppendLine(); } }
private void BuildInterface(StringBuilderIndented builder) { foreach (var resource in Resources) { builder .AppendLine($"export interface I{resource.ResourceName} {{") .IncrementIndent(); foreach (var value in resource.Values.FirstOrDefault().Properties) { builder .AppendLine($"{value.Key}: string;"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); } builder .AppendLine("export interface IResource {") .IncrementIndent() .AppendLine("Culture: string;"); foreach (var resource in Resources) { builder .AppendLine($"{resource.ResourceName}: I{resource.ResourceName};"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); }
public void WriteFactory() { var path = Path.Combine(Config.CSharp.Factory.Path); var file = $"Register.Auto.cs"; var nameSpace = new List<string> { "XCommon.Patterns.Ioc", "XCommon.Patterns.Specification.Validation", "XCommon.Patterns.Specification.Query" }; foreach (var schema in Config.DataBaseItems) { if (Config.CSharp.Repository.Contract != null && Config.CSharp.Repository.Contract.Execute) { nameSpace.Add($"{Config.CSharp.Repository.Contract.NameSpace}.{schema.Name}"); } nameSpace.Add($"{Config.CSharp.Repository.Concrecte.NameSpace}.{schema.Name}"); nameSpace.Add($"{Config.CSharp.Repository.Concrecte.NameSpace}.{schema.Name}.Query"); nameSpace.Add($"{Config.CSharp.Repository.Concrecte.NameSpace}.{schema.Name}.Validate"); nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{schema.Name}"); nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{schema.Name}.Filter"); nameSpace.Add($"{Config.CSharp.EntityFramework.NameSpace}.{schema.Name}"); }; if (Config.CSharp.UsingApplicationBase) { nameSpace.AddRange(Config.CSharp.ApplicationClasses.Select(c => c.NamespaceContext).Distinct()); } var builder = new StringBuilderIndented(); builder .ClassInit("Register", null, $"{Config.CSharp.Factory.NameSpace}", ClassVisibility.Public, true, nameSpace.ToArray()) .AppendLine("public static void Do(bool production, bool unitTest)") .AppendLine("{") .IncrementIndent(); if (Config.CSharp.Repository.Contract != null && Config.CSharp.Repository.Contract.Execute) { builder .AppendLine("RegisterRepository();"); } builder .AppendLine("RegisterValidate();") .AppendLine("RegisterQuery();") .AppendLine("RegisterCustom(production, unitTest);") .DecrementIndent() .AppendLine("}") .AppendLine(); if (Config.CSharp.Repository.Contract != null && Config.CSharp.Repository.Contract.Execute) { GenerateFactoryRepository(builder); } GenerateFactoryQuery(builder); GenerateFactoryValidate(builder); builder .ClassEnd(); Writer.WriteFile(path, file, builder, true); }
private string BuidService(StringBuilderIndented builder) { builder .AppendLine("@Injectable()") .AppendLine("export class ResourceService {") .IncrementIndent() .AppendLine(Config.TypeScript.Resource.LazyLoad ? "constructor(private http: Http) {" : "constructor() {") .IncrementIndent(); BuildLanguageSuportedContructor(builder); builder .AppendLine($"this.SetLanguage('{Config.TypeScript.Resource.CultureDefault.Name}')") .DecrementIndent() .AppendLine("}") .AppendLine(); if (Config.TypeScript.Resource.LazyLoad) { builder .AppendLine("private Resources: Map<IResource> = {};"); } builder .AppendLine("public Languages: ApplicationCulture[] = [];") .AppendLine("private Language: ApplicationCulture;") .AppendLine(); foreach (var resource in Resources) { builder.AppendLine($"public {resource.ResourceName}: I{resource.ResourceName};"); } builder .AppendLine() .AppendLine("public GetLanguage(): ApplicationCulture {") .IncrementIndent() .AppendLine("return this.Language;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("public SetLanguage(language: string): void {") .IncrementIndent() .AppendLine() .AppendLine("if (this.Language && this.Language.Name === language) {") .IncrementIndent() .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("let value = this.Languages.find((item: ApplicationCulture) => item.Name == language);") .AppendLine() .AppendLine("if (!value) {") .IncrementIndent() .AppendLine($"console.warn(`Unknown language: ${{language}}! Set up current culture as the default language: {Config.TypeScript.Resource.CultureDefault.Name}`);") .AppendLine($"this.SetLanguage('{Config.TypeScript.Resource.CultureDefault.Name}');") .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("this.Language = value;") .AppendLine(); if (!Config.TypeScript.Resource.LazyLoad) { BuildNormalService(builder); } if (Config.TypeScript.Resource.LazyLoad) { BuildLazyService(builder); } builder .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}"); return(builder.ToString()); }
private string WriteService(StringBuilderIndented builder) { // Service declaration builder .AppendLine("@Injectable()") .AppendLine($"export class {Config.TypeScript.Resource.ServiceName}Service {{") .IncrementIndent() .AppendLine(""); var visibility = "public "; var setter = " = "; var finalizer = ";"; var resources = Resources[Config.TypeScript.Resource.CultureDefault.Code]; var count = 0; if (Config.TypeScript.Resource.Extractor) { visibility = ""; setter = ": "; finalizer = ","; builder .AppendLine("public XCommon = {") .IncrementIndent(); } // Resource Properties foreach (var resource in resources) { count++; if (resources.Count == count) { finalizer = ""; } builder .AppendLine($"{visibility}{resource.ResourceName}{setter}new {resource.ResourceName}Resource(){finalizer}"); } if (Config.TypeScript.Resource.Extractor) { builder .DecrementIndent() .AppendLine("}") .AppendLine(); } // Current Culture builder .AppendLine("public CurrentCulture = {") .IncrementIndent() .AppendLine($"Code: '{Config.TypeScript.Resource.CultureDefault.Code}',") .AppendLine($"Name: '{Config.TypeScript.Resource.CultureDefault.Name}'") .DecrementIndent() .AppendLine("};") .AppendLine(); // Available cultures builder .AppendLine("public Cultures = [") .IncrementIndent(); foreach (var culture in Config.TypeScript.Resource.Cultures) { var last = Config.TypeScript.Resource.Cultures.Last().Code.Equals(culture.Code); var comma = last ? string.Empty : ","; builder.AppendLine($"{{ Code: '{culture.Code}', Name: '{culture.Name}' }}{comma}"); } builder .DecrementIndent() .AppendLine("];") .AppendLine(""); // Service constructor builder .AppendLine("constructor(private http: HttpClient) {") .AppendLine("}") .AppendLine(""); // Set culture method builder .AppendLine("public setCulture(cultureCode: string): Observable<boolean> {") .AppendLine("") .IncrementIndent() .AppendLine("const newCulture = this.Cultures.find(c => c.Code === cultureCode);") .AppendLine("") .AppendLine("if (!newCulture) {") .IncrementIndent() .AppendLine("throw new Error(`Invalid culture: ${cultureCode}`);") .DecrementIndent() .AppendLine("}") .AppendLine("") .AppendLine("return new Observable<boolean>(observer => {") .AppendLine("") .IncrementIndent() .AppendLine("if (cultureCode === this.CurrentCulture.Code) {") .IncrementIndent() .AppendLine("observer.next(true);") .AppendLine("observer.complete();") .AppendLine("return;") .DecrementIndent() .AppendLine("}") .AppendLine("") .AppendLine($"this.http.get<any>(`{Config.TypeScript.Resource.RequestAddress}{Config.TypeScript.Resource.JsonPrefix}-${{cultureCode}}.json`)") .IncrementIndent() .AppendLine(".subscribe(res => {") .IncrementIndent(); // Set the properties with the new language foreach (var item in Resources[Config.TypeScript.Resource.CultureDefault.Code]) { if (Config.TypeScript.Resource.Extractor) { builder .AppendLine($"this.XCommon.{item.ResourceName} = res.{item.ResourceName};"); } else { builder .AppendLine($"this.{item.ResourceName} = res.{item.ResourceName};"); } } // Complete the service builder .AppendLine("") .AppendLine("this.CurrentCulture = newCulture;") .AppendLine("") .AppendLine("observer.next(true);") .AppendLine("observer.complete();") .DecrementIndent() .AppendLine("});") .DecrementIndent() .DecrementIndent() .AppendLine("});") .DecrementIndent() .AppendLine("}") .DecrementIndent(); // Close service class builder .DecrementIndent() .AppendLine("}"); return(builder.ToString()); }
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); }
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, false)); var builder = new StringBuilderIndented(); var baseClass = "EntityBase"; var appClass = Config.CSharp.ApplicationClasses.FirstOrDefault(c => c.Name == item.Name && c.Schema == item.Schema); if (Config.CSharp.UsingApplicationBase && appClass != null) { nameSpace = new List <string>(); baseClass = $"{appClass.NamespaceEntity}.{item.Name}Entity"; builder .GenerateFileMessage() .ClassInit($"{item.Name}Entity", baseClass, $"{Config.CSharp.Entity.NameSpace}.{item.Schema}", ClassVisibility.Public, true, nameSpace.ToArray()) .ClassEnd(); } else { builder .GenerateFileMessage() .ClassInit($"{item.Name}Entity", baseClass, $"{Config.CSharp.Entity.NameSpace}.{item.Schema}", ClassVisibility.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); }
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); } }
internal static StringBuilderIndented ClassInit(this StringBuilderIndented builder, string className, string parent, string nameSpace, ClassVisility visibily, params string[] usings) { return(builder .ClassInit(className, parent, nameSpace, visibily, false, usings)); }
internal static StringBuilderIndented InterfaceEnd(this StringBuilderIndented builder) { return(ClassEnd(builder)); }
public void WriteFile(string path, string file, StringBuilderIndented builder, bool overrideIfExists) => WriteFile(path, file, builder.ToString(), overrideIfExists);
public void WriteEntity(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); 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, true)); var itemNameSpace = $"{Config.CSharp.EntityFramework.NameSpace}.{item.Schema}"; nameSpace.RemoveAll(c => c == itemNameSpace); var builder = new StringBuilderIndented(); builder .GenerateFileMessage() .ClassInit(item.Name, null, itemNameSpace, ClassVisibility.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); }