< Summary

Information
Class: Elsa.Persistence.EFCore.PersistenceFeatureBase<T1, T2>
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/PersistenceFeatureBase.cs
Line coverage
94%
Covered lines: 33
Uncovered lines: 2
Coverable lines: 35
Total lines: 95
Line coverage: 94.2%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_UseContextPooling()100%11100%
get_RunMigrations()100%11100%
get_DbContextFactoryLifetime()100%11100%
get_DbContextOptionsBuilder()100%11100%
ConfigureHostedServices()100%11100%
Apply()50%4487.5%
ConfigureMigrations()100%11100%
AddStore()100%11100%
AddEntityStore()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/PersistenceFeatureBase.cs

#LineLine coverage
 1using Elsa.Common.Entities;
 2using Elsa.Extensions;
 3using Elsa.Features.Abstractions;
 4using Elsa.Features.Services;
 5using Microsoft.EntityFrameworkCore;
 6using Microsoft.EntityFrameworkCore.Diagnostics;
 7using Microsoft.Extensions.DependencyInjection;
 8
 9// ReSharper disable once CheckNamespace
 10namespace Elsa.Persistence.EFCore;
 11
 612public 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>
 618    public virtual bool UseContextPooling { get; set; }
 19
 20    /// <summary>
 21    /// Gets or sets a value indicating whether to run migrations.
 22    /// </summary>
 1223    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>
 1228    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>
 166933    public virtual Action<IServiceProvider, DbContextOptionsBuilder> DbContextOptionsBuilder { get; set; } = null!;
 34
 35    public override void ConfigureHostedServices()
 36    {
 637        ConfigureMigrations();
 638    }
 39
 40    /// <inheritdoc />
 41    public override void Apply()
 42    {
 643        if (DbContextOptionsBuilder == null)
 044            throw new InvalidOperationException("The DbContextOptionsBuilder must be configured.");
 45
 646        Action<IServiceProvider, DbContextOptionsBuilder> setup = (sp, opts) =>
 647        {
 330648            opts.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning));
 165349            DbContextOptionsBuilder(sp, opts);
 165950        };
 51
 652        if (UseContextPooling)
 053            Services.AddPooledDbContextFactory<TDbContext>(setup);
 54        else
 655            Services.AddDbContextFactory<TDbContext>(setup, DbContextFactoryLifetime);
 56
 657        Services.Decorate<IDbContextFactory<TDbContext>, TenantAwareDbContextFactory<TDbContext>>();
 58
 659        Services.Configure<MigrationOptions>(options =>
 660        {
 661            options.RunMigrations[typeof(TDbContext)] = RunMigrations;
 1262        });
 663    }
 64
 65    protected virtual void ConfigureMigrations()
 66    {
 667        Services.AddStartupTask<RunMigrationsStartupTask<TDbContext>>();
 668    }
 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    {
 377        Services
 378            .AddScoped<Store<TDbContext, TEntity>>()
 379            .AddScoped<TStore>()
 380            ;
 381    }
 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    {
 1090        Services
 1091            .AddScoped<EntityStore<TDbContext, TEntity>>()
 1092            .AddScoped<TStore>()
 1093            ;
 1094    }
 95}