< 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
 4212public 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>
 4218    public virtual bool UseContextPooling { get; set; }
 19
 20    /// <summary>
 21    /// Gets or sets a value indicating whether to run migrations.
 22    /// </summary>
 8423    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>
 8428    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>
 238533    public virtual Action<IServiceProvider, DbContextOptionsBuilder> DbContextOptionsBuilder { get; set; } = null!;
 34
 35    public override void ConfigureHostedServices()
 36    {
 4237        ConfigureMigrations();
 4238    }
 39
 40    /// <inheritdoc />
 41    public override void Apply()
 42    {
 4243        if (DbContextOptionsBuilder == null)
 044            throw new InvalidOperationException("The DbContextOptionsBuilder must be configured.");
 45
 4246        Action<IServiceProvider, DbContextOptionsBuilder> setup = (sp, opts) =>
 4247        {
 454648            opts.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning));
 227349            DbContextOptionsBuilder(sp, opts);
 231550        };
 51
 4252        if (UseContextPooling)
 053            Services.AddPooledDbContextFactory<TDbContext>(setup);
 54        else
 4255            Services.AddDbContextFactory<TDbContext>(setup, DbContextFactoryLifetime);
 56
 4257        Services.Decorate<IDbContextFactory<TDbContext>, TenantAwareDbContextFactory<TDbContext>>();
 58
 4259        Services.Configure<MigrationOptions>(options =>
 4260        {
 4261            options.RunMigrations[typeof(TDbContext)] = RunMigrations;
 8462        });
 4263    }
 64
 65    protected virtual void ConfigureMigrations()
 66    {
 4267        Services.AddStartupTask<RunMigrationsStartupTask<TDbContext>>();
 4268    }
 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    {
 2177        Services
 2178            .AddScoped<Store<TDbContext, TEntity>>()
 2179            .AddScoped<TStore>()
 2180            ;
 2181    }
 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    {
 7090        Services
 7091            .AddScoped<EntityStore<TDbContext, TEntity>>()
 7092            .AddScoped<TStore>()
 7093            ;
 7094    }
 95}