Created
October 25, 2011 03:16
-
-
Save joliver/1311195 to your computer and use it in GitHub Desktop.
NServiceBusCommitDispatcher
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 sealed class NServiceBusCommitDispatcher : IPublishMessages | |
{ | |
private const string AggregateIdKey = "AggregateId"; | |
private const string CommitVersionKey = "CommitVersion"; | |
private const string EventVersionKey = "EventVersion"; | |
private const string BusPrefixKey = "Bus."; | |
private readonly IBus bus; | |
public NServiceBusCommitDispatcher(IBus bus) | |
{ | |
this.bus = bus; | |
} | |
public void Dispose() | |
{ | |
GC.SuppressFinalize(this); | |
} | |
public void Publish(Commit commit) | |
{ | |
for (var i = 0; i < commit.Events.Count; i++) | |
{ | |
var eventMessage = commit.Events[i]; | |
var busMessage = eventMessage.Body as IMessage; | |
AppendHeaders(busMessage, commit.Headers); | |
AppendHeaders(busMessage, eventMessage.Headers); | |
AppendVersion(commit, i); | |
this.bus.Publish(busMessage); | |
} | |
} | |
private static void AppendHeaders(IMessage message, IEnumerable<KeyValuePair<string, object>> headers) | |
{ | |
headers = headers.Where(x => x.Key.StartsWith(BusPrefixKey)); | |
foreach (var header in headers) | |
{ | |
var key = header.Key.Substring(BusPrefixKey.Length); | |
var value = (header.Value ?? string.Empty).ToString(); | |
message.SetHeader(key, value); | |
} | |
} | |
private static void AppendVersion(Commit commit, int index) | |
{ | |
var busMessage = commit.Events[index].Body as IMessage; | |
busMessage.SetHeader(AggregateIdKey, commit.StreamId.ToString()); | |
busMessage.SetHeader(CommitVersionKey, commit.StreamRevision.ToString()); | |
busMessage.SetHeader(EventVersionKey, GetSpecificEventVersion(commit, index).ToString()); | |
} | |
private static int GetSpecificEventVersion(Commit commit, int index) | |
{ | |
// e.g. (StreamRevision: 120) - (5 events) + 1 + (index @ 4: the last index) = event version: 120 | |
return commit.StreamRevision - commit.Events.Count + 1 + index; | |
} | |
} |
Sorry, I haven't used MT in quite some time, but I imagine it would be relatively close to this.
No problem. I'm just getting up to speed on MT myself and was hoping I
could use it as a sort of Rosetta Stone and cheat a little by having an
easy comparison to NSB. Thanks anyway.
…On Mon, Mar 19, 2012 at 7:15 AM, Jonathan Oliver < ***@***.*** > wrote:
Sorry, I haven't used MT in quite some time, but I imagine it would be
relatively close to this.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1311195
There is a great example using MassTransit in the Documently sample application found here: https://github.com/haf/Documently
Thanks padenj
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you happen to know of an equivalent example for MassTransit?