Created
December 7, 2011 14:29
-
-
Save jbogard/1443003 to your computer and use it in GitHub Desktop.
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 RavenDbMessageModule : IMessageModule | |
{ | |
private readonly IDocumentStore documentStore; | |
[ThreadStatic] | |
private static IDocumentSession currentSession; | |
public static IDocumentSession CurrentSession | |
{ | |
get { return currentSession; } | |
} | |
public RavenDbMessageModule(IDocumentStore documentStore) | |
{ | |
this.documentStore = documentStore; | |
} | |
public void Init(ITransport transport, IServiceBus serviceBus) | |
{ | |
transport.MessageArrived += TransportOnMessageArrived; | |
transport.MessageProcessingCompleted += TransportOnMessageProcessingCompleted; | |
} | |
public void Stop(ITransport transport, IServiceBus serviceBus) | |
{ | |
transport.MessageArrived -= TransportOnMessageArrived; | |
transport.MessageProcessingCompleted -= TransportOnMessageProcessingCompleted; | |
} | |
private static void TransportOnMessageProcessingCompleted(CurrentMessageInformation currentMessageInformation, Exception exception) | |
{ | |
if (currentSession != null) | |
{ | |
if (exception == null) | |
currentSession.SaveChanges(); | |
currentSession.Dispose(); | |
} | |
currentSession = null; | |
} | |
private bool TransportOnMessageArrived(CurrentMessageInformation currentMessageInformation) | |
{ | |
if (currentSession == null) | |
currentSession = documentStore.OpenSession(); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment