Example #1
0
        /// <summary>
        ///     Performs the action of the command upon the aggregate.
        /// </summary>
        /// <param name="aggregate">The aggregate to which to apply the command.</param>
        /// <exception cref="CommandValidationException">
        ///     If the command cannot be applied due its state or the state of the aggregate, it should throw a
        ///     <see
        ///         cref="CommandValidationException" />
        ///     indicating the specifics of the failure.
        /// </exception>
        public virtual void ApplyTo(TAggregate aggregate)
        {
            if (aggregate == null)
            {
                throw new ArgumentNullException("aggregate");
            }

            if (!string.IsNullOrWhiteSpace(ETag))
            {
                var eventSourced = aggregate as IEventSourced;
                if (eventSourced.HasETag(ETag))
                {
                    return;
                }
            }

            // validate that the command's state is valid in and of itself
            var validationReport = RunAllValidations(aggregate, false);

            using (CommandContext.Establish(this))
            {
                if (validationReport.HasFailures)
                {
                    HandleCommandValidationFailure(aggregate, validationReport);
                }
                else
                {
                    EnactCommand(aggregate);
                }
            }
        }
Example #2
0
        /// <summary>
        ///     Performs the action of the command upon the target.
        /// </summary>
        /// <param name="target">The target to which to apply the command.</param>
        /// <exception cref="CommandValidationException">
        ///     If the command cannot be applied due its state or the state of the target, it should throw a
        ///     <see
        ///         cref="CommandValidationException" />
        ///     indicating the specifics of the failure.
        /// </exception>
        public virtual void ApplyTo(TTarget target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            var idempotentTarget = target as IIdempotentCommandTarget;

            if (idempotentTarget != null &&
                idempotentTarget.ShouldIgnore(this))
            {
                return;
            }

            var validationReport = RunAllValidations(target, false);

            using (CommandContext.Establish(this))
            {
                if (validationReport.HasFailures)
                {
                    HandleCommandValidationFailure(target, validationReport);
                }
                else
                {
                    EnactCommand(target);
                }
            }
        }
Example #3
0
        /// <summary>
        ///     Performs the action of the command upon the target.
        /// </summary>
        /// <param name="target">The target to which to apply the command.</param>
        /// <exception cref="CommandValidationException">
        ///     If the command cannot be applied due its state or the state of the target, it should throw a
        ///     <see
        ///         cref="CommandValidationException" />
        ///     indicating the specifics of the failure.
        /// </exception>
        public virtual async Task ApplyToAsync(TTarget target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (!string.IsNullOrWhiteSpace(ETag))
            {
                var eventSourced = target as IEventSourced;
                if (eventSourced != null && eventSourced.HasETag(ETag))
                {
                    return;
                }
            }

            var validationReport = RunAllValidations(target, false);

            using (CommandContext.Establish(this))
            {
                if (validationReport.HasFailures)
                {
                    HandleCommandValidationFailure(target, validationReport);
                }
                else
                {
                    await EnactCommandAsync(target);
                }
            }
        }
 public async Task Deliver(IScheduledCommand <TAggregate> scheduledCommand)
 {
     using (CommandContext.Establish(scheduledCommand.Command))
     {
         await repository.ApplyScheduledCommand(scheduledCommand);
     }
 }