| | | 1 | | using System.Reflection; |
| | | 2 | | using CShells.Features; |
| | | 3 | | using Elsa.Common.Entities; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | |
| | | 10 | | // ReSharper disable once CheckNamespace |
| | | 11 | | namespace Elsa.Persistence.EFCore; |
| | | 12 | | |
| | | 13 | | public abstract class PersistenceShellFeatureBase<TDbContext> : IShellFeature |
| | | 14 | | where TDbContext : DbContext |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets or sets a value indicating whether to use context pooling. |
| | | 18 | | /// When not explicitly set, falls back to shared settings if available. |
| | | 19 | | /// </summary> |
| | 0 | 20 | | public bool? UseContextPooling { get; set; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets or sets a value indicating whether to run migrations. |
| | | 24 | | /// When not explicitly set, falls back to shared settings if available, defaulting to true. |
| | | 25 | | /// </summary> |
| | 0 | 26 | | public bool? RunMigrations { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets or sets the lifetime of the <see cref="IDbContextFactory{TContext}"/>. |
| | | 30 | | /// When not explicitly set, falls back to shared settings if available, defaulting to <see cref="ServiceLifetime.Sc |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public ServiceLifetime? DbContextFactoryLifetime { get; set; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets or sets the connection string to use for the database. |
| | | 36 | | /// When not explicitly set, falls back to shared settings if available. |
| | | 37 | | /// </summary> |
| | 0 | 38 | | public string? ConnectionString { get; set; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets additional options to configure the database context. |
| | | 42 | | /// When not explicitly set, falls back to shared settings if available. |
| | | 43 | | /// </summary> |
| | 0 | 44 | | public ElsaDbContextOptions? DbContextOptions { get; set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets or sets the callback used to configure the <see cref="DbContextOptionsBuilder"/>. |
| | | 48 | | /// </summary> |
| | 0 | 49 | | protected virtual Action<IServiceProvider, DbContextOptionsBuilder> DbContextOptionsBuilder { get; set; } = (_, _) = |
| | | 50 | | |
| | | 51 | | public void ConfigureServices(IServiceCollection services) |
| | | 52 | | { |
| | | 53 | | // Capture feature-specific settings |
| | 0 | 54 | | var featureConnectionString = ConnectionString; |
| | 0 | 55 | | var featureDbContextOptions = DbContextOptions; |
| | 0 | 56 | | var featureUseContextPooling = UseContextPooling; |
| | 0 | 57 | | var featureRunMigrations = RunMigrations; |
| | 0 | 58 | | var featureDbContextFactoryLifetime = DbContextFactoryLifetime; |
| | | 59 | | |
| | | 60 | | // Resolve effective settings at runtime, falling back to shared settings |
| | 0 | 61 | | Action<IServiceProvider, DbContextOptionsBuilder> setup = (sp, opts) => |
| | 0 | 62 | | { |
| | 0 | 63 | | var sharedSettings = sp.GetService<IOptions<SharedPersistenceSettings>>()?.Value; |
| | 0 | 64 | | |
| | 0 | 65 | | var connectionString = featureConnectionString |
| | 0 | 66 | | ?? sharedSettings?.ConnectionString |
| | 0 | 67 | | ?? throw new InvalidOperationException( |
| | 0 | 68 | | $"Connection string not configured for {GetType().Name}. " + |
| | 0 | 69 | | $"Either configure the feature directly or provide shared settings via the combined persistence feat |
| | 0 | 70 | | |
| | 0 | 71 | | var dbContextOptions = featureDbContextOptions ?? sharedSettings?.DbContextOptions; |
| | 0 | 72 | | |
| | 0 | 73 | | opts.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); |
| | 0 | 74 | | |
| | 0 | 75 | | // Configure the database provider |
| | 0 | 76 | | var migrationsAssembly = GetMigrationsAssembly(); |
| | 0 | 77 | | ConfigureProvider(opts, migrationsAssembly, connectionString, dbContextOptions); |
| | 0 | 78 | | |
| | 0 | 79 | | // Allow derived classes to further configure |
| | 0 | 80 | | DbContextOptionsBuilder(sp, opts); |
| | 0 | 81 | | }; |
| | | 82 | | |
| | | 83 | | // Resolve pooling and lifetime settings with fallback |
| | | 84 | | // Note: These are resolved at configuration time, not runtime, but they'll use defaults if not set |
| | 0 | 85 | | var useContextPooling = featureUseContextPooling ?? false; |
| | 0 | 86 | | var dbContextFactoryLifetime = featureDbContextFactoryLifetime ?? ServiceLifetime.Scoped; |
| | 0 | 87 | | var runMigrations = featureRunMigrations ?? true; |
| | | 88 | | |
| | 0 | 89 | | if (useContextPooling) |
| | 0 | 90 | | services.AddPooledDbContextFactory<TDbContext>(setup); |
| | | 91 | | else |
| | 0 | 92 | | services.AddDbContextFactory<TDbContext>(setup, dbContextFactoryLifetime); |
| | | 93 | | |
| | 0 | 94 | | services.Decorate<IDbContextFactory<TDbContext>, TenantAwareDbContextFactory<TDbContext>>(); |
| | | 95 | | |
| | 0 | 96 | | services.Configure<MigrationOptions>(options => |
| | 0 | 97 | | { |
| | 0 | 98 | | options.RunMigrations[typeof(TDbContext)] = runMigrations; |
| | 0 | 99 | | }); |
| | | 100 | | |
| | 0 | 101 | | services.AddStartupTask<RunMigrationsStartupTask<TDbContext>>(); |
| | 0 | 102 | | OnConfiguring(services); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the assembly containing migrations for this provider. |
| | | 107 | | /// By default, returns the assembly of the concrete feature type. |
| | | 108 | | /// </summary> |
| | 0 | 109 | | protected virtual Assembly GetMigrationsAssembly() => GetType().Assembly; |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Configures the database provider for the specified <see cref="DbContextOptionsBuilder"/>. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="builder">The options builder to configure.</param> |
| | | 115 | | /// <param name="migrationsAssembly">The assembly containing migrations.</param> |
| | | 116 | | /// <param name="connectionString">The connection string to use.</param> |
| | | 117 | | /// <param name="options">Additional options to configure the database context.</param> |
| | | 118 | | protected abstract void ConfigureProvider( |
| | | 119 | | DbContextOptionsBuilder builder, |
| | | 120 | | Assembly migrationsAssembly, |
| | | 121 | | string connectionString, |
| | | 122 | | ElsaDbContextOptions? options); |
| | | 123 | | |
| | | 124 | | protected virtual void OnConfiguring(IServiceCollection services) |
| | | 125 | | { |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Adds a store to the service collection. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 132 | | /// <typeparam name="TStore">The type of the store.</typeparam> |
| | | 133 | | protected void AddStore<TEntity, TStore>(IServiceCollection services) where TEntity : class, new() where TStore : cl |
| | | 134 | | { |
| | 0 | 135 | | services |
| | 0 | 136 | | .AddScoped<Store<TDbContext, TEntity>>() |
| | 0 | 137 | | .AddScoped<TStore>() |
| | 0 | 138 | | ; |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Adds an entity store to the service collection. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 145 | | /// <typeparam name="TStore">The type of the store.</typeparam> |
| | | 146 | | protected void AddEntityStore<TEntity, TStore>(IServiceCollection services) where TEntity : Entity, new() where TSto |
| | | 147 | | { |
| | 0 | 148 | | services |
| | 0 | 149 | | .AddScoped<EntityStore<TDbContext, TEntity>>() |
| | 0 | 150 | | .AddScoped<TStore>() |
| | 0 | 151 | | ; |
| | 0 | 152 | | } |
| | | 153 | | } |