Last active
August 29, 2015 14:06
-
-
Save michaelnoonan/97777eb445235cb5428a to your computer and use it in GitHub Desktop.
Attribute Routing for Nimbus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AttributeUsage(AttributeTargets.Class)] | |
public class AppServerCommandRouteAttribute : Attribute | |
{ | |
public AppServerCommandRouteAttribute(string route) | |
{ | |
if (string.IsNullOrWhiteSpace(route)) throw new ArgumentNullException("route"); | |
Route = ("appserver." + route).ToLowerInvariant(); | |
} | |
public string Route { get; private set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppServerMessageRouter : IRouter | |
{ | |
internal static readonly string AppServerCommandsQueue = "appserver.commands"; | |
internal static readonly string AppServerRequestsQueue = "appserver.requests"; | |
//internal static readonly string AppServerEventsTopic = "appserver.events"; | |
public bool CanRoute(Type messageType) | |
{ | |
return messageType.IsBusMessage(); | |
} | |
public string Route(Type messageType, QueueOrTopic queueOrTopic) | |
{ | |
if (messageType.IsBusCommand()) | |
{ | |
var commandRouteAttribute = messageType.GetCustomAttribute<AppServerCommandRouteAttribute>(); | |
return commandRouteAttribute != null ? commandRouteAttribute.Route : AppServerCommandsQueue; | |
} | |
if (messageType.IsBusRequest()) | |
return AppServerRequestsQueue; | |
if (messageType.IsBusEvent()) | |
return PathFactory.TopicPathFor(messageType); | |
//return AppServerEventsTopic; | |
throw new Exception("Cannot build a route for the message type '{0}'.".FormatWith(messageType.FullName)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DelegatingRouter : IRouter | |
{ | |
private readonly IEnumerable<IRouter> _routers; | |
public DelegatingRouter(IEnumerable<IRouter> routers) | |
{ | |
_routers = routers; | |
} | |
public string Route(Type messageType, QueueOrTopic queueOrTopic) | |
{ | |
//Terrible, fix it. | |
var router = _routers.FirstOrDefault(x => ((dynamic)x).CanRoute(messageType) == true); | |
if (router == null) | |
throw new RoutingException("Unable to find a router for message type {0}".FormatWith(messageType.FullName)); | |
return router.Route(messageType, queueOrTopic); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.WithRouter(new DelegatingRouter(new IRouter[] | |
{ | |
new AppServerMessageRouter() | |
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QueuedCommunicationCommandRouteAttribute : AppServerCommandRouteAttribute | |
{ | |
public QueuedCommunicationCommandRouteAttribute() | |
: base("queuedcommunication") | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RoutingException : Exception | |
{ | |
public RoutingException(string message) | |
: base(message) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment