Skip to content

Commit

Permalink
Add feature check and refactor dependencies in Elsa
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sfmskywalker committed Apr 1, 2024
1 parent ad7e911 commit a52bc2b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/common/Elsa.Features/Implementations/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public Module(IServiceCollection services)
/// <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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public override void Configure()
});
}
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));
});
};
Expand Down
8 changes: 8 additions & 0 deletions src/modules/Elsa.MassTransit/Features/MassTransitFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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;
Expand Down Expand Up @@ -37,6 +39,11 @@ public MassTransitFeature(IModule module) : base(module)
/// 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()
Expand All @@ -48,6 +55,7 @@ public override void Apply()
{
var messageTypes = this.GetMessages();

Services.AddSingleton(ChannelQueueFormatterFactory);
Services.Configure<MassTransitWorkflowDispatcherOptions>(x => { });
Services.AddActivityProvider<MassTransitActivityTypeProvider>();
_runInMemory = BusConfigurator is null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
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;
Expand All @@ -31,11 +29,7 @@ public MassTransitWorkflowDispatcherFeature(IModule module) : base(module)
/// 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()
Expand All @@ -62,7 +56,6 @@ public override void Apply()
if (ConfigureDispatcherOptions != null)
options.Configure(ConfigureDispatcherOptions);

Services.AddSingleton(ChannelQueueFormatterFactory);
Services.AddScoped<MassTransitWorkflowCancellationDispatcher>();
}
}

0 comments on commit a52bc2b

Please sign in to comment.