< Summary

Information
Class: Elsa.Common.Multitenancy.DefaultTenantAccessor
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Multitenancy/Implementations/DefaultTenantAccessor.cs
Line coverage
100%
Covered lines: 5
Uncovered lines: 0
Coverable lines: 5
Total lines: 41
Line coverage: 100%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_TenantId()50%22100%
get_Tenant()100%11100%
set_Tenant(...)100%11100%
PushContext(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Multitenancy/Implementations/DefaultTenantAccessor.cs

#LineLine coverage
 1namespace Elsa.Common.Multitenancy;
 2
 3/// <summary>
 4/// Default implementation of <see cref="ITenantAccessor"/>.
 5/// </summary>
 6public class DefaultTenantAccessor : ITenantAccessor
 7{
 18    private static readonly AsyncLocal<Tenant?> CurrentTenantField = new();
 9
 12635210    public string TenantId => (Tenant?.Id).NormalizeTenantId();
 11
 12    /// <inheritdoc/>
 13    public Tenant? Tenant
 14    {
 24348115        get => CurrentTenantField.Value;
 114416        private set => CurrentTenantField.Value = value;
 17    }
 18
 19    public IDisposable PushContext(Tenant? tenant)
 20    {
 173021        return new CurrentTenantScope(tenant, Tenant, t => Tenant = t);
 22    }
 23}
 24
 25public class CurrentTenantScope : IDisposable
 26{
 27    private readonly Tenant? _previousTenant;
 28    private readonly Action<Tenant?> _apply;
 29
 30    public CurrentTenantScope(Tenant? newTenant, Tenant? previousTenant, Action<Tenant?> apply)
 31    {
 32        _previousTenant = previousTenant;
 33        _apply = apply;
 34        _apply(newTenant);
 35    }
 36
 37    public void Dispose()
 38    {
 39        _apply(_previousTenant);
 40    }
 41}