Refactored multiple classes to align with modern C# coding practices such as object initializers and nullable type handling. Added a new base class for checklist dropdown providers to enhance UI configuration extensibility. Simplified constructors for feature classes by adopting record-like syntax, improving readability and maintainability.
50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System.Collections.Concurrent;
|
|
using Elsa.Features;
|
|
using Elsa.Features.Services;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Elsa.Extensions;
|
|
|
|
/// <summary>
|
|
/// Provides extension methods to <see cref="IServiceCollection"/>.
|
|
/// </summary>
|
|
public static class ModuleExtensions
|
|
{
|
|
private static readonly IDictionary<IServiceCollection, IModule> Modules = new ConcurrentDictionary<IServiceCollection, IModule>();
|
|
|
|
/// <summary>
|
|
/// Creates a new Elsa module and adds the <see cref="ElsaFeature"/> to it.
|
|
/// </summary>
|
|
public static IModule AddElsa(this IServiceCollection services, Action<IModule>? configure = null)
|
|
{
|
|
var module = services.GetOrCreateModule();
|
|
module.Configure<AppFeature>(app => app.Configurator += configure);
|
|
module.Apply();
|
|
|
|
return module;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures the Elsa module.
|
|
/// </summary>
|
|
public static IModule ConfigureElsa(this IServiceCollection services, Action<IModule>? configure = null)
|
|
{
|
|
var module = services.GetOrCreateModule();
|
|
|
|
if(configure != null)
|
|
module.Configure<AppFeature>(app => app.Configurator += configure);
|
|
|
|
return module;
|
|
}
|
|
|
|
private static IModule GetOrCreateModule(this IServiceCollection services)
|
|
{
|
|
if(Modules.TryGetValue(services, out var module))
|
|
return module;
|
|
|
|
module = services.CreateModule();
|
|
|
|
Modules[services] = module;
|
|
return module;
|
|
}
|
|
} |