| | | 1 | | using CShells.Features; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Elsa.Persistence.EFCore; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Base class for combined persistence shell features that provide unified configuration |
| | | 9 | | /// for multiple persistence components (definitions, instances, runtime, etc.). |
| | | 10 | | /// |
| | | 11 | | /// This base class handles registering <see cref="SharedPersistenceSettings"/> so that |
| | | 12 | | /// dependent persistence features can use the shared settings as a fallback when |
| | | 13 | | /// feature-specific settings are not provided. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// By using this base class, users can configure persistence settings once at the |
| | | 18 | | /// combined feature level instead of repeating configuration for each dependent feature. |
| | | 19 | | /// </para> |
| | | 20 | | /// <example> |
| | | 21 | | /// Example appsettings.json with unified configuration: |
| | | 22 | | /// <code> |
| | | 23 | | /// { |
| | | 24 | | /// "CShells": { |
| | | 25 | | /// "Shells": [{ |
| | | 26 | | /// "Settings": { |
| | | 27 | | /// "SqliteWorkflowPersistence": { |
| | | 28 | | /// "ConnectionString": "Data Source=elsa.db;Cache=Shared", |
| | | 29 | | /// "DbContextOptions": { |
| | | 30 | | /// "EnableSensitiveDataLogging": false |
| | | 31 | | /// } |
| | | 32 | | /// } |
| | | 33 | | /// }, |
| | | 34 | | /// "Features": ["SqliteWorkflowPersistence"] |
| | | 35 | | /// }] |
| | | 36 | | /// } |
| | | 37 | | /// } |
| | | 38 | | /// </code> |
| | | 39 | | /// </example> |
| | | 40 | | /// </remarks> |
| | | 41 | | public abstract class CombinedPersistenceShellFeatureBase : IShellFeature |
| | | 42 | | { |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets the connection string to use for the database. |
| | | 45 | | /// This setting is shared with all dependent persistence features. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public string? ConnectionString { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets or sets additional options to configure the database context. |
| | | 51 | | /// This setting is shared with all dependent persistence features. |
| | | 52 | | /// </summary> |
| | 0 | 53 | | public ElsaDbContextOptions? DbContextOptions { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets or sets a value indicating whether to use context pooling. |
| | | 57 | | /// This setting is shared with all dependent persistence features. |
| | | 58 | | /// </summary> |
| | 0 | 59 | | public bool? UseContextPooling { get; set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets a value indicating whether to run migrations. |
| | | 63 | | /// This setting is shared with all dependent persistence features. |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public bool? RunMigrations { get; set; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets or sets the lifetime of the DbContextFactory. |
| | | 69 | | /// This setting is shared with all dependent persistence features. |
| | | 70 | | /// </summary> |
| | 0 | 71 | | public ServiceLifetime? DbContextFactoryLifetime { get; set; } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc /> |
| | | 74 | | public void ConfigureServices(IServiceCollection services) |
| | | 75 | | { |
| | | 76 | | // Register shared settings that dependent features will use as fallback |
| | 0 | 77 | | services.Configure<SharedPersistenceSettings>(options => |
| | 0 | 78 | | { |
| | 0 | 79 | | // Only set values that were explicitly configured |
| | 0 | 80 | | if (ConnectionString != null) |
| | 0 | 81 | | options.ConnectionString = ConnectionString; |
| | 0 | 82 | | |
| | 0 | 83 | | if (DbContextOptions != null) |
| | 0 | 84 | | options.DbContextOptions = DbContextOptions; |
| | 0 | 85 | | |
| | 0 | 86 | | if (UseContextPooling.HasValue) |
| | 0 | 87 | | options.UseContextPooling = UseContextPooling; |
| | 0 | 88 | | |
| | 0 | 89 | | if (RunMigrations.HasValue) |
| | 0 | 90 | | options.RunMigrations = RunMigrations; |
| | 0 | 91 | | |
| | 0 | 92 | | if (DbContextFactoryLifetime.HasValue) |
| | 0 | 93 | | options.DbContextFactoryLifetime = DbContextFactoryLifetime; |
| | 0 | 94 | | }); |
| | | 95 | | |
| | 0 | 96 | | OnConfiguring(services); |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Override this method to add additional service registrations. |
| | | 101 | | /// </summary> |
| | | 102 | | protected virtual void OnConfiguring(IServiceCollection services) |
| | | 103 | | { |
| | 0 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |