2022-05-26 10:47:31 +00:00
|
|
|
using System.Reflection;
|
2022-05-27 10:36:39 +00:00
|
|
|
using Elsa.Features.Attributes;
|
2022-08-14 17:38:58 +00:00
|
|
|
using Elsa.Features.Extensions;
|
2022-05-27 10:36:39 +00:00
|
|
|
using Elsa.Features.Services;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
namespace Elsa.Features.Implementations;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
public class Module : IModule
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
|
|
|
|
private record HostedServiceDescriptor(int Order, Type HostedServiceType);
|
|
|
|
|
|
2022-08-11 10:45:07 +00:00
|
|
|
private readonly ISet<IFeature> _features = new HashSet<IFeature>();
|
2022-05-26 10:47:31 +00:00
|
|
|
private readonly ICollection<HostedServiceDescriptor> _hostedServiceDescriptors = new List<HostedServiceDescriptor>();
|
|
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
public Module(IServiceCollection services)
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
|
|
|
|
Services = services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IServiceCollection Services { get; }
|
|
|
|
|
|
2022-08-14 17:38:58 +00:00
|
|
|
public T Configure<T>(Action<T>? configure = default) where T : class, IFeature => Configure(module => (T)Activator.CreateInstance(typeof(T), module)!, configure);
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
public T Configure<T>(Func<IModule, T> factory, Action<T>? configure = default) where T : class, IFeature
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2022-08-14 17:38:58 +00:00
|
|
|
if (_features.FirstOrDefault(x => x is T) is not T feature)
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2022-08-14 17:38:58 +00:00
|
|
|
feature = factory(this);
|
|
|
|
|
_features.Add(feature);
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-14 17:38:58 +00:00
|
|
|
configure?.Invoke(feature);
|
|
|
|
|
return feature;
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
public IModule ConfigureHostedService<T>(int priority = 0)
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
|
|
|
|
_hostedServiceDescriptors.Add(new HostedServiceDescriptor(priority, typeof(T)));
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Apply()
|
|
|
|
|
{
|
2022-08-14 17:38:58 +00:00
|
|
|
var featureTypes = _features.Select(x => x.GetType()).TSort(x => x.GetCustomAttributes<DependsOn>().Select(dependsOn => dependsOn.Type)).ToList();
|
|
|
|
|
var features = featureTypes.Select(featureType => _features.FirstOrDefault(x => x.GetType() == featureType) ?? (IFeature)Activator.CreateInstance(featureType, this)!).ToList();
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2022-08-14 17:38:58 +00:00
|
|
|
foreach (var feature in features)
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2022-08-11 10:45:07 +00:00
|
|
|
feature.Configure();
|
|
|
|
|
feature.ConfigureHostedServices();
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var hostedServiceDescriptor in _hostedServiceDescriptors.OrderBy(x => x.Order))
|
|
|
|
|
Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), hostedServiceDescriptor.HostedServiceType));
|
|
|
|
|
|
2022-08-14 17:38:58 +00:00
|
|
|
foreach (var feature in features)
|
|
|
|
|
feature.Apply();
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
}
|