elsa-core/src/core/Elsa.Runtime/Extensions/ServiceCollectionExtensions.cs
2022-05-03 16:17:20 +02:00

31 lines
1.9 KiB
C#

using System.Threading.Channels;
using Elsa.Options;
using Elsa.Runtime.Options;
using Elsa.Runtime.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Runtime.Extensions;
public static class ServiceCollectionExtensions
{
public static ElsaOptionsConfigurator ConfigureElsaRuntime(this ElsaOptionsConfigurator configurator, Action<ElsaRuntimeOptions>? configure = default)
{
configurator.Configure(configure);
return configurator;
}
public static IServiceCollection CreateChannel<T>(this IServiceCollection services) =>
services
.AddSingleton(CreateChannel<T>())
.AddSingleton(CreateChannelReader<T>)
.AddSingleton(CreateChannelWriter<T>);
private static Channel<T> CreateChannel<T>() => Channel.CreateUnbounded<T>(new UnboundedChannelOptions());
private static ChannelReader<T> CreateChannelReader<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequiredService<Channel<T>>().Reader;
private static ChannelWriter<T> CreateChannelWriter<T>(IServiceProvider serviceProvider) => serviceProvider.GetRequiredService<Channel<T>>().Writer;
public static IServiceCollection AddWorkflowDefinitionProvider<T>(this IServiceCollection services) where T : class, IWorkflowDefinitionProvider => services.AddSingleton<IWorkflowDefinitionProvider, T>();
public static IServiceCollection AddStimulusHandler<T>(this IServiceCollection services) where T : class, IStimulusHandler => services.AddSingleton<IStimulusHandler, T>();
public static IServiceCollection AddInstructionInterpreter<T>(this IServiceCollection services) where T : class, IWorkflowInstructionInterpreter => services.AddSingleton<IWorkflowInstructionInterpreter, T>();
public static IServiceCollection ConfigureWorkflowRuntime(this IServiceCollection services, Action<ElsaRuntimeOptions> configure) => services.Configure(configure);
}