< Summary

Information
Class: Elsa.Extensions.OutputConverterServiceCollectionExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/OutputConverterServiceCollectionExtensions.cs
Line coverage
86%
Covered lines: 20
Uncovered lines: 3
Coverable lines: 23
Total lines: 53
Line coverage: 86.9%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddOutputConverter(...)100%11100%
ValidateUniqueId(...)50%4470%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/OutputConverterServiceCollectionExtensions.cs

#LineLine coverage
 1using Elsa.Workflows;
 2using Elsa.Workflows.Models;
 3using Microsoft.Extensions.DependencyInjection;
 4using Microsoft.Extensions.DependencyInjection.Extensions;
 5
 6namespace Elsa.Extensions;
 7
 8/// <summary>
 9/// Adds output converter registrations to a service collection.
 10/// </summary>
 11public static class OutputConverterServiceCollectionExtensions
 12{
 13    /// <summary>
 14    /// Registers a discoverable output converter using its stable Converter ID as the keyed-service key.
 15    /// </summary>
 16    public static IServiceCollection AddOutputConverter<TConverter>(
 17        this IServiceCollection services,
 18        OutputConverterDescriptor descriptor,
 19        ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
 20        where TConverter : class, IOutputConverter
 21    {
 822        ArgumentNullException.ThrowIfNull(services);
 823        ArgumentNullException.ThrowIfNull(descriptor);
 24
 825        OutputConverterRegistry.ValidateDescriptor(descriptor);
 826        ValidateUniqueId(services, descriptor.Id);
 27
 828        var registration = new OutputConverterRegistration(descriptor, descriptor.Id, serviceLifetime);
 829        services.AddSingleton(registration);
 830        services.Add(ServiceDescriptor.DescribeKeyed(
 831            typeof(IOutputConverter),
 832            descriptor.Id,
 833            typeof(TConverter),
 834            serviceLifetime));
 835        services.TryAddSingleton<IOutputConverterRegistry, OutputConverterRegistry>();
 836        return services;
 37    }
 38
 39    private static void ValidateUniqueId(IServiceCollection services, string id)
 40    {
 841        if (string.IsNullOrWhiteSpace(id))
 042            throw new ArgumentException("Output converter IDs cannot be empty.", nameof(id));
 43
 844        var existingRegistration = services
 301245            .Where(x => x.ServiceType == typeof(OutputConverterRegistration))
 046            .Select(x => x.ImplementationInstance)
 847            .OfType<OutputConverterRegistration>()
 848            .FirstOrDefault(x => string.Equals(x.Descriptor.Id, id, StringComparison.OrdinalIgnoreCase));
 49
 850        if (existingRegistration != null)
 051            throw new InvalidOperationException($"Output converter ID '{id}' is already registered or differs from '{exi
 852    }
 53}