< 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{
 917    private sealed record HostedServiceDescriptor(int Order, Type Type);
 18
 119    private Dictionary<Type, IFeature> _features = new();
 120    private readonly HashSet<IFeature> _configuredFeatures = new();
 121    private readonly List<HostedServiceDescriptor> _hostedServiceDescriptors = new();
 22
 23    /// <summary>
 24    /// Constructor.
 25    /// </summary>
 126    public Module(IServiceCollection services)
 27    {
 128        Services = services;
 129    }
 30
 31    /// <inheritdoc />
 16732    public IServiceCollection Services { get; }
 33
 34    /// <inheritdoc />
 735    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    {
 8952        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    {
 6458        if (!_features.TryGetValue(typeof(T), out var feature))
 59        {
 2560            feature = factory(this);
 2561            _features[typeof(T)] = feature;
 62        }
 63
 6464        configure?.Invoke((T)feature);
 65
 6466        if (!_isApplying)
 167            return (T)feature;
 68
 6369        var dependencies = GetDependencyTypes(feature.GetType()).ToHashSet();
 76870        foreach (var dependency in dependencies.Select(GetOrCreateFeature))
 32171            ConfigureFeature(dependency);
 72
 6373        ConfigureFeature(feature);
 6374        return (T)feature;
 75    }
 76
 77    /// <inheritdoc />
 78    public IModule ConfigureHostedService<T>(int priority = 0) where T : class, IHostedService
 79    {
 380        return ConfigureHostedService(typeof(T), priority);
 81    }
 82
 83    /// <inheritdoc />
 84    public IModule ConfigureHostedService(Type hostedServiceType, int priority = 0)
 85    {
 386        _hostedServiceDescriptors.Add(new(priority, hostedServiceType));
 387        return this;
 88    }
 89
 90    private bool _isApplying;
 91
 92    /// <inheritdoc />
 93    public void Apply()
 94    {
 195        _isApplying = true;
 196        var featureTypes = GetFeatureTypes();
 3597        _features = featureTypes.ToDictionary(featureType => featureType, featureType => _features.TryGetValue(featureTy
 98
 99        // Iterate over a copy of the features to avoid concurrent modification exceptions.
 36100        foreach (var feature in _features.Values.ToList())
 101        {
 102            // This will cause additional features to be added to _features.
 17103            ConfigureFeature(feature);
 104        }
 105
 106        // Filter out features that depend on other features that are not installed.
 89107        _features = ExcludeFeaturesWithMissingDependencies(_features.Values).ToDictionary(x => x.GetType(), x => x);
 108
 109        // Add hosted services in order of priority.
 11110        foreach (var hostedServiceDescriptor in _hostedServiceDescriptors.OrderBy(x => x.Order))
 3111            Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), hostedServiceDescriptor.Type))
 112
 113        // Make sure to use the complete list of features when applying them.
 90114        foreach (var feature in _features.Values)
 44115            feature.Apply();
 116
 117        // Add a registry of enabled features to the service collection for client applications to reflect on what featu
 1118        var registry = new InstalledFeatureRegistry();
 90119        foreach (var feature in _features.Values)
 120        {
 44121            var type = feature.GetType();
 44122            var name = type.Name.Replace("Feature", string.Empty);
 44123            var ns = "Elsa";
 44124            var displayName = type.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? name;
 44125            var description = type.GetCustomAttribute<DescriptionAttribute>()?.Description;
 44126            registry.Add(new(name, ns, displayName, description));
 127        }
 128
 1129        Services.AddSingleton<IInstalledFeatureRegistry>(registry);
 1130    }
 131
 132    private IEnumerable<IFeature> ExcludeFeaturesWithMissingDependencies(IEnumerable<IFeature> features)
 133    {
 1134        return
 1135            from feature in features
 44136            let featureType = feature.GetType()
 44137            let dependencyOfAttributes = featureType.GetCustomAttributes<DependencyOfAttribute>(true).ToList()
 46138            let missingDependencies = dependencyOfAttributes.Where(x => !_features.ContainsKey(x.Type)).ToList()
 44139            where missingDependencies.Count == 0
 45140            select feature;
 141    }
 142
 143    private void ConfigureFeature(IFeature feature)
 144    {
 401145        if (_configuredFeatures.Contains(feature))
 357146            return;
 147
 44148        feature.Configure();
 44149        feature.ConfigureHostedServices();
 44150        _features[feature.GetType()] = feature;
 44151        _configuredFeatures.Add(feature);
 44152    }
 153
 154    private IFeature GetOrCreateFeature(Type featureType)
 155    {
 321156        return _features.TryGetValue(featureType, out var existingFeature) ? existingFeature : (IFeature)Activator.Creat
 157    }
 158
 159    private HashSet<Type> GetFeatureTypes()
 160    {
 1161        var featureTypes = _features.Keys.ToHashSet();
 1162        var featureTypesWithDependencies = featureTypes.Concat(featureTypes.SelectMany(GetDependencyTypes)).ToHashSet();
 39163        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    {
 920169        var dependencies = type.GetCustomAttributes<DependsOnAttribute>(true).Select(dependsOn => dependsOn.Type).ToList
 492170        return dependencies.Concat(dependencies.SelectMany(GetDependencyTypes));
 171    }
 172}