| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Features.Attributes; |
| | | 5 | | using Elsa.Features.Contracts; |
| | | 6 | | using Elsa.Features.Models; |
| | | 7 | | using Elsa.Features.Services; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | using Microsoft.Extensions.Hosting; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Features.Implementations; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public class Module : IModule |
| | | 16 | | { |
| | 66 | 17 | | private sealed record HostedServiceDescriptor(int Order, Type Type); |
| | | 18 | | |
| | 13 | 19 | | private Dictionary<Type, IFeature> _features = new(); |
| | 13 | 20 | | private readonly HashSet<IFeature> _configuredFeatures = new(); |
| | 13 | 21 | | private readonly List<HostedServiceDescriptor> _hostedServiceDescriptors = new(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Constructor. |
| | | 25 | | /// </summary> |
| | 13 | 26 | | public Module(IServiceCollection services) |
| | | 27 | | { |
| | 13 | 28 | | Services = services; |
| | 13 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | 822 | 32 | | public IServiceCollection Services { get; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 68 | 35 | | public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>(); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public bool HasFeature<T>() where T : class, IFeature |
| | | 39 | | { |
| | 0 | 40 | | return HasFeature(typeof(T)); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public bool HasFeature(Type featureType) |
| | | 45 | | { |
| | 0 | 46 | | return _features.ContainsKey(featureType); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public T Configure<T>(Action<T>? configure = null) where T : class, IFeature |
| | | 51 | | { |
| | 338 | 52 | | 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 | | { |
| | 238 | 58 | | if (!_features.TryGetValue(typeof(T), out var feature)) |
| | | 59 | | { |
| | 100 | 60 | | feature = factory(this); |
| | 100 | 61 | | _features[typeof(T)] = feature; |
| | | 62 | | } |
| | | 63 | | |
| | 238 | 64 | | configure?.Invoke((T)feature); |
| | | 65 | | |
| | 238 | 66 | | if (!_isApplying) |
| | 13 | 67 | | return (T)feature; |
| | | 68 | | |
| | 225 | 69 | | var dependencies = GetDependencyTypes(feature.GetType()).ToHashSet(); |
| | 2966 | 70 | | foreach (var dependency in dependencies.Select(GetOrCreateFeature)) |
| | 1258 | 71 | | ConfigureFeature(dependency); |
| | | 72 | | |
| | 225 | 73 | | ConfigureFeature(feature); |
| | 225 | 74 | | return (T)feature; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public IModule ConfigureHostedService<T>(int priority = 0) where T : class, IHostedService |
| | | 79 | | { |
| | 22 | 80 | | return ConfigureHostedService(typeof(T), priority); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public IModule ConfigureHostedService(Type hostedServiceType, int priority = 0) |
| | | 85 | | { |
| | 22 | 86 | | _hostedServiceDescriptors.Add(new(priority, hostedServiceType)); |
| | 22 | 87 | | return this; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private bool _isApplying; |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public void Apply() |
| | | 94 | | { |
| | 13 | 95 | | _isApplying = true; |
| | 13 | 96 | | var featureTypes = GetFeatureTypes(); |
| | 137 | 97 | | _features = featureTypes.ToDictionary(featureType => featureType, featureType => _features.TryGetValue(featureTy |
| | | 98 | | |
| | | 99 | | // Iterate over a copy of the features to avoid concurrent modification exceptions. |
| | 150 | 100 | | foreach (var feature in _features.Values.ToList()) |
| | | 101 | | { |
| | | 102 | | // This will cause additional features to be added to _features. |
| | 62 | 103 | | ConfigureFeature(feature); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | // Filter out features that depend on other features that are not installed. |
| | 317 | 107 | | _features = ExcludeFeaturesWithMissingDependencies(_features.Values).ToDictionary(x => x.GetType(), x => x); |
| | | 108 | | |
| | | 109 | | // Hosted services are registered after the features have been applied, because applying a feature can contribut |
| | | 110 | | // at this position in the service collection though, ahead of whatever the features register from their Apply m |
| | 13 | 111 | | var hostedServiceIndex = Services.Count; |
| | | 112 | | |
| | | 113 | | // Make sure to use the complete list of features when applying them. Applying a feature can introduce additiona |
| | | 114 | | // Module.Configure<T>() from its Apply method, directly or through a helper such as AddActivity<T>()), so keep |
| | 13 | 115 | | var appliedFeatures = new HashSet<IFeature>(); |
| | 35 | 116 | | while (GetFeaturesPendingApply(appliedFeatures) is { Count: > 0 } pendingFeatures) |
| | | 117 | | { |
| | 22 | 118 | | appliedFeatures.UnionWith(pendingFeatures); |
| | | 119 | | |
| | 368 | 120 | | foreach (var feature in pendingFeatures) |
| | 162 | 121 | | feature.Apply(); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // Add hosted services in order of priority. |
| | 13 | 125 | | RegisterHostedServices(hostedServiceIndex); |
| | | 126 | | |
| | | 127 | | // Add a registry of enabled features to the service collection for client applications to reflect on what featu |
| | 13 | 128 | | var registry = new InstalledFeatureRegistry(); |
| | 350 | 129 | | foreach (var feature in _features.Values) |
| | | 130 | | { |
| | 162 | 131 | | var type = feature.GetType(); |
| | 162 | 132 | | var name = type.Name.Replace("Feature", string.Empty); |
| | 162 | 133 | | var ns = "Elsa"; |
| | 162 | 134 | | var displayName = type.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? name; |
| | 162 | 135 | | var description = type.GetCustomAttribute<DescriptionAttribute>()?.Description; |
| | 162 | 136 | | registry.Add(new(name, ns, displayName, description)); |
| | | 137 | | } |
| | | 138 | | |
| | 13 | 139 | | Services.AddSingleton<IInstalledFeatureRegistry>(registry); |
| | 16 | 140 | | Services.AddSingleton<IInstalledFeatureProvider>(sp => new InstalledFeatureProvider(sp.GetRequiredService<IInsta |
| | 13 | 141 | | } |
| | | 142 | | |
| | | 143 | | private IEnumerable<IFeature> ExcludeFeaturesWithMissingDependencies(IEnumerable<IFeature> features) |
| | | 144 | | { |
| | 13 | 145 | | return |
| | 13 | 146 | | from feature in features |
| | 152 | 147 | | let featureType = feature.GetType() |
| | 152 | 148 | | let dependencyOfAttributes = featureType.GetCustomAttributes<DependencyOfAttribute>(true).ToList() |
| | 161 | 149 | | let missingDependencies = dependencyOfAttributes.Where(x => !_features.ContainsKey(x.Type)).ToList() |
| | 152 | 150 | | where missingDependencies.Count == 0 |
| | 165 | 151 | | select feature; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | // Registers the configured hosted services in order of priority, starting at the specified index. |
| | | 155 | | private void RegisterHostedServices(int index) |
| | | 156 | | { |
| | 13 | 157 | | var appendIndex = Services.Count; |
| | | 158 | | |
| | 92 | 159 | | foreach (var hostedServiceDescriptor in _hostedServiceDescriptors.OrderBy(x => x.Order)) |
| | 22 | 160 | | Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), hostedServiceDescriptor.Type)) |
| | | 161 | | |
| | | 162 | | // TryAddEnumerable appends, so move what it added back to where hosted services configured through this module |
| | | 163 | | // than inserting directly keeps the de-duplication behaviour of TryAddEnumerable, which also considers what the |
| | 13 | 164 | | var appendedDescriptors = Services.Skip(appendIndex).ToList(); |
| | | 165 | | |
| | 70 | 166 | | for (var i = 0; i < appendedDescriptors.Count; i++) |
| | 22 | 167 | | Services.RemoveAt(appendIndex); |
| | | 168 | | |
| | 70 | 169 | | for (var i = 0; i < appendedDescriptors.Count; i++) |
| | 22 | 170 | | Services.Insert(index + i, appendedDescriptors[i]); |
| | 13 | 171 | | } |
| | | 172 | | |
| | | 173 | | // Returns the features that have not been applied yet, sorted so that dependencies are applied before the features |
| | | 174 | | private List<IFeature> GetFeaturesPendingApply(IReadOnlySet<IFeature> appliedFeatures) |
| | | 175 | | { |
| | 531 | 176 | | var pendingFeatureTypes = _features.Where(x => !appliedFeatures.Contains(x.Value)).Select(x => x.Key).ToList(); |
| | 35 | 177 | | var pendingFeatureTypeLookup = pendingFeatureTypes.ToHashSet(); |
| | | 178 | | |
| | | 179 | | // Sorting pulls in dependencies that are not pending themselves, so filter those out again. |
| | 35 | 180 | | return pendingFeatureTypes |
| | 35 | 181 | | .TSort(GetDeclaredDependencyTypes) |
| | 35 | 182 | | .Where(pendingFeatureTypeLookup.Contains) |
| | 162 | 183 | | .Select(x => _features[x]) |
| | 35 | 184 | | .Distinct() |
| | 35 | 185 | | .ToList(); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private void ConfigureFeature(IFeature feature) |
| | | 189 | | { |
| | 1545 | 190 | | if (_configuredFeatures.Contains(feature)) |
| | 1383 | 191 | | return; |
| | | 192 | | |
| | 162 | 193 | | feature.Configure(); |
| | 162 | 194 | | feature.ConfigureHostedServices(); |
| | 162 | 195 | | _features[feature.GetType()] = feature; |
| | 162 | 196 | | _configuredFeatures.Add(feature); |
| | 162 | 197 | | } |
| | | 198 | | |
| | | 199 | | private IFeature GetOrCreateFeature(Type featureType) |
| | | 200 | | { |
| | 1258 | 201 | | return _features.TryGetValue(featureType, out var existingFeature) ? existingFeature : (IFeature)Activator.Creat |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private HashSet<Type> GetFeatureTypes() |
| | | 205 | | { |
| | 13 | 206 | | var featureTypes = _features.Keys.ToHashSet(); |
| | 13 | 207 | | var featureTypesWithDependencies = featureTypes.Concat(featureTypes.SelectMany(GetDependencyTypes)).ToHashSet(); |
| | 13 | 208 | | return featureTypesWithDependencies.TSort(GetDeclaredDependencyTypes).ToHashSet(); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | private static IEnumerable<Type> GetDeclaredDependencyTypes(Type type) |
| | | 212 | | { |
| | 4051 | 213 | | return type.GetCustomAttributes<DependsOnAttribute>(true).Select(dependsOn => dependsOn.Type); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | // Recursively get dependency types. |
| | | 217 | | private IEnumerable<Type> GetDependencyTypes(Type type) |
| | | 218 | | { |
| | 1908 | 219 | | var dependencies = GetDeclaredDependencyTypes(type).ToList(); |
| | 1908 | 220 | | return dependencies.Concat(dependencies.SelectMany(GetDependencyTypes)); |
| | | 221 | | } |
| | | 222 | | } |