< Summary

Information
Class: Elsa.Features.Implementations.Module
Assembly: Elsa.Features
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Features/Implementations/Module.cs
Line coverage
96%
Covered lines: 64
Uncovered lines: 2
Coverable lines: 66
Total lines: 172
Line coverage: 96.9%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Order()100%11100%
.ctor(...)100%11100%
get_Services()100%11100%
get_Properties()100%11100%
HasFeature()100%210%
HasFeature(...)100%210%
Configure(...)100%22100%
Configure(...)100%88100%
ConfigureHostedService(...)100%11100%
ConfigureHostedService(...)100%11100%
Apply()85.71%1414100%
ExcludeFeaturesWithMissingDependencies(...)100%11100%
ConfigureFeature(...)100%22100%
GetOrCreateFeature(...)100%22100%
GetFeatureTypes()100%11100%
GetDependencyTypes(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Features/Implementations/Module.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Reflection;
 3using Elsa.Extensions;
 4using Elsa.Features.Attributes;
 5using Elsa.Features.Contracts;
 6using Elsa.Features.Models;
 7using Elsa.Features.Services;
 8using Microsoft.Extensions.DependencyInjection;
 9using Microsoft.Extensions.DependencyInjection.Extensions;
 10using Microsoft.Extensions.Hosting;
 11
 12namespace Elsa.Features.Implementations;
 13
 14/// <inheritdoc />
 15public class Module : IModule
 16{
 3617    private sealed record HostedServiceDescriptor(int Order, Type Type);
 18
 319    private Dictionary<Type, IFeature> _features = new();
 320    private readonly HashSet<IFeature> _configuredFeatures = new();
 321    private readonly List<HostedServiceDescriptor> _hostedServiceDescriptors = new();
 22
 23    /// <summary>
 24    /// Constructor.
 25    /// </summary>
 326    public Module(IServiceCollection services)
 27    {
 328        Services = services;
 329    }
 30
 31    /// <inheritdoc />
 57332    public IServiceCollection Services { get; }
 33
 34    /// <inheritdoc />
 2135    public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
 36
 37    /// <inheritdoc />
 38    public bool HasFeature<T>() where T : class, IFeature
 39    {
 040        return HasFeature(typeof(T));
 41    }
 42
 43    /// <inheritdoc />
 44    public bool HasFeature(Type featureType)
 45    {
 046        return _features.ContainsKey(featureType);
 47    }
 48
 49    /// <inheritdoc />
 50    public T Configure<T>(Action<T>? configure = null) where T : class, IFeature
 51    {
 28852        return Configure(module => (T)Activator.CreateInstance(typeof(T), module)!, configure);
 53    }
 54
 55    /// <inheritdoc />
 56    public T Configure<T>(Func<IModule, T> factory, Action<T>? configure = null) where T : class, IFeature
 57    {
 21358        if (!_features.TryGetValue(typeof(T), out var feature))
 59        {
 7560            feature = factory(this);
 7561            _features[typeof(T)] = feature;
 62        }
 63
 21364        configure?.Invoke((T)feature);
 65
 21366        if (!_isApplying)
 367            return (T)feature;
 68
 21069        var dependencies = GetDependencyTypes(feature.GetType()).ToHashSet();
 249070        foreach (var dependency in dependencies.Select(GetOrCreateFeature))
 103571            ConfigureFeature(dependency);
 72
 21073        ConfigureFeature(feature);
 21074        return (T)feature;
 75    }
 76
 77    /// <inheritdoc />
 78    public IModule ConfigureHostedService<T>(int priority = 0) where T : class, IHostedService
 79    {
 1280        return ConfigureHostedService(typeof(T), priority);
 81    }
 82
 83    /// <inheritdoc />
 84    public IModule ConfigureHostedService(Type hostedServiceType, int priority = 0)
 85    {
 1286        _hostedServiceDescriptors.Add(new(priority, hostedServiceType));
 1287        return this;
 88    }
 89
 90    private bool _isApplying;
 91
 92    /// <inheritdoc />
 93    public void Apply()
 94    {
 395        _isApplying = true;
 396        var featureTypes = GetFeatureTypes();
 10597        _features = featureTypes.ToDictionary(featureType => featureType, featureType => _features.TryGetValue(featureTy
 98
 99        // Iterate over a copy of the features to avoid concurrent modification exceptions.
 108100        foreach (var feature in _features.Values.ToList())
 101        {
 102            // This will cause additional features to be added to _features.
 51103            ConfigureFeature(feature);
 104        }
 105
 106        // Filter out features that depend on other features that are not installed.
 273107        _features = ExcludeFeaturesWithMissingDependencies(_features.Values).ToDictionary(x => x.GetType(), x => x);
 108
 109        // Add hosted services in order of priority.
 42110        foreach (var hostedServiceDescriptor in _hostedServiceDescriptors.OrderBy(x => x.Order))
 12111            Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), hostedServiceDescriptor.Type))
 112
 113        // Make sure to use the complete list of features when applying them.
 276114        foreach (var feature in _features.Values)
 135115            feature.Apply();
 116
 117        // Add a registry of enabled features to the service collection for client applications to reflect on what featu
 3118        var registry = new InstalledFeatureRegistry();
 276119        foreach (var feature in _features.Values)
 120        {
 135121            var type = feature.GetType();
 135122            var name = type.Name.Replace("Feature", string.Empty);
 135123            var ns = "Elsa";
 135124            var displayName = type.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? name;
 135125            var description = type.GetCustomAttribute<DescriptionAttribute>()?.Description;
 135126            registry.Add(new(name, ns, displayName, description));
 127        }
 128
 3129        Services.AddSingleton<IInstalledFeatureRegistry>(registry);
 3130    }
 131
 132    private IEnumerable<IFeature> ExcludeFeaturesWithMissingDependencies(IEnumerable<IFeature> features)
 133    {
 3134        return
 3135            from feature in features
 135136            let featureType = feature.GetType()
 135137            let dependencyOfAttributes = featureType.GetCustomAttributes<DependencyOfAttribute>(true).ToList()
 144138            let missingDependencies = dependencyOfAttributes.Where(x => !_features.ContainsKey(x.Type)).ToList()
 135139            where missingDependencies.Count == 0
 138140            select feature;
 141    }
 142
 143    private void ConfigureFeature(IFeature feature)
 144    {
 1296145        if (_configuredFeatures.Contains(feature))
 1161146            return;
 147
 135148        feature.Configure();
 135149        feature.ConfigureHostedServices();
 135150        _features[feature.GetType()] = feature;
 135151        _configuredFeatures.Add(feature);
 135152    }
 153
 154    private IFeature GetOrCreateFeature(Type featureType)
 155    {
 1035156        return _features.TryGetValue(featureType, out var existingFeature) ? existingFeature : (IFeature)Activator.Creat
 157    }
 158
 159    private HashSet<Type> GetFeatureTypes()
 160    {
 3161        var featureTypes = _features.Keys.ToHashSet();
 3162        var featureTypesWithDependencies = featureTypes.Concat(featureTypes.SelectMany(GetDependencyTypes)).ToHashSet();
 117163        return featureTypesWithDependencies.TSort(x => x.GetCustomAttributes<DependsOnAttribute>(true).Select(dependsOn 
 164    }
 165
 166    // Recursively get dependency types.
 167    private IEnumerable<Type> GetDependencyTypes(Type type)
 168    {
 2949169        var dependencies = type.GetCustomAttributes<DependsOnAttribute>(true).Select(dependsOn => dependsOn.Type).ToList
 1581170        return dependencies.Concat(dependencies.SelectMany(GetDependencyTypes));
 171    }
 172}