< Summary

Information
Class: Elsa.Common.Multitenancy.CurrentTenantScope
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: 7
Uncovered lines: 0
Coverable lines: 7
Total lines: 41
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Dispose()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{
 8    private static readonly AsyncLocal<Tenant?> CurrentTenantField = new();
 9
 10    public string TenantId => (Tenant?.Id).NormalizeTenantId();
 11
 12    /// <inheritdoc/>
 13    public Tenant? Tenant
 14    {
 15        get => CurrentTenantField.Value;
 16        private set => CurrentTenantField.Value = value;
 17    }
 18
 19    public IDisposable PushContext(Tenant? tenant)
 20    {
 21        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
 58630    public CurrentTenantScope(Tenant? newTenant, Tenant? previousTenant, Action<Tenant?> apply)
 31    {
 58632        _previousTenant = previousTenant;
 58633        _apply = apply;
 58634        _apply(newTenant);
 58635    }
 36
 37    public void Dispose()
 38    {
 55839        _apply(_previousTenant);
 55840    }
 41}