private void BuildNormalService(StringBuilderIndented builder) { builder .AppendLine("switch (this.Language.Name) {") .IncrementIndent(); foreach (var culture in Config.TypeScript.Resource.Cultures) { builder .AppendLine($"case '{culture.Name}':") .IncrementIndent(); foreach (var resource in Resources) { builder .AppendLine($"this.{resource.ResourceName} = new {resource.ResourceName}{GetCultureName(culture.Name)}();"); } builder .AppendLine("break;") .DecrementIndent(); } builder .DecrementIndent() .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}"); } ; var builder = new StringBuilderIndented(); builder .ClassInit("Register", null, $"{Config.CSharp.Factory.NameSpace}", ClassVisility.Public, true, nameSpace.ToArray()) .AppendLine("public static void Do(bool unitTest = false)") .AppendLine("{") .IncrementIndent(); if (Config.CSharp.Repository.Contract != null && Config.CSharp.Repository.Contract.Execute) { builder .AppendLine("RegisterRepository();"); } builder .AppendLine("RegisterValidate();") .AppendLine("RegisterQuery();") .AppendLine("RegisterCustom(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 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); }
private void WriteResourceClasses(StringBuilderIndented builder) { var resources = Resources[Config.TypeScript.Resource.CultureDefault.Code]; // Write the file header builder .AppendLine("import { Injectable } from '@angular/core';") .AppendLine("import { HttpClient } from '@angular/common/http';") .AppendLine("import { Observable } from 'rxjs';") .AppendLine(""); // Create the resouce class with default values foreach (var resource in resources) { builder .AppendLine($"class {resource.ResourceName}Resource {{") .IncrementIndent(); foreach (var value in resource.Properties) { builder .AppendLine($"{value.Key} = '{value.Value.Replace("'", "\\'")}';"); } builder .DecrementIndent() .AppendLine("}") .AppendLine() .DecrementIndent(); } }
internal static StringBuilderIndented GenerateFileMessage(this StringBuilderIndented builder) { builder.AppendLine("/***************************************** WARNING *****************************************/"); builder.AppendLine("/* Don't write any code in this file, because it will be rewritten on the next generation. */"); builder.AppendLine("/*******************************************************************************************/"); builder.AppendLine(); return(builder); }
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); }
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); }
private void TypeScript(string path, string name, string selector, string module, string feture) { var file = $"{selector}.component.ts"; var builder = new StringBuilderIndented(); var templateUrl = $"{Quote}./{selector}.html{Quote},"; builder .AppendLine($"import {{ Component, OnInit }} from {Quote}@angular/core{Quote};") .AppendLine() .AppendLine("@Component({") .IncrementIndent() .AppendLine($"selector: {Quote}{selector}{Quote},") .AppendLine("templateUrl: " + templateUrl.ToLower()) .AppendLine($"styleUrls: [{Quote}./{selector}.scss{Quote}]") .DecrementIndent() .AppendLine("})") .AppendLine($"export class {name.GetName()}Component implements OnInit {{") .IncrementIndent() .AppendLine("constructor() { }") .AppendLine() .AppendLine("public ngOnInit(): void { }") .DecrementIndent() .AppendLine("}"); Writer.WriteFile(path, file, builder, false); }
private void WriteDataSource(DataBaseTable item) { var path = Path.Combine(Config.CSharp.UnitTest.Path, "DataSource", item.Schema); var file = $"{item.Name}DataSource.cs"; var nameSpace = new List <string> { "System", "System.Collections.Generic", "XCommon.Extensions.Converters", "XCommon.Util" }; nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}"); nameSpace.Add($"{Config.CSharp.Entity.NameSpace}.{item.Schema}.Filter"); var builder = new StringBuilderIndented(); builder .ClassInit($"{item.Name}DataSource", string.Empty, $"{Config.CSharp.UnitTest.NameSpace}.{item.Schema}", ClassVisibility.PublicStatic, nameSpace.ToArray()); builder .AppendLine($"public static IEnumerable<object[]> {item.Name}EntityDataSource") .AppendLine("{") .IncrementIndent() .AppendLine("get") .AppendLine("{") .IncrementIndent() .AppendLine($"var result = new PairList<Pair<{item.Name}Entity>, bool, string>();"); builder .ClassEnd(); Writer.WriteFile(path, file, builder, false); }
private void BuildLanguageSuportedContructor(StringBuilderIndented builder) { foreach (var culture in Config.TypeScript.Resource.Cultures) { builder.AppendLine($"this.Languages.push({{ Name: '{culture.Name}', Description: '{culture.Description}' }});"); } }
private void TypeScript(string path, string name, string selector, string module, string feture) { var file = $"{selector}.component.ts"; var builder = new StringBuilderIndented(); var templateUrl = $"'./{selector}.html',"; builder .AppendLine("import { Component, OnInit } from '@angular/core';") .AppendLine() .AppendLine("@Component({") .IncrementIndent() .AppendLine($"selector: '{selector}',") .AppendLine("templateUrl: " + templateUrl.ToLower()) .AppendLine($"styles: [require('./{selector}.scss')]") .DecrementIndent() .AppendLine("})") .AppendLine($"export class {name.GetName()}Component implements OnInit {{") .IncrementIndent() .AppendLine("constructor() { }") .AppendLine() .AppendLine("ngOnInit(): void { }") .DecrementIndent() .AppendLine("}"); Writer.WriteFile(path, file, builder, false); }
private void ProcessExtras() { if (!Config.TypeScript.Entity.IncludeUtils) { return; } var builder = new StringBuilderIndented(); builder .AppendLine("export interface Map<TValue> {") .IncrementIndent() .AppendLine("[K: string]: TValue;") .DecrementIndent() .AppendLine("}") .AppendLine() .AppendLine("export interface KeyValue<TKey, TValue> {") .IncrementIndent() .AppendLine("Key: TKey;") .AppendLine("Value: TValue;") .DecrementIndent() .AppendLine("}"); Writer.WriteFile(Config.TypeScript.Entity.Path.ToLower(), "entity-util.ts", builder, false); }
private void ProcessStaticData() { var builder = new StringBuilderIndented(); builder .AppendLine($"import {{ Injectable }} from {Quote}@angular/core{Quote};") .AppendLine($"import {{ {string.Join(", ", TSEnums.Select(c => c.Name))} }} from {Quote}../entity/enum{Quote};") .AppendLine($"import {{ TranslateService }} from {Quote}./translate.service{Quote};") .AppendLine("") .AppendLine("export class SelectOption {") .IncrementIndent() .AppendLine("value: any;") .AppendLine("text: string;") .DecrementIndent() .AppendLine("}") .AppendLine("") .AppendLine("@Injectable()") .AppendLine("export class StaticDataService {") .AppendLine("") .AppendLine("constructor(private translator: TranslateService) { }") .AppendLine("") .IncrementIndent(); foreach (var item in TSEnums) { var lowerName = item.Name.Substring(0, 1).ToLower() + item.Name.Substring(1); builder .AppendLine($"public {lowerName}: SelectOption[] = this.mapEnum({Quote}{item.Name}{Quote}, {item.Name});") .AppendLine(""); } builder .AppendLine("private mapEnum(key: string, type: any): SelectOption[] {") .IncrementIndent() .AppendLine("return Object.keys(type)") .IncrementIndent() .AppendLine($".filter(c => typeof type[c] === {Quote}number{Quote})") .AppendLine(".map(c => ({ value: type[c], text: this.translator.Static[`${key}${c}`] || c }));") .DecrementIndent() .DecrementIndent() .AppendLine("}") .DecrementIndent() .AppendLine("}"); Writer.WriteFile(Config.TypeScript.Resource.Path.ToLower(), "static-data.service.ts", builder, true); }
private void GenerateFactoryValidate(StringBuilderIndented builder) { builder .AppendLine("private static void RegisterValidate()") .AppendLine("{") .IncrementIndent(); foreach (var table in Config.DataBaseItems.SelectMany(c => c.Tables)) { builder .AppendLine($"Kernel.Map<ISpecificationValidation<{table.Name}Entity>>().To<{table.Name}Validate>();"); } builder .DecrementIndent() .AppendLine("}"); }
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(); }
private void GenerateFactoryRepository(StringBuilderIndented builder) { builder .AppendLine("private static void RegisterRepository()") .AppendLine("{") .IncrementIndent(); foreach (var table in Config.DataBaseItems.SelectMany(c => c.Tables)) { builder .AppendLine($"Kernel.Map<I{table.Name}Business>().To<{table.Name}Business>();"); } builder .DecrementIndent() .AppendLine("}") .AppendLine(); }
private void BuildLanguageSuported(StringBuilderIndented builder) { builder .AppendLine("export class ApplicationCulture {") .IncrementIndent() .AppendLine("public Name: string;") .AppendLine("public Description: string;") .DecrementIndent() .AppendLine("}") .AppendLine(); }
private void Sass(string path, string selector) { var file = $"{selector}.scss"; var builder = new StringBuilderIndented(); builder .AppendLine($".{selector} {{") .AppendLine("}"); Writer.WriteFile(path, file, builder, false); }
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("});"); }
internal static StringBuilderIndented AddUsing(this StringBuilderIndented builder, params string[] args) { foreach (var nameSpace in args.Distinct().OrderBy(c => c)) { if (nameSpace.IsEmpty()) { continue; } builder.AppendLine($"using {nameSpace};"); } return(builder); }
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(); } } }
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); }
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(); }
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); 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(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 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 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); }