public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch) { var updatedViews = new HashSet <TViewInstance>(); foreach (var e in batch) { if (ViewLocator.IsRelevant <TViewInstance>(e)) { var affectedViewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in affectedViewIds) { var viewInstance = _views.GetOrAdd(viewId, id => _dispatcher.CreateNewInstance(id)); _dispatcher.DispatchToView(viewContext, e, viewInstance); updatedViews.Add(viewInstance); } } Interlocked.Exchange(ref _position, e.GetGlobalSequenceNumber()); } RaiseUpdatedEventFor(updatedViews); }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch, IViewManagerProfiler viewManagerProfiler) { var eventList = batch.ToList(); if (!eventList.Any()) { return; } var newPosition = eventList.Max(e => e.GetGlobalSequenceNumber()); using (var conn = new SqlConnection(_connectionString)) { conn.Open(); using (var tx = conn.BeginTransaction()) { var activeViewsById = new Dictionary <string, TViewInstance>(); if (BatchDispatchEnabled) { var domainEventBatch = new DomainEventBatch(eventList); eventList.Clear(); eventList.Add(domainEventBatch); } foreach (var e in eventList) { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var stopwatch = Stopwatch.StartNew(); var viewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in viewIds) { var view = activeViewsById .GetOrAdd(viewId, id => FindOneById(id, tx, conn) ?? _dispatcher.CreateNewInstance(viewId)); _dispatcher.DispatchToView(viewContext, e, view); } viewManagerProfiler.RegisterTimeSpent(this, e, stopwatch.Elapsed); } Save(activeViewsById, conn, tx); RaiseUpdatedEventFor(activeViewsById.Values); UpdatePosition(conn, tx, newPosition); tx.Commit(); } } Interlocked.Exchange(ref _cachedPosition, newPosition); }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch, IViewManagerProfiler viewManagerProfiler) { var updatedViews = new HashSet <TViewInstance>(); foreach (var e in batch) { if (ViewLocator.IsRelevant <TViewInstance>(e)) { var stopwatch = Stopwatch.StartNew(); var affectedViewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in affectedViewIds) { var viewInstance = _views.GetOrAdd(viewId, id => _dispatcher.CreateNewInstance(id)); _dispatcher.DispatchToView(viewContext, e, viewInstance); updatedViews.Add(viewInstance); } viewManagerProfiler.RegisterTimeSpent(this, e, stopwatch.Elapsed); } Interlocked.Exchange(ref _position, e.GetGlobalSequenceNumber()); } RaiseUpdatedEventFor(updatedViews); }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch, IViewManagerProfiler viewManagerProfiler) { var eventList = batch.ToList(); if (!eventList.Any()) { return; } var newPosition = eventList.Max(e => e.GetGlobalSequenceNumber()); using (var connection = GetConnection()) { using (var transaction = connection.BeginTransaction()) { var activeViewsById = new Dictionary <string, ActiveViewInstance>(); foreach (var e in eventList) { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var stopwatch = Stopwatch.StartNew(); var viewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in viewIds) { var view = activeViewsById .GetOrAdd(viewId, id => { var existing = FindOneById(id, connection, transaction); return(existing != null ? ActiveViewInstance.Existing(existing) : ActiveViewInstance.New(_dispatcher.CreateNewInstance(id))); }); _dispatcher.DispatchToView(viewContext, e, view.ViewInstance); } viewManagerProfiler.RegisterTimeSpent(this, e, stopwatch.Elapsed); } Save(activeViewsById, connection, transaction); RaiseUpdatedEventFor(activeViewsById.Values.Select(a => a.ViewInstance)); UpdatePosition(connection, transaction, newPosition); transaction.Commit(); } } }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch, IViewManagerProfiler viewManagerProfiler) { var eventList = batch.ToList(); if (!eventList.Any()) { return; } if (BatchDispatchEnabled) { var domainEventBatch = new DomainEventBatch(eventList); eventList.Clear(); eventList.Add(domainEventBatch); } var activeViewInstances = new Dictionary <string, TViewInstance>(); using (var context = GetContext()) { foreach (var e in eventList) { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var stopwatch = Stopwatch.StartNew(); var viewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in viewIds) { var viewInstance = activeViewInstances.ContainsKey(viewId) ? activeViewInstances[viewId] : (activeViewInstances[viewId] = context.ViewCollection.FirstOrDefault(v => v.Id == viewId) ?? CreateAndAddNewViewInstance(viewId, context.ViewCollection)); _dispatcherHelper.DispatchToView(viewContext, e, viewInstance); } viewManagerProfiler.RegisterTimeSpent(this, e, stopwatch.Elapsed); } UpdatePersistentCache(context, eventList.Max(e => e.GetGlobalSequenceNumber())); context.SaveChanges(); } RaiseUpdatedEventFor(activeViewInstances.Values); }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch, IViewManagerProfiler viewManagerProfiler) { if (_purging) { return; } var cachedViewInstances = new Dictionary <string, TViewInstance>(); var eventList = batch.ToList(); if (!eventList.Any()) { return; } if (BatchDispatchEnabled) { var domainEventBatch = new DomainEventBatch(eventList); eventList.Clear(); eventList.Add(domainEventBatch); } foreach (var e in eventList) { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var stopwatch = Stopwatch.StartNew(); var viewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in viewIds) { var viewInstance = cachedViewInstances[viewId] = GetOrCreateViewInstance(viewId, cachedViewInstances); _dispatcherHelper.DispatchToView(viewContext, e, viewInstance); } viewManagerProfiler.RegisterTimeSpent(this, e, stopwatch.Elapsed); } FlushCacheToDatabase(cachedViewInstances); RaiseUpdatedEventFor(cachedViewInstances.Values); UpdatePersistentCache(eventList.Max(e => e.GetGlobalSequenceNumber())); }
public void Dispatch(IEventStore eventStore, IEnumerable <DomainEvent> events) { if (_stopped) { return; } try { var viewContext = new DefaultViewContext(_aggregateRootRepository, _domainTypeNameMapper); foreach (var e in events) { try { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var affectedViewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in affectedViewIds) { try { var viewInstance = _views.GetOrAdd(viewId, id => _dispatcher.CreateNewInstance(id)); _dispatcher.DispatchToView(viewContext, e, viewInstance); } catch (Exception exception) { throw new ApplicationException(string.Format("An error ocurred when dispatching {0} to view with ID {1}", e, viewId), exception); } } } catch (Exception exception) { throw new ApplicationException(string.Format("Could not dispatch {0} to view(s)", e), exception); } } } catch (Exception exception) { _logger.Warn(exception, "An error occurred during event processing - the view will stop processing events!"); _stopped = true; } }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch) { if (_purging) { return; } var cachedViewInstances = new Dictionary <string, TViewInstance>(); var eventList = batch.ToList(); if (!eventList.Any()) { return; } foreach (var e in eventList) { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var viewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in viewIds) { var viewInstance = cachedViewInstances[viewId] = GetOrCreateViewInstance(viewId, cachedViewInstances); _dispatcherHelper.DispatchToView(viewContext, e, viewInstance); } } FlushCacheToDatabase(cachedViewInstances); RaiseUpdatedEventFor(cachedViewInstances.Values); UpdatePersistentCache(eventList.Max(e => e.GetGlobalSequenceNumber())); }
public void CanDetermineIfRelevantWithPolymorphy() { Assert.That(ViewLocator.IsRelevant <AnotherView>(new Event()), Is.EqualTo(true)); Assert.That(ViewLocator.IsRelevant <AnotherView>(new AnotherEvent()), Is.EqualTo(true)); }
public void CanDetermineIfRelevant() { Assert.That(ViewLocator.IsRelevant <SomeView>(new Event()), Is.EqualTo(true)); Assert.That(ViewLocator.IsRelevant <SomeView>(new AnotherEvent()), Is.EqualTo(false)); }
public override void Dispatch(IViewContext viewContext, IEnumerable <DomainEvent> batch, IViewManagerProfiler viewManagerProfiler) { if (_purging) { return; } var eventList = batch.ToList(); if (!eventList.Any()) { return; } if (BatchDispatchEnabled) { var domainEventBatch = new DomainEventBatch(eventList); eventList.Clear(); eventList.Add(domainEventBatch); } using (var session = _store.OpenSession()) { foreach (var e in eventList) { if (!ViewLocator.IsRelevant <TViewInstance>(e)) { continue; } var stopwatch = Stopwatch.StartNew(); var viewIds = _viewLocator.GetAffectedViewIds(viewContext, e); foreach (var viewId in viewIds) { var viewInstance = session.Load <TViewInstance>(viewId); if (viewInstance == null) { viewInstance = _dispatcherHelper.CreateNewInstance(viewId); session.Store(viewInstance); } _dispatcherHelper.DispatchToView(viewContext, e, viewInstance); } viewManagerProfiler.RegisterTimeSpent(this, e, stopwatch.Elapsed); } var position = session.Load <ViewPosition>(_viewPositionKey); if (position == null) { position = new ViewPosition { Id = _viewPositionKey }; session.Store(position); } position.Position = eventList.Max(e => e.GetGlobalSequenceNumber()); RaiseUpdatedEventFor(session.Advanced.ManagedEntities.OfType <TViewInstance>()); session.SaveChanges(); } }