Add feature check and refactor dependencies in Elsa

The commit introduces a new feature check in the `Module` class and refactors the dependencies in MassTransit features. Specifically, it enables querying for a specific feature before configuring the dispatcher endpoints, increasing flexibility and control. In addition, the responsibility for creating `IEndpointChannelFormatter` has been shifted from `MassTransitWorkflowDispatcherFeature` to `MassTransitFeature`, aligning with responsibility distribution.

Fixes #5165
This commit is contained in:
Sipke Schoorstra 2024-04-01 11:58:34 +02:00
parent 298ee70ae2
commit 96ba4f4e1c
4 changed files with 25 additions and 9 deletions

View file

@ -34,6 +34,18 @@ public class Module : IModule
/// <inheritdoc />
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
/// <inheritdoc />
public bool HasFeature<T>() where T : class, IFeature
{
return HasFeature(typeof(T));
}
/// <inheritdoc />
public bool HasFeature(Type featureType)
{
return _features.ContainsKey(featureType);
}
/// <inheritdoc />
public T Configure<T>(Action<T>? configure = default) where T : class, IFeature
=> Configure(module => (T)Activator.CreateInstance(typeof(T), module)!, configure);

View file

@ -76,7 +76,10 @@ public class RabbitMqServiceBusFeature : FeatureBase
});
}
configurator.SetupWorkflowDispatcherEndpoints(context);
// Only configure the dispatcher endpoints if the Masstransit Workflow Dispatcher feature is enabled.
if (Module.HasFeature<MassTransitWorkflowDispatcherFeature>())
configurator.SetupWorkflowDispatcherEndpoints(context);
configurator.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter("Elsa", false));
});
};

View file

@ -5,7 +5,9 @@ using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Services;
using Elsa.MassTransit.Consumers;
using Elsa.MassTransit.Contracts;
using Elsa.MassTransit.Extensions;
using Elsa.MassTransit.Formatters;
using Elsa.MassTransit.Models;
using Elsa.MassTransit.Options;
using Elsa.MassTransit.Services;
@ -37,6 +39,11 @@ public class MassTransitFeature : FeatureBase
/// A delegate that can be set to configure MassTransit's <see cref="IBusRegistrationConfigurator"/>. Used by transport-level features such as AzureServiceBusFeature and RabbitMqServiceBusFeature.
/// </summary>
public Action<IBusRegistrationConfigurator>? BusConfigurator { get; set; }
/// <summary>
/// A factory that creates a <see cref="IEndpointChannelFormatter"/>.
/// </summary>
public Func<IServiceProvider, IEndpointChannelFormatter> ChannelQueueFormatterFactory { get; set; } = _ => new DefaultEndpointChannelFormatter();
/// <inheritdoc />
public override void Configure()
@ -48,6 +55,7 @@ public class MassTransitFeature : FeatureBase
{
var messageTypes = this.GetMessages();
Services.AddSingleton(ChannelQueueFormatterFactory);
Services.Configure<MassTransitWorkflowDispatcherOptions>(x => { });
Services.AddActivityProvider<MassTransitActivityTypeProvider>();
_runInMemory = BusConfigurator is null;

View file

@ -4,8 +4,6 @@ using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.MassTransit.ConsumerDefinitions;
using Elsa.MassTransit.Consumers;
using Elsa.MassTransit.Contracts;
using Elsa.MassTransit.Formatters;
using Elsa.MassTransit.Options;
using Elsa.MassTransit.Services;
using Elsa.Workflows.Runtime.Contracts;
@ -31,11 +29,7 @@ public class MassTransitWorkflowDispatcherFeature : FeatureBase
/// Configures the MassTransit workflow dispatcher.
/// </summary>
public Action<MassTransitWorkflowDispatcherOptions>? ConfigureDispatcherOptions { get; set; }
/// <summary>
/// A factory that creates a <see cref="IEndpointChannelFormatter"/>.
/// </summary>
public Func<IServiceProvider, IEndpointChannelFormatter> ChannelQueueFormatterFactory { get; set; } = _ => new DefaultEndpointChannelFormatter();
/// <inheritdoc />
public override void Configure()
@ -62,7 +56,6 @@ public class MassTransitWorkflowDispatcherFeature : FeatureBase
if (ConfigureDispatcherOptions != null)
options.Configure(ConfigureDispatcherOptions);
Services.AddSingleton(ChannelQueueFormatterFactory);
Services.AddScoped<MassTransitWorkflowCancellationDispatcher>();
}
}