< Summary

Information
Class: Elsa.Persistence.EFCore.TenantAwareDbContextFactory<T>
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/TenantAwareDbContextFactory.cs
Line coverage
76%
Covered lines: 10
Uncovered lines: 3
Coverable lines: 13
Total lines: 37
Line coverage: 76.9%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CreateDbContext()100%210%
CreateDbContextAsync()100%11100%
SetTenantId(...)100%44100%

File(s)

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

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Extensions;
 3using JetBrains.Annotations;
 4using Microsoft.EntityFrameworkCore;
 5
 6namespace Elsa.Persistence.EFCore;
 7
 8/// <summary>
 9/// A factory class that wraps and extends the functionality of an existing <see cref="IDbContextFactory{TDbContext}"/>
 10/// to provide multi-tenancy support. This ensures that the created DbContext instances are aware of the current tenant 
 11/// </summary>
 12[UsedImplicitly]
 165313public class TenantAwareDbContextFactory<TDbContext>(
 165314    IDbContextFactory<TDbContext> decoratedFactory,
 165315    ITenantAccessor tenantAccessor) : IDbContextFactory<TDbContext>
 16    where TDbContext : DbContext
 17{
 18    public TDbContext CreateDbContext()
 19    {
 020        var context = decoratedFactory.CreateDbContext();
 021        SetTenantId(context);
 022        return context;
 23    }
 24
 25    public async Task<TDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default)
 26    {
 1266327        var context = await decoratedFactory.CreateDbContextAsync(cancellationToken);
 1266328        SetTenantId(context);
 1266329        return context;
 1266330    }
 31
 32    private void SetTenantId(TDbContext context)
 33    {
 1266334        if (context is ElsaDbContextBase elsaContext)
 1266335            elsaContext.TenantId = tenantAccessor.Tenant?.Id.NullIfEmpty();
 1266336    }
 37}