public string WriteInterfaceCode(IOpenApiDocument document, OpenApiOptions options) { string @namespace = Dependencies.Namespace.Context(options.RootNamespace); string entityNamespace = Dependencies.Namespace.Entity(options.RootNamespace); Clear(); GenerateFileHeader(); WriteLine("using System;"); WriteLine("using System.Threading;"); WriteLine("using Microsoft.EntityFrameworkCore;"); WriteLine($"using {entityNamespace};"); WriteLine(); WriteLine($"namespace {@namespace}"); using (OpenBlock()) { WriteLine($"public interface {options.ContextInterfaceName}"); using (OpenBlock()) { foreach (var kvp in document.Components.Schemas) { string name = kvp.Key; OpenApiSchema schema = kvp.Value; WriteLine(); WriteLine($@"DbSet<{name}> {name} {{ get; set; }}"); } } } return(GetText()); }
//private static void Process2() //{ // OpenApiOptions options = OpenApiUtilities.GetOptions("Guid", OutDir, RootNamespace, ContextName, SupervisorName, FilePath); // IEntityModelBuilder builder = OpenApiServices.GetService<IEntityModelBuilder>(); // var model = builder.BuildModel(options); // var entities = model.GetEntities(); //} private static void Process() { IScaffoldingManager manager = OpenApiServices.GetService <IScaffoldingManager>(); IOpenApiDocument document = OpenApiServices.GetService <IOpenApiDocument>(); ScaffoldedModel model = manager.ScaffoldModel(document.Options); manager.SaveModel(model); System.Diagnostics.Process.Start("explorer.exe", document.Options.OutputDir); }
public GeneratorDependencies( ITextProvider textProvider, INamespaceHelper namespaceHelper, INameHelper namer, ISchemaConverter schema, IPluralizer pluralizer, IOpenApiDocument document) { Namer = namer; Provider = textProvider; Namespace = namespaceHelper; Schema = schema; Pluralizer = pluralizer; Document = document; }
public string WriteInterfaceCode(IOpenApiDocument document, string supervisorInterfaceName, string @namespace) { string viewModelNamespace = Dependencies.Namespace.ViewModel(document.Options.RootNamespace); Clear(); GenerateFileHeader(); WriteLine("using System;"); WriteLine("using System.Threading;"); WriteLine("using System.Threading.Tasks;"); WriteLine("using System.Collections.Generic;"); WriteLine($"using {viewModelNamespace};"); WriteLine(); WriteLine($"namespace {@namespace}"); using (OpenBlock()) { WriteLine($"public interface {supervisorInterfaceName}"); using (OpenBlock()) { foreach (var kvp in document.GetSchemas()) { string name = StringUtilities.MakePascal(kvp.Key); string entityName = Dependencies.Namer.Entity(kvp.Key); string viewModelName = Dependencies.Namer.ViewModel(kvp.Key); string primaryKeyTypeName = document.Options.PrimaryKeyTypeName; WriteLine(); WriteLine($"// {name}"); WriteLine(); WriteLine($"Task<List<{viewModelName}>> GetAll{name}Async(CancellationToken ct = default(CancellationToken));"); WriteLine($"Task<{viewModelName}> Get{name}ByIdAsync({primaryKeyTypeName} id, CancellationToken ct = default(CancellationToken));"); WriteLine($"Task<{viewModelName}> Add{name}Async({viewModelName} input, CancellationToken ct = default(CancellationToken));"); WriteLine($"Task<bool> Update{name}Async({viewModelName} input, CancellationToken ct = default(CancellationToken));"); WriteLine($"Task<bool> Delete{name}Async({primaryKeyTypeName} id, CancellationToken ct = default(CancellationToken));"); } } } return(GetText()); }
//public static OpenApiOptions GetOptions( // string primaryKeyTypeName, // string outputDir, // string rootNamespace, // string contextName, // string supervisorName, // string filePath) //{ // return new OpenApiOptions // { // PrimaryKeyTypeName = primaryKeyTypeName, // OutputDir = outputDir, // RootNamespace = rootNamespace, // ContextClassName = contextName, // ContextInterfaceName = $"I{contextName}", // SupervisorClassName = supervisorName, // SupervisorInterfaceName = $"I{supervisorName}", // Document = ReadDocument(filePath) // }; //} public static IDictionary <string, OpenApiSchema> GetSchemas(this IOpenApiDocument document) { return(document.Components.Schemas.Where(x => !x.Value.GetBoolExtensionValue("skip")) .ToDictionary(x => x.Key, x => x.Value)); }
public string WriteClassCode(IOpenApiDocument document, string supervisorName, string supervisorInterfaceName, string @namespace) { string entityNamespace = Dependencies.Namespace.Entity(document.Options.RootNamespace); string converterNamespace = Dependencies.Namespace.Converter(document.Options.RootNamespace); string viewModelNamespace = Dependencies.Namespace.ViewModel(document.Options.RootNamespace); string repositoryNamespace = Dependencies.Namespace.Repository(document.Options.RootNamespace); Clear(); GenerateFileHeader(); WriteLine("using System;"); WriteLine("using System.Threading;"); WriteLine("using System.Threading.Tasks;"); WriteLine("using System.Collections.Generic;"); WriteLine($"using {entityNamespace};"); WriteLine($"using {converterNamespace};"); WriteLine($"using {viewModelNamespace};"); WriteLine($"using {repositoryNamespace};"); WriteLine(); WriteLine($"namespace {@namespace}"); using (OpenBlock()) { WriteLine($"public partial class {supervisorName} : {supervisorInterfaceName}"); using (OpenBlock()) { var nameList = document.GetSchemaKeys(); IDictionary <string, OpenApiSchema> schemas = document.GetSchemas(); foreach (var kvp in schemas) { string name = kvp.Key; OpenApiSchema schema = kvp.Value; string repositoryInterface = Dependencies.Namer.RepositoryInterface(name); string fieldName = Dependencies.Namer.RepositoryFieldName(name); WriteLine($"private readonly {repositoryInterface} {fieldName};"); } WriteLine(); Write($"public {supervisorName}("); PushIndent(); var list = schemas.ToList(); for (int i = 0; i < list.Count; i++) { var kvp = list[i]; string name = kvp.Key; string repositoryInterface = Dependencies.Namer.RepositoryInterface(name); string parameterName = Dependencies.Namer.RepositoryParameterName(name); Write($"{repositoryInterface} {parameterName}"); WriteLine(i != list.Count - 1 ? "," : ")"); } PopIndent(); using (OpenBlock()) { foreach (var kvp in schemas) { string name = kvp.Key; string parameterName = Dependencies.Namer.RepositoryParameterName(name); string fieldName = Dependencies.Namer.RepositoryFieldName(name); WriteLine($"{fieldName} = {parameterName};"); } } foreach (var kvp in schemas) { Process(StringUtilities.MakePascal(kvp.Key), kvp.Value); } } } return(GetText()); }
public SolutionFactory(IOpenApiDocument document, ISolutionScaffolder solution) { Document = document; Solution = solution; }
public string WriteClassCode(IOpenApiDocument document, OpenApiOptions options) { string @namespace = Dependencies.Namespace.Context(options.RootNamespace); string configurationNamespace = Dependencies.Namespace.Configuration(options.RootNamespace); string entityNamespace = Dependencies.Namespace.Entity(options.RootNamespace); Clear(); GenerateFileHeader(); WriteLine("using System;"); WriteLine("using System.Linq;"); WriteLine("using System.Threading;"); WriteLine("using System.Threading.Tasks;"); WriteLine("using System.Collections.Generic;"); WriteLine("using Microsoft.EntityFrameworkCore;"); WriteLine("using Microsoft.EntityFrameworkCore.Query;"); WriteLine($"using {entityNamespace};"); WriteLine($"using {configurationNamespace};"); WriteLine(); WriteLine($"namespace {@namespace}"); using (OpenBlock()) { WriteLine($"public partial class {options.ContextClassName} : DbContext"); using (OpenBlock()) { WriteLine(); WriteLine("public static long InstanceCount;"); IDictionary <string, OpenApiSchema> schemas = document.GetSchemas(); foreach (var kvp in schemas) { string name = kvp.Key; OpenApiSchema schema = kvp.Value; string pluralName = Dependencies.Pluralizer.Pluralize(name); WriteLine(); WriteLine( $@"private static readonly Func<{options.ContextClassName}, AsyncEnumerable<{name}>> _queryGetAll{pluralName} ="); PushIndent(); WriteLine($"EF.CompileAsyncQuery(({options.ContextClassName} db) => db.{name});"); PopIndent(); WriteLine(); WriteLine( $"private static readonly Func<{options.ContextClassName}, {options.PrimaryKeyTypeName}, AsyncEnumerable<{name}>> _queryGet{name} ="); PushIndent(); WriteLine($"EF.CompileAsyncQuery(({options.ContextClassName} db, {options.PrimaryKeyTypeName} id) =>"); PushIndent(); WriteLine($"db.{name}.Where( x => x.{name}Id == id));"); PopIndent(); PopIndent(); } WriteLine(); foreach (var kvp in schemas) { string name = kvp.Key; OpenApiSchema schema = kvp.Value; WriteLine($@"public virtual DbSet<{name}> {name} {{ get; set; }}"); } WriteLine(); WriteLine($"public {options.ContextClassName}(DbContextOptions options) : base(options) "); PushIndent(); WriteLine("=> Interlocked.Increment(ref InstanceCount);"); PopIndent(); WriteLine(); WriteLine("protected override void OnModelCreating(ModelBuilder modelBuilder)"); using (OpenBlock()) { foreach (var kvp in schemas) { string name = kvp.Key; OpenApiSchema schema = kvp.Value; string configurationName = Dependencies.Namer.Configuration(name); WriteLine($@"new {configurationName}().Configure(modelBuilder.Entity<{name}>());"); } } foreach (var kvp in schemas) { string name = kvp.Key; OpenApiSchema schema = kvp.Value; string pluralName = Dependencies.Pluralizer.Pluralize(name); WriteLine(); WriteLine( $@"public async Task<List<{name}>> GetAll{pluralName}Async() => await _queryGetAll{pluralName}(this).ToListAsync();"); WriteLine(); WriteLine( $"public async Task<List<{name}>> Get{name}Async({options.PrimaryKeyTypeName} id) => await _queryGet{name}(this, id).ToListAsync();"); } } } return(GetText()); }