Services are responsible for mapping each external request to a command object and sending it to a new or existing aggregate. A service is often invoked by a Spring MVC Controller, but can also be invoked by an integration framework such as Spring Integration in response to a message.

The Eventuate client framework for Java supports several different ways to implement services. The simplest approach is to use an AggregateRepository.

Using the AggregateRepository class

AggregateRepository is a generic class that has two type parameters: the aggregate class and the command class. It encapsulates the boilerplate logic you will use to access the Event Store, in lieu of writing logic from scratch each time.

AggregateRepository defines the following methods:

  • save() - Creates a new aggregate by processing the specified command, applying the events, and persisting the new aggregate in the Event Store
  • *update()** - Updates an existing aggregate by loading the aggregate from the Event Store, processing the specified command, applying the events, and persisting the new aggregate in the Event Store

Both methods are asynchronous and return an Rx Java Observable containing the updated aggregate.

Here is an example AccountService that uses an AggregateRepository:

public class AccountService  {

  private final AggregateRepository<Account, JavaAccountCommand> accountRepository;

  public AccountService(AggregateRepository<Account, JavaAccountCommand>
                                                                 accountRepository) {
    this.accountRepository = accountRepository;
  }

  public Observable<EntityWithMetadata<Account>>
              openAccount(BigDecimal initialBalance) {
       return accountRepository.save(new JavaOpenAccountCommand(initialBalance));
  }

}

Using the Event Store API directly

Instead of using an AggregateRepository you can write services that use the Event Store API directly.


Stay in touch
Copyright © 2021 Eventuate, Inc • All rights reserved.