elsa-core/src/core/Elsa.Core/ElsaOptions.cs

134 lines
5.1 KiB
C#
Raw Normal View History

using System;
using Elsa.Caching;
using Elsa.DistributedLock;
2020-12-12 13:07:17 +00:00
using Elsa.Persistence;
using Elsa.Persistence.InMemory;
2020-10-17 18:19:44 +00:00
using Elsa.Serialization;
2020-11-20 22:24:48 +00:00
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
2020-10-17 18:19:44 +00:00
using Newtonsoft.Json;
2020-04-19 13:39:28 +00:00
using Rebus.DataBus.InMem;
2020-11-22 10:49:28 +00:00
using Rebus.Logging;
using Rebus.Persistence.InMem;
using Rebus.Routing.TypeBased;
using Rebus.Transport.InMem;
using Storage.Net;
using Storage.Net.Blobs;
namespace Elsa
{
public class ElsaOptions
{
public ElsaOptions(IServiceCollection services)
{
Services = services;
WorkflowDefinitionStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowDefinitionStore>(sp);
WorkflowInstanceStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowInstanceStore>(sp);
StorageFactory = sp => Storage.Net.StorageFactory.Blobs.InMemory();
DistributedLockProviderFactory = sp => new DefaultLockProvider();
SignalFactory = sp => new Signal();
2020-11-20 22:24:48 +00:00
JsonSerializerConfigurer = (sp, serializer) => { };
AddAutoMapper = () =>
{
services.AddAutoMapper(ServiceLifetime.Singleton);
services.AddSingleton(sp => sp.CreateAutoMapperConfiguration());
};
2020-11-20 12:29:08 +00:00
2020-11-20 22:24:48 +00:00
services.AddSingleton<InMemNetwork>();
services.AddSingleton<InMemorySubscriberStore>();
services.AddSingleton<InMemDataStore>();
2020-11-20 22:24:48 +00:00
ConfigureServiceBusEndpoint = ConfigureInMemoryServiceBusEndpoint;
2020-10-17 18:19:44 +00:00
CreateJsonSerializer = sp =>
{
var serializer = DefaultContentSerializer.CreateDefaultJsonSerializer();
2020-10-17 18:19:44 +00:00
JsonSerializerConfigurer(sp, serializer);
return serializer;
};
}
public IServiceCollection Services { get; }
internal Func<IServiceProvider, IBlobStorage> StorageFactory { get; set; }
2020-12-12 13:07:17 +00:00
internal Func<IServiceProvider, IWorkflowDefinitionStore> WorkflowDefinitionStoreFactory { get; set; }
internal Func<IServiceProvider, IWorkflowInstanceStore> WorkflowInstanceStoreFactory { get; set; }
internal Func<IServiceProvider, IDistributedLockProvider> DistributedLockProviderFactory { get; private set; }
internal Func<IServiceProvider, ISignal> SignalFactory { get; private set; }
2020-10-17 18:19:44 +00:00
internal Func<IServiceProvider, JsonSerializer> CreateJsonSerializer { get; private set; }
internal Action<IServiceProvider, JsonSerializer> JsonSerializerConfigurer { get; private set; }
internal Action AddAutoMapper { get; private set; }
2020-11-20 22:24:48 +00:00
internal Action<ServiceBusEndpointConfigurationContext> ConfigureServiceBusEndpoint { get; private set; }
public ElsaOptions UseDistributedLockProvider(Func<IServiceProvider, IDistributedLockProvider> factory)
{
DistributedLockProviderFactory = factory;
return this;
}
public ElsaOptions UseSignal(Func<IServiceProvider, ISignal> factory)
{
SignalFactory = factory;
return this;
}
2020-10-23 12:38:26 +00:00
public ElsaOptions UseStorage(Func<IBlobStorage> factory) => UseStorage(_ => factory());
public ElsaOptions UseStorage(Func<IServiceProvider, IBlobStorage> factory)
{
StorageFactory = factory;
return this;
}
2020-12-12 13:07:17 +00:00
public ElsaOptions UseWorkflowDefinitionStore(Func<IServiceProvider, IWorkflowDefinitionStore> factory)
{
WorkflowDefinitionStoreFactory = factory;
return this;
}
public ElsaOptions UseWorkflowInstanceStore(Func<IServiceProvider, IWorkflowInstanceStore> factory)
{
WorkflowInstanceStoreFactory = factory;
return this;
}
2020-11-20 22:24:48 +00:00
public ElsaOptions UseAutoMapper(Action addAutoMapper)
{
AddAutoMapper = addAutoMapper;
return this;
}
2020-10-10 19:10:09 +00:00
2020-10-17 18:19:44 +00:00
public ElsaOptions UseJsonSerializer(Func<IServiceProvider, JsonSerializer> factory)
{
CreateJsonSerializer = factory;
return this;
}
public ElsaOptions ConfigureJsonSerializer(Action<IServiceProvider, JsonSerializer> configure)
{
JsonSerializerConfigurer = configure;
return this;
}
public ElsaOptions UseServiceBus(Action<ServiceBusEndpointConfigurationContext> setup)
2020-11-20 22:24:48 +00:00
{
ConfigureServiceBusEndpoint = setup;
return this;
}
private static void ConfigureInMemoryServiceBusEndpoint(ServiceBusEndpointConfigurationContext context)
{
var serviceProvider = context.ServiceProvider;
var transport = serviceProvider.GetService<InMemNetwork>();
var store = serviceProvider.GetRequiredService<InMemorySubscriberStore>();
var queueName = context.QueueName;
context.Configurer
2020-11-22 10:49:28 +00:00
.Logging(l => l.ColoredConsole(LogLevel.Info))
2020-11-20 22:24:48 +00:00
.Subscriptions(s => s.StoreInMemory(store))
.Transport(t => t.UseInMemoryTransport(transport, queueName))
2020-11-22 10:49:28 +00:00
.Routing(r => r.TypeBased().Map(context.MessageTypeMap));
}
}
}