| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Features.Abstractions; |
| | | 4 | | using Elsa.Features.Services; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | // ReSharper disable once CheckNamespace |
| | | 10 | | namespace Elsa.Persistence.EFCore; |
| | | 11 | | |
| | 6 | 12 | | public abstract class PersistenceFeatureBase<TFeature, TDbContext>(IModule module) : FeatureBase(module) |
| | | 13 | | where TDbContext : ElsaDbContextBase |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets or sets a value indicating whether to use context pooling. |
| | | 17 | | /// </summary> |
| | 6 | 18 | | public virtual bool UseContextPooling { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets or sets a value indicating whether to run migrations. |
| | | 22 | | /// </summary> |
| | 12 | 23 | | public virtual bool RunMigrations { get; set; } = true; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets the lifetime of the <see cref="IDbContextFactory{TContext}"/>. Defaults to <see cref="ServiceLifeti |
| | | 27 | | /// </summary> |
| | 12 | 28 | | public ServiceLifetime DbContextFactoryLifetime { get; set; } = ServiceLifetime.Scoped; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets or sets the callback used to configure the <see cref="DbContextOptionsBuilder"/>. |
| | | 32 | | /// </summary> |
| | 1669 | 33 | | public virtual Action<IServiceProvider, DbContextOptionsBuilder> DbContextOptionsBuilder { get; set; } = null!; |
| | | 34 | | |
| | | 35 | | public override void ConfigureHostedServices() |
| | | 36 | | { |
| | 6 | 37 | | ConfigureMigrations(); |
| | 6 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public override void Apply() |
| | | 42 | | { |
| | 6 | 43 | | if (DbContextOptionsBuilder == null) |
| | 0 | 44 | | throw new InvalidOperationException("The DbContextOptionsBuilder must be configured."); |
| | | 45 | | |
| | 6 | 46 | | Action<IServiceProvider, DbContextOptionsBuilder> setup = (sp, opts) => |
| | 6 | 47 | | { |
| | 3306 | 48 | | opts.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); |
| | 1653 | 49 | | DbContextOptionsBuilder(sp, opts); |
| | 1659 | 50 | | }; |
| | | 51 | | |
| | 6 | 52 | | if (UseContextPooling) |
| | 0 | 53 | | Services.AddPooledDbContextFactory<TDbContext>(setup); |
| | | 54 | | else |
| | 6 | 55 | | Services.AddDbContextFactory<TDbContext>(setup, DbContextFactoryLifetime); |
| | | 56 | | |
| | 6 | 57 | | Services.Decorate<IDbContextFactory<TDbContext>, TenantAwareDbContextFactory<TDbContext>>(); |
| | | 58 | | |
| | 6 | 59 | | Services.Configure<MigrationOptions>(options => |
| | 6 | 60 | | { |
| | 6 | 61 | | options.RunMigrations[typeof(TDbContext)] = RunMigrations; |
| | 12 | 62 | | }); |
| | 6 | 63 | | } |
| | | 64 | | |
| | | 65 | | protected virtual void ConfigureMigrations() |
| | | 66 | | { |
| | 6 | 67 | | Services.AddStartupTask<RunMigrationsStartupTask<TDbContext>>(); |
| | 6 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Adds a store to the service collection. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 74 | | /// <typeparam name="TStore">The type of the store.</typeparam> |
| | | 75 | | protected void AddStore<TEntity, TStore>() where TEntity : class, new() where TStore : class |
| | | 76 | | { |
| | 3 | 77 | | Services |
| | 3 | 78 | | .AddScoped<Store<TDbContext, TEntity>>() |
| | 3 | 79 | | .AddScoped<TStore>() |
| | 3 | 80 | | ; |
| | 3 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Adds an entity store to the service collection. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 87 | | /// <typeparam name="TStore">The type of the store.</typeparam> |
| | | 88 | | protected void AddEntityStore<TEntity, TStore>() where TEntity : Entity, new() where TStore : class |
| | | 89 | | { |
| | 10 | 90 | | Services |
| | 10 | 91 | | .AddScoped<EntityStore<TDbContext, TEntity>>() |
| | 10 | 92 | | .AddScoped<TStore>() |
| | 10 | 93 | | ; |
| | 10 | 94 | | } |
| | | 95 | | } |