2023-01-04 14:28:28 +00:00
using Elsa.Features.Services ;
using Elsa.MassTransit.Features ;
2023-01-10 13:32:14 +00:00
using Elsa.MassTransit.Implementations ;
2023-12-11 18:59:13 +00:00
using Elsa.MassTransit.Models ;
2023-01-04 14:28:28 +00:00
using MassTransit ;
// ReSharper disable once CheckNamespace
namespace Elsa.Extensions ;
/// <summary>
/// Provides extensions to <see cref="IModule"/> that enables and configures MassTransit.
/// </summary>
public static class MassTransitFeatureExtensions
{
private static readonly object ServiceBusConsumerTypesKey = new ( ) ;
2023-01-10 13:32:14 +00:00
private static readonly object MessageTypesKey = new ( ) ;
2023-01-04 14:28:28 +00:00
/// <summary>
/// Registers the specified type for MassTransit service bus consumer discovery.
/// </summary>
2024-01-25 13:51:48 +00:00
public static MassTransitFeature AddConsumer < T > ( this MassTransitFeature feature , string? name , bool isTemporary ) where T : IConsumer = > feature . AddConsumer ( typeof ( T ) , name , isTemporary ) ;
2023-12-11 18:59:13 +00:00
2023-01-04 14:28:28 +00:00
/// <summary>
/// Registers the specified type for MassTransit service bus consumer discovery.
/// </summary>
2023-12-11 18:59:13 +00:00
/// <typeparam name="T">The consumer type.</typeparam>
/// <typeparam name="TDefinition">The consumer definition type.</typeparam>
2024-01-25 13:51:48 +00:00
public static MassTransitFeature AddConsumer < T , TDefinition > ( this MassTransitFeature feature , string? name , bool isTemporary )
2023-12-11 18:59:13 +00:00
where T : IConsumer
where TDefinition : IConsumerDefinition
2023-01-04 14:28:28 +00:00
{
2024-01-25 13:51:48 +00:00
return feature . AddConsumer ( typeof ( T ) , name , isTemporary , typeof ( TDefinition ) ) ;
2023-12-11 18:59:13 +00:00
}
/// <summary>
/// Registers the specified type for MassTransit service bus consumer discovery.
/// </summary>
2024-01-25 13:51:48 +00:00
public static MassTransitFeature AddConsumer ( this MassTransitFeature feature , Type type , string? name , bool isTemporary ,
Type ? consumerDefinitionType = default )
2023-12-11 18:59:13 +00:00
{
var types = feature . Module . Properties . GetOrAdd ( ServiceBusConsumerTypesKey , ( ) = > new HashSet < ConsumerTypeDefinition > ( ) ) ;
2024-01-25 13:51:48 +00:00
types . Add ( new ConsumerTypeDefinition ( type , consumerDefinitionType , name , isTemporary ) ) ;
2023-01-04 14:28:28 +00:00
return feature ;
}
2023-01-10 13:32:14 +00:00
/// <summary>
/// Registers a message type which is to be used by the <see cref="MassTransitActivityTypeProvider"/> to dynamically provide activities to send and receive these messages.
/// </summary>
public static MassTransitFeature AddMessageType < T > ( this MassTransitFeature feature ) where T : class = > feature . AddMessageType ( typeof ( T ) ) ;
2023-12-11 18:59:13 +00:00
2023-01-04 14:28:28 +00:00
/// <summary>
2023-01-10 13:32:14 +00:00
/// Registers a message type which is to be used by the <see cref="MassTransitActivityTypeProvider"/> to dynamically provide activities to send and receive these messages.
/// </summary>
public static MassTransitFeature AddMessageType ( this MassTransitFeature feature , Type type )
{
var types = feature . Module . Properties . GetOrAdd ( MessageTypesKey , ( ) = > new HashSet < Type > ( ) ) ;
types . Add ( type ) ;
return feature ;
}
2023-12-11 18:59:13 +00:00
2023-01-10 13:32:14 +00:00
/// <summary>
/// Returns all collected consumer types.
2023-01-04 14:28:28 +00:00
/// </summary>
2024-01-25 13:51:48 +00:00
public static IEnumerable < ConsumerTypeDefinition > GetConsumers ( this MassTransitFeature feature ) = > feature . Module . Properties . GetOrAdd ( ServiceBusConsumerTypesKey , ( ) = > new HashSet < ConsumerTypeDefinition > ( ) ) ;
2023-12-11 18:59:13 +00:00
2023-01-10 13:32:14 +00:00
/// <summary>
/// Returns all collected message types.
/// </summary>
internal static IEnumerable < Type > GetMessages ( this MassTransitFeature feature ) = > feature . Module . Properties . GetOrAdd ( MessageTypesKey , ( ) = > new HashSet < Type > ( ) ) ;
2023-01-04 14:28:28 +00:00
}