< Summary

Information
Class: Elsa.Common.Features.MultitenancyFeature
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Features/MultitenancyFeature.cs
Line coverage
88%
Covered lines: 31
Uncovered lines: 4
Coverable lines: 35
Total lines: 62
Line coverage: 88.5%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
UseTenantsProvider()0%620%
UseTenantsProvider(...)100%210%
ConfigureHostedServices()100%11100%
Apply()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Features/MultitenancyFeature.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Common.Multitenancy.EventHandlers;
 3using Elsa.Common.Multitenancy.HostedServices;
 4using Elsa.Common.RecurringTasks;
 5using Elsa.Features.Abstractions;
 6using Elsa.Features.Services;
 7using Microsoft.Extensions.DependencyInjection;
 8using Microsoft.Extensions.DependencyInjection.Extensions;
 9
 10namespace Elsa.Common.Features;
 11
 112public class MultitenancyFeature(IModule module) : FeatureBase(module)
 13{
 314    private Func<IServiceProvider, ITenantsProvider> _tenantsProviderFactory = sp => sp.GetRequiredService<DefaultTenant
 15
 16    public MultitenancyFeature UseTenantsProvider<TTenantsProvider>() where TTenantsProvider : class, ITenantsProvider
 17    {
 018        Services.TryAddScoped<TTenantsProvider>();
 019        return UseTenantsProvider(sp => sp.GetRequiredService<TTenantsProvider>());
 20    }
 21
 22    public MultitenancyFeature UseTenantsProvider(Func<IServiceProvider, ITenantsProvider> tenantsProviderFactory)
 23    {
 024        _tenantsProviderFactory = tenantsProviderFactory;
 025        return this;
 26    }
 27
 28    public override void ConfigureHostedServices()
 29    {
 130        Module
 131            .ConfigureHostedService<ActivateTenants>(-1)
 132            ;
 133    }
 34
 35    public override void Apply()
 36    {
 137        Services
 138            .AddSingleton<ITenantScopeFactory, DefaultTenantScopeFactory>()
 139            .AddSingleton<ITenantAccessor, DefaultTenantAccessor>()
 140            .AddSingleton<ITenantFinder, DefaultTenantFinder>()
 141            .AddSingleton<ITenantService, DefaultTenantService>()
 142
 143            // Order is important: Startup task first, then background and recurring tasks.
 144            .AddSingleton<ITenantActivatedEvent, RunStartupTasks>()
 145
 146            .AddSingleton<RunBackgroundTasks>()
 147            .AddSingleton<ITenantActivatedEvent>(sp => sp.GetRequiredService<RunBackgroundTasks>())
 148            .AddSingleton<ITenantDeactivatedEvent>(sp => sp.GetRequiredService<RunBackgroundTasks>())
 149
 150            .AddSingleton<StartRecurringTasks>()
 151            .AddSingleton<ITenantActivatedEvent>(sp => sp.GetRequiredService<StartRecurringTasks>())
 152            .AddSingleton<ITenantDeactivatedEvent>(sp => sp.GetRequiredService<StartRecurringTasks>())
 153
 154            .AddSingleton<RecurringTaskScheduleManager>()
 155            .AddSingleton<TenantEventsManager>()
 156            .AddScoped<DefaultTenantsProvider>()
 157            .AddScoped<DefaultTenantResolver>()
 158            .AddScoped<ITaskExecutor, TaskExecutor>()
 159            .AddScoped<IBackgroundTaskStarter, TaskExecutor>()
 160            .AddScoped(_tenantsProviderFactory);
 161    }
 62}