Example #1
0
        /// <summary>
        /// Publish the events from the transaction
        /// </summary>
        /// <param name="aggregateUnitOfWork">Event transaction containing the new events</param>
        private void PublishEvents(IAggregateUnitOfWork aggregateUnitOfWork)
        {
            var newEvents = aggregateUnitOfWork.GetEvents();

            foreach (var newEvent in newEvents)
            {
                this.messageBus.Publish(newEvent);
            }
        }
        /// <summary>
        /// Find (rehydrate) an account aggregate.
        /// </summary>
        /// <param name="id">Account Id</param>
        /// <param name="unitOfWork">Unit of work</param>
        /// <returns>Rehydrated aggregate</returns>
        /// <remarks>TODO: Could it happen that there are new events for the aggregate that are not on the aggregate in our unit of work?</remarks>
        public Account Find(Guid id, IAggregateUnitOfWork unitOfWork)
        {
            // This assumes that the aggregate in the unit of work is up-to-date
            var fromUnitOfWork = unitOfWork.Get<Account>(id);
            if (fromUnitOfWork != null)
            {
                return fromUnitOfWork;
            }

            var newAccount = new Account(id, this.eventStore.GetEventsFor(id));
            unitOfWork.Register(newAccount);
            return newAccount;
        }
        /// <summary>
        /// Find (rehydrate) an account aggregate.
        /// </summary>
        /// <param name="id">Account Id</param>
        /// <param name="unitOfWork">Unit of work</param>
        /// <returns>Rehydrated aggregate</returns>
        /// <remarks>TODO: Could it happen that there are new events for the aggregate that are not on the aggregate in our unit of work?</remarks>
        public Account Find(Guid id, IAggregateUnitOfWork unitOfWork)
        {
            // This assumes that the aggregate in the unit of work is up-to-date
            var fromUnitOfWork = unitOfWork.Get <Account>(id);

            if (fromUnitOfWork != null)
            {
                return(fromUnitOfWork);
            }

            var newAccount = new Account(id, this.eventStore.GetEventsFor(id));

            unitOfWork.Register(newAccount);
            return(newAccount);
        }
Example #4
0
        /// <summary>
        /// Resolve and invoke the corresponding command handler
        /// </summary>
        /// <typeparam name="TCommand">Command type</typeparam>
        /// <param name="command">Command to execute</param>
        /// <param name="aggregateUnitOfWork">Event transaction to track unpublished events</param>
        private void InvokeHandler <TCommand>(TCommand command, IAggregateUnitOfWork aggregateUnitOfWork) where TCommand : ICommand
        {
            var handler = this.dependencyInjectionContainer.Resolve <ICommandHandler <TCommand> >();

            handler.Handle(command, aggregateUnitOfWork);
        }
Example #5
0
 /// <summary>
 /// Add the events from the transaction to the event store
 /// </summary>
 /// <param name="aggregateUnitOfWork">Event transaction</param>
 private void StoreEvents(IAggregateUnitOfWork aggregateUnitOfWork)
 {
     this.eventStore.Add(aggregateUnitOfWork.GetEvents());
 }
 /// <summary>
 /// Save the (new) account (in the unit of work)
 /// </summary>
 /// <param name="account">Account to save</param>
 /// <param name="unitOfWork">Unit of work to use</param>
 public void Save(Account account, IAggregateUnitOfWork unitOfWork)
 {
     unitOfWork.Register(account);
 }
Example #7
0
 /// <summary>
 /// Handles the CreateAccountName command
 /// </summary>
 /// <param name="command">The CreateAccountNameCommand</param>
 /// <param name="aggregateUnitOfWork">The event transaction</param>
 public void Handle(CreateAccountCommand command, IAggregateUnitOfWork aggregateUnitOfWork)
 {
     Aggregates.Account account = new Aggregates.Account(command.Id, command.Name);
     this.repository.Save(account, aggregateUnitOfWork);
 }
Example #8
0
 /// <summary>
 /// Handles the ChangeAccountName command
 /// </summary>
 /// <param name="command">The ChangeAccountNameCommand</param>
 /// <param name="aggregateUnitOfWork">The event transaction</param>
 public void Handle(ChangeAccountNameCommand command, IAggregateUnitOfWork aggregateUnitOfWork)
 {
     Aggregates.Account account = this.repository.Find(command.Id, aggregateUnitOfWork);
     account.ChangeName(command.Name);
     this.repository.Save(account, aggregateUnitOfWork);
 }
 /// <summary>
 /// Save the (new) account (in the unit of work)
 /// </summary>
 /// <param name="account">Account to save</param>
 /// <param name="unitOfWork">Unit of work to use</param>
 public void Save(Account account, IAggregateUnitOfWork unitOfWork)
 {
     unitOfWork.Register(account);
 }