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);
        }
Beispiel #2
0
        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()));
        }
Beispiel #4
0
        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();
            }
        }
        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>();

                    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 =>
                            {
                                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 void Handle(IViewContext context, DomainEventBatch domainEvent)
 {
     NumberOfCalls++;
 }