|
| 1 | +using System; |
| 2 | +using DbApiBuilderEntityGenerator.Core.Extensions; |
| 3 | +using DbApiBuilderEntityGenerator.Core.Options; |
| 4 | +using Microsoft.EntityFrameworkCore.Design; |
| 5 | +using Microsoft.EntityFrameworkCore.Scaffolding; |
| 6 | +using Microsoft.EntityFrameworkCore.Scaffolding.Metadata; |
| 7 | +using Microsoft.EntityFrameworkCore.Storage; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | + |
| 11 | +namespace DbApiBuilderEntityGenerator.Core; |
| 12 | + |
| 13 | +public class CodeGenerator : ICodeGenerator |
| 14 | +{ |
| 15 | + private readonly ILoggerFactory _loggerFactory; |
| 16 | + private readonly ILogger _logger; |
| 17 | + private readonly ModelGenerator _modelGenerator; |
| 18 | + |
| 19 | + public CodeGenerator(ILoggerFactory loggerFactory) |
| 20 | + { |
| 21 | + _loggerFactory = loggerFactory; |
| 22 | + _logger = loggerFactory.CreateLogger<CodeGenerator>(); |
| 23 | + _modelGenerator = new ModelGenerator(loggerFactory); |
| 24 | + // _synchronizer = new SourceSynchronizer(loggerFactory); |
| 25 | + } |
| 26 | + |
| 27 | + public GeneratorOptions Options { get; set; } = null!; |
| 28 | + public bool Generate(GeneratorOptions options) |
| 29 | + { |
| 30 | + Options = options ?? throw new ArgumentNullException(nameof(options)); |
| 31 | + |
| 32 | + var databaseProviders = GetDatabaseProviders(); |
| 33 | + var databaseModel = GetDatabaseModel(databaseProviders.factory); |
| 34 | + if (databaseModel == null) |
| 35 | + throw new InvalidOperationException("Failed to create database model"); |
| 36 | + |
| 37 | + _logger.LogInformation("Loaded database model for: {databaseName}", databaseModel.DatabaseName); |
| 38 | + |
| 39 | + var context = _modelGenerator.Generate(Options, databaseModel, databaseProviders.mapping); |
| 40 | + |
| 41 | + return true; |
| 42 | + } |
| 43 | + private DatabaseModel GetDatabaseModel(IDatabaseModelFactory factory) |
| 44 | + { |
| 45 | + _logger.LogInformation("Loading database model ..."); |
| 46 | + |
| 47 | + |
| 48 | + var connectionString = ResolveConnectionString(Options.ConnectionString, Options.UserSecretsId, Options.ConnectionName); |
| 49 | + if (string.IsNullOrEmpty(connectionString)) |
| 50 | + throw new InvalidOperationException("Could not find connection string."); |
| 51 | + |
| 52 | + var options = new DatabaseModelFactoryOptions(Options.Tables, Options.Schemas); |
| 53 | + |
| 54 | + return factory.Create(connectionString, options); |
| 55 | + } |
| 56 | + |
| 57 | + private static string? ResolveConnectionString(string? connectionString, |
| 58 | + string? userSecretsId, |
| 59 | + string? connectionName) |
| 60 | + { |
| 61 | + if (connectionString.HasValue()) |
| 62 | + return connectionString; |
| 63 | + |
| 64 | + if (userSecretsId.HasValue() && connectionName.HasValue()) |
| 65 | + { |
| 66 | + var secretsStore = new SecretsStore(userSecretsId); |
| 67 | + if (secretsStore.ContainsKey(connectionName)) |
| 68 | + return secretsStore[connectionName]; |
| 69 | + } |
| 70 | + |
| 71 | + throw new InvalidOperationException("Could not find connection string."); |
| 72 | + } |
| 73 | + |
| 74 | + private (IDatabaseModelFactory factory, IRelationalTypeMappingSource mapping) GetDatabaseProviders() |
| 75 | + { |
| 76 | + var provider = Options.Provider; |
| 77 | + |
| 78 | + _logger.LogDebug("Creating database model factory for: {provider}", provider); |
| 79 | + |
| 80 | + // start a new service container to create the database model factory |
| 81 | + var services = new ServiceCollection() |
| 82 | + .AddSingleton(_loggerFactory) |
| 83 | + .AddEntityFrameworkDesignTimeServices(); |
| 84 | + |
| 85 | + switch (provider) |
| 86 | + { |
| 87 | + case DatabaseProviders.SqlServer: |
| 88 | + ConfigureSqlServerServices(services); |
| 89 | + break; |
| 90 | + case DatabaseProviders.PostgreSQL: |
| 91 | + ConfigurePostgresServices(services); |
| 92 | + break; |
| 93 | + case DatabaseProviders.MySQL: |
| 94 | + ConfigureMySqlServices(services); |
| 95 | + break; |
| 96 | + case DatabaseProviders.Sqlite: |
| 97 | + ConfigureSqliteServices(services); |
| 98 | + break; |
| 99 | + case DatabaseProviders.Oracle: |
| 100 | + ConfigureOracleServices(services); |
| 101 | + break; |
| 102 | + default: |
| 103 | + throw new NotSupportedException($"The specified provider '{provider}' is not supported."); |
| 104 | + } |
| 105 | + |
| 106 | + var serviceProvider = services |
| 107 | + .BuildServiceProvider(); |
| 108 | + |
| 109 | + var databaseModelFactory = serviceProvider |
| 110 | + .GetRequiredService<IDatabaseModelFactory>(); |
| 111 | + |
| 112 | + var typeMappingSource = serviceProvider |
| 113 | + .GetRequiredService<IRelationalTypeMappingSource>(); |
| 114 | + |
| 115 | + return (databaseModelFactory, typeMappingSource); |
| 116 | + } |
| 117 | + private static void ConfigureMySqlServices(IServiceCollection services) |
| 118 | + { |
| 119 | + var designTimeServices = new Pomelo.EntityFrameworkCore.MySql.Design.Internal.MySqlDesignTimeServices(); |
| 120 | + designTimeServices.ConfigureDesignTimeServices(services); |
| 121 | + services.AddEntityFrameworkMySqlNetTopologySuite(); |
| 122 | + } |
| 123 | + |
| 124 | + private static void ConfigurePostgresServices(IServiceCollection services) |
| 125 | + { |
| 126 | + var designTimeServices = new Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal.NpgsqlDesignTimeServices(); |
| 127 | + designTimeServices.ConfigureDesignTimeServices(services); |
| 128 | + services.AddEntityFrameworkNpgsqlNetTopologySuite(); |
| 129 | + } |
| 130 | + |
| 131 | + private static void ConfigureSqlServerServices(IServiceCollection services) |
| 132 | + { |
| 133 | + var designTimeServices = new Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerDesignTimeServices(); |
| 134 | + designTimeServices.ConfigureDesignTimeServices(services); |
| 135 | + services.AddEntityFrameworkSqlServerNetTopologySuite(); |
| 136 | + } |
| 137 | + |
| 138 | + private static void ConfigureSqliteServices(IServiceCollection services) |
| 139 | + { |
| 140 | + var designTimeServices = new Microsoft.EntityFrameworkCore.Sqlite.Design.Internal.SqliteDesignTimeServices(); |
| 141 | + designTimeServices.ConfigureDesignTimeServices(services); |
| 142 | + services.AddEntityFrameworkSqliteNetTopologySuite(); |
| 143 | + } |
| 144 | + |
| 145 | + private static void ConfigureOracleServices(IServiceCollection services) |
| 146 | + { |
| 147 | + var designTimeServices = new Oracle.EntityFrameworkCore.Design.Internal.OracleDesignTimeServices(); |
| 148 | + designTimeServices.ConfigureDesignTimeServices(services); |
| 149 | + } |
| 150 | +} |
0 commit comments