Example #1
0
        private ViewProjection <TView, TId> projectEvent <TEvent>(
            Func <IDocumentSession, object, Guid, TId> viewIdSelector,
            Func <IDocumentSession, object, Guid, List <TId> > viewIdsSelector,
            Action <TView, TEvent> handler,
            ProjectionEventType type = ProjectionEventType.Modify) where TEvent : class
        {
            if (viewIdSelector == null && viewIdsSelector == null)
            {
                throw new ArgumentException($"{nameof(viewIdSelector)} or {nameof(viewIdsSelector)} must be provided.");
            }
            if (handler == null && type == ProjectionEventType.Modify)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            EventHandler eventHandler;

            if (type == ProjectionEventType.Modify)
            {
                eventHandler = new EventHandler(viewIdSelector, viewIdsSelector, (view, @event) => handler(view, @event as TEvent), type);
            }
            else
            {
                eventHandler = new EventHandler(viewIdSelector, viewIdsSelector, null, type);
            }

            _handlers.Add(typeof(TEvent), eventHandler);

            return(this);
        }
Example #2
0
 public EventHandler(
     Func <IDocumentSession, object, Guid, TId> idSelector,
     Func <IDocumentSession, object, Guid, List <TId> > idsSelector,
     Action <TView, object> handler,
     ProjectionEventType type)
 {
     IdSelector  = idSelector;
     IdsSelector = idsSelector;
     Handler     = handler;
     Type        = type;
 }
Example #3
0
 public EventHandler(
     Func <IDocumentSession, object, Guid, TId> idSelector,
     Func <IDocumentSession, object, Guid, List <TId> > idsSelector,
     Func <IDocumentSession, TView, object, Task> handler,
     Func <IDocumentSession, TView, object, Task <bool> > shouldDelete,
     ProjectionEventType type)
 {
     IdSelector   = idSelector;
     IdsSelector  = idsSelector;
     Handler      = handler;
     ShouldDelete = shouldDelete ?? defaultShouldDelete;
     Type         = type;
 }
Example #4
0
            public EventProjection(EventHandler eventHandler, TId viewId, IEvent @event, object projectionEvent)
            {
                ViewId = viewId;
                Type   = eventHandler.Type;

                if (Type == ProjectionEventType.Delete)
                {
                    ShouldDelete = (session, view) => eventHandler.ShouldDelete(session, view, projectionEvent ?? @event.Data);
                }
                else
                {
                    ProjectTo = (session, view) => eventHandler.Handler(session, view, projectionEvent ?? @event.Data);
                }
            }
Example #5
0
            public EventProjection(EventHandler eventHandler, TId viewId, IEvent @event, object projectionEvent)
            {
                ViewId = viewId;
                Type   = eventHandler.Type;

                if (projectionEvent != null)
                {
                    // Event handler uses ProjectionEvent generic
                    ProjectTo = view => eventHandler.Handler(view, projectionEvent);
                }
                else
                {
                    ProjectTo = view => eventHandler.Handler(view, @event.Data);
                }
            }
Example #6
0
        private ViewProjection <TView, TId> projectEvent <TEvent>(
            Func <IDocumentSession, object, Guid, TId> viewIdSelector,
            Func <IDocumentSession, object, Guid, List <TId> > viewIdsSelector,
            Func <IDocumentSession, TView, TEvent, Task> handler,
            Func <IDocumentSession, TView, TEvent, Task <bool> > shouldDelete = null,
            ProjectionEventType type = ProjectionEventType.CreateAndUpdate) where TEvent : class
        {
            if (viewIdSelector == null && viewIdsSelector == null)
            {
                throw new ArgumentException($"{nameof(viewIdSelector)} or {nameof(viewIdsSelector)} must be provided.");
            }
            if (handler == null && type == ProjectionEventType.CreateAndUpdate)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            EventHandler eventHandler;

            if (type == ProjectionEventType.CreateAndUpdate || type == ProjectionEventType.UpdateOnly)
            {
                eventHandler = new EventHandler(
                    viewIdSelector,
                    viewIdsSelector,
                    (session, view, @event) => handler(session, view, @event as TEvent),
                    null,
                    type);
            }
            else
            {
                eventHandler = new EventHandler(
                    viewIdSelector,
                    viewIdsSelector,
                    null,
                    shouldDelete == null
                        ? (Func <IDocumentSession, TView, object, Task <bool> >)null
                        : (session, view, @event) => shouldDelete(session, view, @event as TEvent),
                    type);
            }

            _handlers.Add(typeof(TEvent), eventHandler);

            return(this);
        }