Last active
August 29, 2015 14:13
-
-
Save valeriob/a81fc839671df23cb88b to your computer and use it in GitHub Desktop.
RavenDb Contextual Listener
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
| // The same principle can be applyed to pass Container scope to the Listener and extract events published by entities via change tracking. | |
| // Has the nice addition to audit messages and can enable async dispatching | |
| // register documentstore on container | |
| cb.Register<IDocumentSession>(r => | |
| { | |
| var s = r.Resolve<IDocumentStore>().OpenSession(database); | |
| s.Advanced.ExternalState["ILifetimeScope"] = r.Resolve<ILifetimeScope>(); | |
| return s; | |
| }).InstancePerLifetimeScope(); | |
| // Extensions method to handle messages | |
| public static class Extensions | |
| { | |
| private static string StateKey = "PendingCommit"; | |
| public static void Send(this IDocumentSession session, object command) | |
| { | |
| var commit = GetCurrentCommit(session); | |
| commit.Send(command); | |
| } | |
| public static void Publish(this IDocumentSession session, object evnt) | |
| { | |
| var commit = GetCurrentCommit(session); | |
| commit.Events.Add(evnt); | |
| } | |
| static Commit GetCurrentCommit(IDocumentSession session) | |
| { | |
| if (session.Advanced.ExternalState.ContainsKey(StateKey) == false) | |
| { | |
| var scope = session.Advanced.ExternalState["ILifetimeScope"] as ILifetimeScope; | |
| var c = new Commit(scope); | |
| session.Advanced.ExternalState[StateKey] = c; | |
| session.Store(c); | |
| } | |
| return session.Advanced.ExternalState[StateKey] as Commit; | |
| } | |
| } | |
| // Dispatching RavenDb Listener | |
| class DistpatchDocumentStoreListener : IDocumentStoreListener | |
| { | |
| public void AfterStore(string key, object entityInstance, RavenJObject metadata) | |
| { | |
| var commit = entityInstance as Commit; | |
| if (commit == null) | |
| return; | |
| // dispatch | |
| //var bus = commit.CurrentScope.Resolve<IBus>(); | |
| commit.Reset(); | |
| } | |
| public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original) | |
| { | |
| return false; | |
| } | |
| } | |
| // Commit abstraction | |
| public class Commit | |
| { | |
| public string Id { get; private set; } | |
| public List<object> Events { get; private set; } | |
| public List<object> Commands { get; private set; } | |
| [Raven.Imports.Newtonsoft.Json.JsonIgnore] | |
| public ILifetimeScope CurrentScope { get; private set; } | |
| public Commit(ILifetimeScope scope) | |
| { | |
| Events = new List<object>(); | |
| Commands = new List<object>(); | |
| CurrentScope = scope; | |
| } | |
| internal void Send(object command) | |
| { | |
| Commands.Add(command); | |
| } | |
| internal void Publish(object evnt) | |
| { | |
| Events.Add(evnt); | |
| } | |
| public void Reset() | |
| { | |
| Events.Clear(); | |
| Commands.Clear(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment