예제 #1
0
        async Task INHibernateChildProjector.ProjectEvent(object anEvent, NHibernateProjectionContext context)
        {
            if (anEvent == null)
            {
                throw new ArgumentNullException(nameof(anEvent));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                await mapConfigurator.ProjectEvent(anEvent, context).ConfigureAwait(false);
            }
            catch (ProjectionException projectionException)
            {
                if (string.IsNullOrEmpty(projectionException.ChildProjector))
                {
                    projectionException.ChildProjector = typeof(TProjection).ToString();
                }

                throw;
            }
            catch (Exception exception)
            {
                throw new ProjectionException("Projector failed to project an event.", exception)
                      {
                          ChildProjector = typeof(TProjection).ToString()
                      };
            }
        }
예제 #2
0
        private Task HandleProjectionDeletion(TKey key, NHibernateProjectionContext context,
                                              ProjectionDeletionOptions options)
        {
            TProjection existingProjection = context.Session.Get <TProjection>(key);

            if (existingProjection == null)
            {
                switch (options.MissingProjectionBehavior)
                {
                case MissingProjectionDeletionBehavior.Ignore:
                {
                    break;
                }

                case MissingProjectionDeletionBehavior.Throw:
                {
                    throw new ProjectionException(
                              $"Cannot delete {typeof(TProjection)} projection with key {key}. The projection does not exist.");
                }

                default:
                {
                    throw new NotSupportedException(
                              $"Not supported missing projection behavior {options.MissingProjectionBehavior}.");
                }
                }
            }
            else
            {
                context.Session.Delete(existingProjection);
            }

            return(Task.FromResult(false));
        }
예제 #3
0
        private async Task ProjectTransaction(Transaction transaction, ISession session)
        {
            foreach (EventEnvelope eventEnvelope in transaction.Events)
            {
                var context = new NHibernateProjectionContext
                {
                    TransactionId      = transaction.Id,
                    Session            = session,
                    StreamId           = transaction.StreamId,
                    TimeStampUtc       = transaction.TimeStampUtc,
                    Checkpoint         = transaction.Checkpoint,
                    EventHeaders       = eventEnvelope.Headers,
                    TransactionHeaders = transaction.Headers
                };

                try
                {
                    await mapConfigurator.ProjectEvent(eventEnvelope.Body, context).ConfigureAwait(false);
                }
                catch (ProjectionException projectionException)
                {
                    projectionException.TransactionId = transaction.Id;
                    projectionException.CurrentEvent  = eventEnvelope;
                    throw;
                }
                catch (Exception exception)
                {
                    throw new ProjectionException("Projector failed to project an event.", exception)
                          {
                              TransactionId = transaction.Id,
                              CurrentEvent  = eventEnvelope
                          };
                }
            }
        }
예제 #4
0
        public async Task ProjectEvent(object anEvent, NHibernateProjectionContext context)
        {
            foreach (INHibernateChildProjector child in children)
            {
                await child.ProjectEvent(anEvent, context).ConfigureAwait(false);
            }

            await map.Handle(anEvent, context).ConfigureAwait(false);
        }
예제 #5
0
        private async Task HandleProjectionModification(TKey key, NHibernateProjectionContext context,
                                                        Func <TProjection, Task> projector, ProjectionModificationOptions options)
        {
            TProjection projection = context.Session.Get <TProjection>(key);

            if (projection == null)
            {
                switch (options.MissingProjectionBehavior)
                {
                case MissingProjectionModificationBehavior.Create:
                {
                    projection = new TProjection {
                        Id = key
                    };
                    await projector(projection).ConfigureAwait(false);

                    context.Session.Save(projection);
                    break;
                }

                case MissingProjectionModificationBehavior.Ignore:
                {
                    break;
                }

                case MissingProjectionModificationBehavior.Throw:
                {
                    throw new ProjectionException(
                              $"Projection {typeof(TProjection)} with key {key} does not exist.");
                }

                default:
                {
                    throw new NotSupportedException(
                              $"Not supported missing projection behavior {options.MissingProjectionBehavior}.");
                }
                }
            }
            else
            {
                switch (options.ExistingProjectionBehavior)
                {
                case ExistingProjectionModificationBehavior.Update:
                {
                    await projector(projection).ConfigureAwait(false);

                    break;
                }

                case ExistingProjectionModificationBehavior.Ignore:
                {
                    break;
                }

                case ExistingProjectionModificationBehavior.Throw:
                {
                    throw new ProjectionException(
                              $"Projection {typeof(TProjection)} with key {key} already exists.");
                }

                default:
                {
                    throw new NotSupportedException(
                              $"Not supported existing projection behavior {options.ExistingProjectionBehavior}.");
                }
                }
            }
        }