using Elsa.Features.Services; using Elsa.MassTransit.Features; using Elsa.MassTransit.Implementations; using Elsa.MassTransit.Models; using MassTransit; // ReSharper disable once CheckNamespace namespace Elsa.Extensions; /// /// Provides extensions to that enables and configures MassTransit. /// public static class MassTransitFeatureExtensions { private static readonly object ServiceBusConsumerTypesKey = new(); private static readonly object MessageTypesKey = new(); /// /// Registers the specified type for MassTransit service bus consumer discovery. /// public static MassTransitFeature AddConsumer(this MassTransitFeature feature, string? name, bool isTemporary) where T : IConsumer => feature.AddConsumer(typeof(T), name, isTemporary); /// /// Registers the specified type for MassTransit service bus consumer discovery. /// /// The consumer type. /// The consumer definition type. public static MassTransitFeature AddConsumer(this MassTransitFeature feature, string? name, bool isTemporary) where T : IConsumer where TDefinition : IConsumerDefinition { return feature.AddConsumer(typeof(T), name, isTemporary, typeof(TDefinition)); } /// /// Registers the specified type for MassTransit service bus consumer discovery. /// public static MassTransitFeature AddConsumer(this MassTransitFeature feature, Type type, string? name, bool isTemporary, Type? consumerDefinitionType = default) { var types = feature.Module.Properties.GetOrAdd(ServiceBusConsumerTypesKey, () => new HashSet()); types.Add(new ConsumerTypeDefinition(type, consumerDefinitionType, name, isTemporary)); return feature; } /// /// Registers a message type which is to be used by the to dynamically provide activities to send and receive these messages. /// public static MassTransitFeature AddMessageType(this MassTransitFeature feature) where T : class => feature.AddMessageType(typeof(T)); /// /// Registers a message type which is to be used by the to dynamically provide activities to send and receive these messages. /// public static MassTransitFeature AddMessageType(this MassTransitFeature feature, Type type) { var types = feature.Module.Properties.GetOrAdd(MessageTypesKey, () => new HashSet()); types.Add(type); return feature; } /// /// Returns all collected consumer types. /// public static IEnumerable GetConsumers(this MassTransitFeature feature) => feature.Module.Properties.GetOrAdd(ServiceBusConsumerTypesKey, () => new HashSet()); /// /// Returns all collected message types. /// internal static IEnumerable GetMessages(this MassTransitFeature feature) => feature.Module.Properties.GetOrAdd(MessageTypesKey, () => new HashSet()); }