< 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: 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
.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    /// <inheritdoc/>
 11    public Tenant? Tenant
 12    {
 13        get => CurrentTenantField.Value;
 14        private set => CurrentTenantField.Value = value;
 15    }
 16
 17    public IDisposable PushContext(Tenant? tenant)
 18    {
 19        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
 328    public CurrentTenantScope(Tenant? newTenant, Tenant? previousTenant, Action<Tenant?> apply)
 29    {
 330        _previousTenant = previousTenant;
 331        _apply = apply;
 332        _apply(newTenant);
 333    }
 34
 35    public void Dispose()
 36    {
 237        _apply(_previousTenant);
 238    }
 39}