Find a way to load the aggregate object from id and sequence number

I’m trying to find a way to, given an event store, provides the aggregate from id and sequence number;

Does anyone have idea how to help us do that?

Hi Lorenzo: This is be possible using the EventSourcingRepository API:

https://apidocs.axoniq.io/4.6/org/axonframework/eventsourcing/EventSourcingRepository.html

You can pass the eventStore to the Repository builder

val repo =  EventSourcingRepository
      .builder(YOUR_AGGREGATE::class.java)
      //.parameterResolverFactory(parameterResolverFactory)
      .eventStore(eventStore)
      .build()

and then load the aggregateInstance via

val aggregate = repo.load(identifier)

Note that though this is technically possible, you should keep in mind CQRS, which means that on the command side you probably will seldomly deal with this configuration yourself because the registered aggregate will handle commands and state automatically, and on the read side you will create custom projections based on events.

2 Likes

Hi this is what i’ve been testing.

I expected to get the AccountAggregate loaded until version spacified … but i receive always the last version.

public class AccountQueryServiceImpl implements AccountQueryService {

    private final EventStore eventStore;
    private UnitOfWork<?> unitOfWork;
    private AccountRepository accountRepository;
    private EventSourcingRepository<AccountAggregate> testSubject;

    .....

    @Override
    public AccountQueryEntity getAccount(String accountNumber, Long sequenceNumber) {
        if (sequenceNumber == null) {
            return accountRepository.findById(accountNumber).get();
        }
        unitOfWork = DefaultUnitOfWork.startAndGet(null);
        testSubject = EventSourcingRepository
            .builder(AccountAggregate.class)
            
            //.parameterResolverFactory(parameterResolverFactory)
            .eventStore(eventStore)
            .filterByAggregateType()
            .build();
        
        Object aggregate = testSubject.load(accountNumber, sequenceNumber).getWrappedAggregate().getAggregateRoot();

Is there a way to load the Aggregate “state” passing id and axonSeqeunenceNumber ?