< Summary

Information
Class: Elsa.Extensions.WorkflowDictionaryExtensions
Assembly: Elsa.Workflows.Runtime
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Extensions/WorkflowDictionaryExtensions.cs
Line coverage
88%
Covered lines: 23
Uncovered lines: 3
Coverable lines: 26
Total lines: 62
Line coverage: 88.4%
Branch coverage
90%
Covered branches: 20
Total branches: 22
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Add(...)100%210%
Add(...)91.66%121294.44%
ValidateWorkflowType(...)100%88100%
GetDisplayName(...)50%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Runtime/Extensions/WorkflowDictionaryExtensions.cs

#LineLine coverage
 1using Elsa.Workflows;
 2using Microsoft.Extensions.DependencyInjection;
 3
 4// ReSharper disable once CheckNamespace
 5namespace Elsa.Extensions;
 6
 7/// <summary>
 8/// Extension methods for <see cref="IDictionary{TKey,TValue}"/>.
 9/// </summary>
 10public static class WorkflowDictionaryExtensions
 11{
 12    /// <summary>
 13    /// Register a workflow factory that creates an instance of the specified workflow type.
 14    /// </summary>
 15    public static void Add<TWorkflow>(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary)
 16    {
 017        dictionary.Add(typeof(TWorkflow));
 018    }
 19
 20    /// <summary>
 21    /// Register a workflow factory that creates an instance of the specified workflow type.
 22    /// </summary>
 23    public static void Add(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary, Type workf
 24    {
 23225        ValidateWorkflowType(workflowType);
 26
 22327        if (string.IsNullOrWhiteSpace(workflowType.FullName))
 028            throw new ArgumentException($"Workflow type '{workflowType.Name}' must have a full name.", nameof(workflowTy
 29
 22330        var key = workflowType.GetSimpleAssemblyQualifiedName();
 22331        var legacyKey = workflowType.FullName;
 22332        var hasFactory = dictionary.TryGetValue(key, out var factory);
 33
 22334        if (!hasFactory && !string.IsNullOrWhiteSpace(legacyKey))
 22335            hasFactory = dictionary.TryGetValue(legacyKey, out factory);
 36
 22337        if (!hasFactory)
 38        {
 22239            factory = sp =>
 22240            {
 236141                var workflow = (IWorkflow)ActivatorUtilities.GetServiceOrCreateInstance(sp, workflowType);
 236142                return new ValueTask<IWorkflow>(workflow);
 22243            };
 44        }
 45
 22346        dictionary[key] = factory!;
 47
 22348        if (!string.IsNullOrWhiteSpace(legacyKey) && legacyKey != key)
 22349            dictionary[legacyKey] = factory!;
 22350    }
 51
 52    private static void ValidateWorkflowType(Type workflowType)
 53    {
 23254        if (!typeof(IWorkflow).IsAssignableFrom(workflowType))
 355            throw new ArgumentException($"Workflow type '{GetDisplayName(workflowType)}' must implement {nameof(IWorkflo
 56
 22957        if (workflowType.IsAbstract || workflowType.IsInterface || workflowType.ContainsGenericParameters)
 658            throw new ArgumentException($"Workflow type '{GetDisplayName(workflowType)}' must be a concrete, closed type
 22359    }
 60
 961    private static string GetDisplayName(Type type) => type.FullName ?? type.Name;
 62}