< 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: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 39
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
.cctor()100%11100%
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
 10    /// <inheritdoc/>
 11    public Tenant? Tenant
 12    {
 2325013        get => CurrentTenantField.Value;
 514        private set => CurrentTenantField.Value = value;
 15    }
 16
 17    public IDisposable PushContext(Tenant? tenant)
 18    {
 819        return new CurrentTenantScope(tenant, Tenant, t => Tenant = t);
 20    }
 21}
 22
 23public class CurrentTenantScope : IDisposable
 24{
 25    private readonly Tenant? _previousTenant;
 26    private readonly Action<Tenant?> _apply;
 27
 28    public CurrentTenantScope(Tenant? newTenant, Tenant? previousTenant, Action<Tenant?> apply)
 29    {
 30        _previousTenant = previousTenant;
 31        _apply = apply;
 32        _apply(newTenant);
 33    }
 34
 35    public void Dispose()
 36    {
 37        _apply(_previousTenant);
 38    }
 39}