using System.ComponentModel; using System.Reflection; using Elsa.Extensions; using Elsa.Features.Attributes; using Elsa.Features.Contracts; using Elsa.Features.Models; using Elsa.Features.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; namespace Elsa.Features.Implementations; /// public class Module : IModule { private record HostedServiceDescriptor(int Order, Type Type); private IDictionary _features = new Dictionary(); private readonly ISet _configuredFeatures = new HashSet(); private readonly ICollection _hostedServiceDescriptors = new List(); /// /// Constructor. /// public Module(IServiceCollection services) { Services = services; } /// public IServiceCollection Services { get; } /// public IDictionary Properties { get; } = new Dictionary(); /// public T Configure(Action? configure = default) where T : class, IFeature => Configure(module => (T)Activator.CreateInstance(typeof(T), module)!, configure); /// public T Configure(Func factory, Action? configure = default) where T : class, IFeature { if (!_features.TryGetValue(typeof(T), out var feature)) { feature = factory(this); _features[typeof(T)] = feature; } configure?.Invoke((T)feature); if (!_isApplying) return (T)feature; var dependencies = GetDependencyTypes(feature.GetType()).ToHashSet(); foreach (var dependency in dependencies.Select(GetOrCreateFeature)) ConfigureFeature(dependency); ConfigureFeature(feature); return (T)feature; } /// public IModule ConfigureHostedService(int priority = 0) where T : class, IHostedService { return ConfigureHostedService(typeof(T), priority); } /// public IModule ConfigureHostedService(Type hostedServiceType, int priority = 0) { _hostedServiceDescriptors.Add(new HostedServiceDescriptor(priority, hostedServiceType)); return this; } private bool _isApplying; /// public void Apply() { _isApplying = true; var featureTypes = GetFeatureTypes(); _features = featureTypes.ToDictionary(featureType => featureType, featureType => _features.TryGetValue(featureType, out var existingFeature) ? existingFeature : (IFeature)Activator.CreateInstance(featureType, this)!); // Iterate over a copy of the features to avoid concurrent modification exceptions. foreach (var feature in _features.Values.ToList()) { // This will cause additional features to be added to _features. ConfigureFeature(feature); } foreach (var hostedServiceDescriptor in _hostedServiceDescriptors.OrderBy(x => x.Order)) Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), hostedServiceDescriptor.Type)); // Make sure to use the complete list of features when applying them. foreach (var feature in _features.Values) feature.Apply(); // Add a registry of enabled features to the service collection for client applications to reflect on what features are installed. var registry = new InstalledFeatureRegistry(); foreach (var feature in _features.Values) { var type = feature.GetType(); var name = type.Name.Replace("Feature", string.Empty); var ns = "Elsa"; var displayName = type.GetCustomAttribute()?.DisplayName ?? name; var description = type.GetCustomAttribute()?.Description; registry.Add(new FeatureDescriptor(name, ns, displayName, description)); } Services.AddSingleton(registry); } private void ConfigureFeature(IFeature feature) { if (_configuredFeatures.Contains(feature)) return; feature.Configure(); feature.ConfigureHostedServices(); _features[feature.GetType()] = feature; _configuredFeatures.Add(feature); } private IFeature GetOrCreateFeature(Type featureType) { return _features.TryGetValue(featureType, out var existingFeature) ? existingFeature : (IFeature)Activator.CreateInstance(featureType, this)!; } private ISet GetFeatureTypes() { var featureTypes = _features.Keys.ToHashSet(); var featureTypesWithDependencies = featureTypes.Concat(featureTypes.SelectMany(GetDependencyTypes)).ToHashSet(); return featureTypesWithDependencies.TSort(x => x.GetCustomAttributes().Select(dependsOn => dependsOn.Type)).ToHashSet(); } // Recursively get dependency types. private IEnumerable GetDependencyTypes(Type type) { var dependencies = type.GetCustomAttributes().Select(dependsOn => dependsOn.Type).ToList(); return dependencies.Concat(dependencies.SelectMany(GetDependencyTypes)); } }