示例#1
0
        /// <summary>
        /// Schedules a command for asynchronous and, optionally, deferred delivery.
        /// </summary>
        /// <typeparam name="TCommand">The type of the command.</typeparam>
        /// <param name="command">The command.</param>
        /// <param name="due">The time when the command should be delivered. If this is null, the scheduler will deliver it as soon as possible.</param>
        /// <exception cref="System.ArgumentNullException">command</exception>
        protected async Task ScheduleCommandAsync <TCommand>(TCommand command,
                                                             DateTimeOffset?due = null)
            where TCommand : class, ICommand <T>
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            var commandScheduledEvent = new CommandScheduled <T>
            {
                AggregateId = Id,
                Command     = command,
                DueTime     = due
            };

            var scheduledCommand = new ScheduledCommand <T>(
                command,
                Id,
                due,
                new EventHasBeenRecordedPrecondition(commandScheduledEvent.ETag, Id));

            await Configuration.Current
            .CommandScheduler <T>()
            .Schedule(scheduledCommand);

            RecordEvent(commandScheduledEvent);
        }
示例#2
0
        internal static IScheduledCommand <TAggregate> CreateScheduledCommand <TCommand, TAggregate>(
            Guid aggregateId,
            TCommand command,
            DateTimeOffset?dueTime,
            IEvent deliveryDependsOn = null)
            where TCommand : ICommand <TAggregate> where TAggregate : IEventSourced
        {
            ScheduledCommandPrecondition precondition = null;

            if (deliveryDependsOn != null)
            {
                if (deliveryDependsOn.AggregateId == Guid.Empty)
                {
                    throw new ArgumentException("An AggregateId must be set on the event on which the scheduled command depends.");
                }

                if (String.IsNullOrWhiteSpace(deliveryDependsOn.ETag))
                {
                    deliveryDependsOn.IfTypeIs <Event>()
                    .ThenDo(e => e.ETag = Guid.NewGuid().ToString("N"))
                    .ElseDo(() => { throw new ArgumentException("An ETag must be set on the event on which the scheduled command depends."); });
                }

                precondition = new ScheduledCommandPrecondition
                {
                    AggregateId = deliveryDependsOn.AggregateId,
                    ETag        = deliveryDependsOn.ETag
                };
            }

            if (String.IsNullOrEmpty(command.ETag))
            {
                command.IfTypeIs <Command>()
                .ThenDo(c => c.ETag = CommandContext.Current
                                      .IfNotNull()
                                      .Then(ctx => ctx.NextETag(aggregateId.ToString("N")))
                                      .Else(() => Guid.NewGuid().ToString("N")));
            }

            var scheduledCommand = new CommandScheduled <TAggregate>
            {
                Command              = command,
                DueTime              = dueTime,
                AggregateId          = aggregateId,
                SequenceNumber       = -DateTimeOffset.UtcNow.Ticks,
                DeliveryPrecondition = precondition
            };

            return(scheduledCommand);
        }
示例#3
0
        /// <summary>
        /// Schedules a command for asynchronous and, optionally, deferred delivery.
        /// </summary>
        /// <typeparam name="TCommand">The type of the command.</typeparam>
        /// <param name="command">The command.</param>
        /// <param name="due">The time when the command should be delivered. If this is null, the scheduler will deliver it as soon as possible.</param>
        /// <exception cref="System.ArgumentNullException">command</exception>
        protected void ScheduleCommand <TCommand>(TCommand command,
                                                  DateTimeOffset?due = null)
            where TCommand : class, ICommand <T>
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var scheduled = new CommandScheduled <T>
            {
                Command = command,
                DueTime = due
            };

            RecordEvent(scheduled);
        }