Created
November 2, 2009 20:04
-
-
Save joliver/224412 to your computer and use it in GitHub Desktop.
CQRS Sample Routing Table
This file contains hidden or 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
private static void Main() | |
{ | |
IContainer container = null; // build container here | |
RegisterRoutes(container); // routes don't change | |
using (threadSpecificContainer = container.CreateInnerContainer()) | |
{ | |
var handler = new UnitOfWorkMessageHandler<IContainer>( | |
threadSpecificContainer, | |
new TransactionScope(), | |
new UnitOfWork(), | |
threadSpecificContainer.Resolve<RoutingTable<IContainer>>()); | |
using (handler) | |
handler.Handle(commandMessage); | |
} | |
} |
This file contains hidden or 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 RoutingTable<TContainer> | |
{ | |
private readonly IDictionary<Type, ICollection<Action<TContainer, IMessage>>> table = | |
new Dictionary<Type, ICollection<Action<TContainer, IMessage>>>(); | |
public void Register<TMessage>(Action<TContainer, TMessage> route) where TMessage : class, IMessage | |
{ | |
ICollection<Action<TContainer, IMessage>> routes; | |
var routingKey = typeof(TMessage); | |
if (!this.table.TryGetValue(routingKey, out routes)) | |
this.table[routingKey] = routes = new LinkedList<Action<TContainer, IMessage>>(); | |
routes.Add((container, message) => route(container, message as TMessage)); | |
} | |
public void Route(TContainer container, IMessage message) | |
{ | |
ICollection<Action<TContainer, IMessage>> routes; | |
if (!this.table.TryGetValue(message.GetType(), out routes)) | |
throw new RouteNotRegisteredException(message.GetType()); | |
foreach (var route in routes) | |
route(container, message); | |
} | |
} |
This file contains hidden or 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 UnitOfWorkMessageHandler<TContainer> : IHandleMessages | |
where TContainer : IDisposable | |
{ | |
private readonly TContainer container; | |
private readonly TransactionScope transaction; | |
private readonly IUnitOfWork unitOfWork; | |
private readonly RoutingTable<TContainer> routes; | |
public UnitOfWorkMessageHandler( | |
TContainer container, TransactionScope transaction, IUnitOfWork unitOfWork, RoutingTable<TContainer> routes) | |
{ | |
this.container = container; | |
this.transaction = transaction; | |
this.unitOfWork = unitOfWork; | |
this.routes = routes; | |
} | |
public void Handle(params IMessage[] messages) | |
{ | |
this.Handle(messages.AsEnumerable()); | |
} | |
public void Handle(IEnumerable<IMessage> messages) | |
{ | |
foreach (var message in messages) | |
this.routes.Route(this.container, message); | |
this.unitOfWork.Complete(); | |
this.transaction.Complete(); | |
} | |
public void Dispose() | |
{ | |
this.unitOfWork.Dispose(); | |
this.transaction.Dispose(); | |
this.container.Dispose(); | |
GC.SuppressFinalize(this); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment