public static DomainContext GetBlogContext() { var dataContext = new Entities(); var workspace = GetDomainWorkspace(dataContext); var persistedEventBus = GetPersistedEventBus(); var messageBus = GetEarlyEventBus(dataContext, workspace, persistedEventBus); var context = new DomainContext(workspace,messageBus); return context; }
private static MessageBus GetEarlyEventBus(Entities dataContext, IWorkspace workspace, MessageBus persistedEventBus) { var messageBus = new MessageBus(); messageBus .AsObservable<IDomainEvent>() .Do(e => workspace.Committing += (s, ea) => { ObjectStateEntry entry; if (dataContext.ObjectStateManager.TryGetObjectStateEntry(e.Sender, out entry)) { //resend the same event after commit persistedEventBus.Send(e); } }) .Subscribe(); return messageBus; }
public static IWorkspace GetDomainWorkspace(Entities context) { Func<Type, object> selector = t => { if (t == typeof(MyBlog.Domain.Entities.Post)) return context.Posts; if (t == typeof(MyBlog.Domain.Entities.Comment)) return context.Comments; if (t == typeof(MyBlog.Domain.Entities.Category)) return context.Categories; return null; }; return new EF4Workspace(context,selector); }