< Summary

Information
Class: Elsa.Extensions.ServiceCollectionExtensions
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/ServiceCollectionExtensions.cs
Line coverage
50%
Covered lines: 3
Uncovered lines: 3
Coverable lines: 6
Total lines: 37
Line coverage: 50%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryAddScopedImplementation(...)100%44100%
TryAddScopedImplementation(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/ServiceCollectionExtensions.cs

#LineLine coverage
 1namespace Elsa.Extensions;
 2
 3using Microsoft.Extensions.DependencyInjection;
 4using System;
 5using System.Linq;
 6
 7public static class ServiceCollectionExtensions
 8{
 9    /// <summary>
 10    /// Adds the service with a specific implementation type only if the combination
 11    /// of service and implementation does not already exist in the service collection.
 12    /// </summary>
 13    public static IServiceCollection TryAddScopedImplementation<TService, TImplementation>(
 14        this IServiceCollection services)
 15        where TService : class
 16        where TImplementation : class, TService
 17    {
 77618        if (!services.Any(sd => sd.ServiceType == typeof(TService) && sd.ImplementationType == typeof(TImplementation)))
 219            services.AddScoped<TService, TImplementation>();
 20
 621        return services;
 22    }
 23
 24    /// <summary>
 25    /// Adds the service with a specific implementation factory only if the combination
 26    /// of service and implementation already doesn't exist.
 27    /// </summary>
 28    public static IServiceCollection TryAddScopedImplementation<TService>(
 29        this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory)
 30        where TService : class
 31    {
 032        if (services.All(sd => sd.ServiceType != typeof(TService)))
 033            services.AddScoped(implementationFactory);
 34
 035        return services;
 36    }
 37}