/// <summary>
        /// Creates a new instance of <see cref="CommandEnvelope"/> for the specified <paramref name="aggregateId"/>and <paramref name="command"/>.
        /// </summary>
        /// <param name="aggregateId">The unique <see cref="Aggregate"/> identifier that is the target of the associated <paramref name="command"/>.</param>
        /// <param name="command">The command payload that is to be processed by the specified <see cref="Aggregate"/>.</param>
        public CommandEnvelope(Guid aggregateId, Command command)
        {
            Verify.NotNull(command, nameof(command));

            this.aggregateId = aggregateId;
            this.command = command;
        }
        protected CommandFailed(String reason, Command command)
        {
            Verify.NotNull(command, nameof(command));
            Verify.NotNullOrWhiteSpace(reason, nameof(reason));

            Command = command;
            Reason = reason;
        }
        /// <summary>
        /// Gets the <see cref="CommandHandler"/> associated with the specified <paramref name="command"/>.
        /// </summary>
        /// <param name="command">The command for which to retrieve a <see cref="CommandHandler"/> instance.</param>
        public CommandHandler GetHandlerFor(Command command)
        {
            Verify.NotNull(command, nameof(command));

            CommandHandler commandHandler;
            Type commandType = command.GetType();
            if (!knownCommandHandlers.TryGetValue(commandType, out commandHandler))
                throw new MappingException(Exceptions.CommandHandlerNotFound.FormatWith(commandType));

            return commandHandler;
        }
 /// <summary>
 /// Initializes a new instance of <see cref="SagaCommand"/>.
 /// </summary>
 /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
 /// <param name="command">The command to be published.</param>
 /// <param name="headers">The set of custom message headers associated with the <paramref name="command"/>.</param>
 public SagaCommand(Guid aggregateId, IEnumerable<Header> headers, Command command)
 {
     this.aggregateId = aggregateId;
     this.headers = headers;
     this.command = command;
 }
 public WithdrawlMoneyFailed(String reason, Command command)
     : base(reason, command)
 {
 }
 public MoneyTransferFailed(Guid transferId, String reason, Command command)
     : base(reason, command)
 {
     TransferId = transferId;
 }
 public MoneyDepositFailed(String reason, Command command)
     : base(reason, command)
 {
 }
 public CloseAccountFailed(String reason, Command command)
     : base(reason, command)
 {
 }
        /// <summary>
        /// Publishes the specified <paramref name="command"/> with the enumerable set of custom message headers.
        /// </summary>
        /// <param name="publisher">The command publisher.</param>
        /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
        /// <param name="command">The command to be published.</param>
        /// <param name="headers">The set of message headers associated with the command.</param>
        public static void Publish(this IPublishCommands publisher, Guid aggregateId, Command command, IEnumerable<Header> headers)
        {
            Verify.NotNull(publisher, nameof(publisher));

            publisher.Publish(headers, new CommandEnvelope(aggregateId, command));
        }
Example #10
0
 /// <summary>
 /// Verify that the specified command can be handled by this aggregate instance.
 /// </summary>
 /// <param name="command">The command to be handled.</param>
 internal void VerifyCanHandleCommand(Command command)
 {
     if (Version == 0 && RequiresExplicitCreate && !CanCreateAggregate(command))
         throw new InvalidOperationException(Exceptions.AggregateNotInitialized.FormatWith(GetType(), Id));
 }
Example #11
0
 /// <summary>
 /// Return <value>true</value> if the <paramref name="command"/> can create a new aggregateinstance; otherwise return <value>false</value> (default = <value>false</value>).
 /// </summary>
 /// <param name="command">The command attempting to create this aggregate instance.</param>
 protected override bool CanCreateAggregate(Command command)
 {
     return command is OpenAccount;
 }
Example #12
0
        /// <summary>
        /// Publishes the specified <paramref name="command"/> with the enumerable set of custom message headers.
        /// </summary>
        /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
        /// <param name="command">The command to be published.</param>
        /// <param name="headers">The set of message headers associated with the command.</param>
        protected void Publish(Guid aggregateId, Command command, IEnumerable<Header> headers)
        {
            Log.Trace("Publishing {0} command to aggregate {1}", command, aggregateId);

            var context = SagaContext.Current;
            if (context == null)
                throw new InvalidOperationException(Exceptions.NoSagaContext);

            context.Publish(aggregateId, headers == null ? GetHeadersFromEventContext() : headers.Concat(GetHeadersFromEventContext()).Distinct(header => header.Name), command);

            Log.Trace("Published {0} command to aggregate {1}", command, aggregateId);
        }
Example #13
0
 /// <summary>
 /// Publishes the specified <paramref name="command"/> with the set of custom message headers.
 /// </summary>
 /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
 /// <param name="command">The command to be published.</param>
 /// <param name="headers">The set of one or more custom message headers.</param>
 protected void Publish(Guid aggregateId, Command command, params Header[] headers)
 {
     Publish(aggregateId, command, headers == null || headers.Length == 0 ? (IEnumerable<Header>)null : headers);
 }
Example #14
0
 /// <summary>
 /// Publishes the specified <paramref name="command"/> with only the default message headers.
 /// </summary>
 /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
 /// <param name="command">The command to be published.</param>
 protected void Publish(Guid aggregateId, Command command)
 {
     Publish(aggregateId, command, (IEnumerable<Header>)null);
 }
Example #15
0
 /// <summary>
 /// Return <value>true</value> if the <paramref name="command"/> can create a new aggregateinstance; otherwise return <value>false</value> (default = <value>false</value>).
 /// </summary>
 /// <param name="command">The command attempting to create this aggregate instance.</param>
 protected virtual Boolean CanCreateAggregate(Command command)
 {
     return false;
 }
        /// <summary>
        /// Publishes the specified <paramref name="command"/> with the enumerable set of custom message headers.
        /// </summary>
        /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
        /// <param name="command">The command to be published.</param>
        /// <param name="headers">The set of message headers associated with the command.</param>
        internal void Publish(Guid aggregateId, IEnumerable<Header> headers, Command command)
        {
            if (publishedCommands == null)
                publishedCommands = new List<SagaCommand>();

            publishedCommands.Add(new SagaCommand(aggregateId, headers, command));
        }
        /// <summary>
        /// Publishes the specified <paramref name="command"/> with only the default message headers.
        /// </summary>
        /// <param name="publisher">The command publisher.</param>
        /// <param name="aggregateId">The <see cref="Aggregate"/> identifier that will handle the specified <paramref name="command"/>.</param>
        /// <param name="command">The command to be published.</param>
        public static void Publish(this IPublishCommands publisher, Guid aggregateId, Command command)
        {
            Verify.NotNull(publisher, nameof(publisher));

            publisher.Publish(null, new CommandEnvelope(aggregateId, command));
        }