2017-01-31 11:01:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Default
|
|
|
|
|
{
|
|
|
|
|
public class NamespaceMatchExchangeStrategy : IExchangeStrategy
|
|
|
|
|
{
|
2017-02-07 15:13:33 +00:00
|
|
|
|
// If the namespace starts with "Messaging.Service[.Optional.Further.Parts]", the exchange will be "Service".
|
|
|
|
|
// If no Messaging prefix is present, the first part of the namespace will be used instead.
|
|
|
|
|
private static readonly Regex NamespaceRegex = new Regex("^(Messaging\\.)?(?<exchange>[^\\.]+)", RegexOptions.Compiled | RegexOptions.Singleline);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetExchange(Type messageType)
|
|
|
|
|
{
|
|
|
|
|
if (messageType.Namespace == null)
|
|
|
|
|
throw new ArgumentException($"{messageType.FullName} does not have a namespace");
|
|
|
|
|
|
2017-02-07 15:13:33 +00:00
|
|
|
|
var match = NamespaceRegex.Match(messageType.Namespace);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
if (!match.Success)
|
|
|
|
|
throw new ArgumentException($"Namespace for {messageType.FullName} does not match the specified format");
|
|
|
|
|
|
2017-02-07 15:13:33 +00:00
|
|
|
|
return match.Groups["exchange"].Value.ToLower();
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|