From 50f69e1d81537571e2d7d9e101fb8e0bade94459 Mon Sep 17 00:00:00 2001 From: Paolo Possanzini Date: Mon, 18 Nov 2024 09:18:15 +0100 Subject: [PATCH] more expressivity in queue configuration --- Arbitrer/extensions/ArbitrerExtensions.cs | 46 +++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/Arbitrer/extensions/ArbitrerExtensions.cs b/Arbitrer/extensions/ArbitrerExtensions.cs index 34384f7..2a6147f 100644 --- a/Arbitrer/extensions/ArbitrerExtensions.cs +++ b/Arbitrer/extensions/ArbitrerExtensions.cs @@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; using MediatR; using Microsoft.Extensions.DependencyInjection; @@ -312,15 +313,46 @@ public static bool IsNotification(this Type t) return typeof(INotification).IsAssignableFrom(t) && !typeof(IBaseRequest).IsAssignableFrom(t); } - public static ArbitrerOptions SetTypeQueueName(this ArbitrerOptions options, string queueName) + public static ArbitrerOptions SetTypeQueueName(this ArbitrerOptions options, string queueName) { - if (options.QueueNames.ContainsKey(typeof(T))) + options.SetTypeQueueName(typeof(T), queueName); + return options; + } + + public static ArbitrerOptions SetTypeQueueName(this ArbitrerOptions options, Type type, string queueName) + { + if (options.QueueNames.ContainsKey(type)) { - options.QueueNames[typeof(T)] = queueName; + options.QueueNames[type] = queueName; } else { - options.QueueNames.Add(typeof(T), queueName); + options.QueueNames.Add(type, queueName); + } + + return options; + } + + public static ArbitrerOptions SetTypesQueueName(this ArbitrerOptions options, Func> typeselect, Func typeNameFunction) + { + var types = typeselect(); + foreach (var t in types) + { + var result = typeNameFunction(t); + options.SetTypeQueueName(t, result); + } + + return options; + } + + public static ArbitrerOptions SetTypesQueueName(this ArbitrerOptions options, Func> typeselect, Func typeNameFunction) + { + var types = typeselect(); + foreach (var t in types) + { + var name = t.ArbitrerTypeName(options); + var result = typeNameFunction(name); + options.SetTypeQueueName(t, result); } return options; @@ -357,11 +389,11 @@ where typeof(INotification).IsAssignableFrom(t) /// - /// Gets the queue name for the specified type. + /// Gets the type name used for the specified type. /// /// The type. - /// The instance to append the queue name to (optional). - /// The queue name for the specified type. + /// The instance to append the type name to (optional). + /// The type name for the specified type. public static string ArbitrerTypeName(this Type t, ArbitrerOptions options, StringBuilder sb = null) { if (t.CustomAttributes.Any())