A S
A S

Reputation: 85

I am trying to do update on query side

What I am doing is that i am trying to get the aggregate from repository and then process that particular event and mark it as a new event...so that query recognizes that it is a update event. But the problem is that I am not able to get that particular aggregate.

What I have done is here:

LockAwareAggregate<CustomerAggregate, EventSourcedAggregate<CustomerAggregate>> lockCustomerAggregate = 
    customerEventSourcingRepository.load(command.getId().toString());

EventSourcedAggregate<CustomerAggregate> eventSourcedCustomerAggregate = 
    lockCustomerAggregate.getWrappedAggregate();

CustomerAggregate customerAggregate = eventSourcedCustomerAggregate.getAggregateRoot();

customerAggregate.updateAddress(command.getAddress());

I used this code in 2.4.3 and it was returning a CustomerAggregate but right now it's returning a LockAwareAggregate. Can you please suggest where I am getting wrong.

can you please also share whats message in
UnitOfWork work = DefaultUnitOfWork.startAndGet(message??);

Thanks in advance.

Upvotes: 0

Views: 167

Answers (1)

Steven
Steven

Reputation: 7275

Although I'm not completely sure what you're trying to do, what it sounds like is that you're retrieving events from your EventStore and you say you're updating them. I'd highly encourage not to do that.

You've EventStore should be append only. The events stored in it have happened, so that's history. In real life you cannot adjust history, and that's how you should regard your events in the EventStore as well.

So, that's a warning. Your code snippet luckily suggests you're just trying to call a function on your aggregate to perform a certain 'action'. Action is the keyword here, as it signals you're doing a 'command'. Thus, you could call that function just as easily through a @CommandHandler annotated field on your aggregate to adjust it. In doing that you will not have to load your aggregate manually.

Added, I can't really see which Repository implementation you're using to load your aggregate manually (I guess the LockingRepository), but I'd suggest to use the Repository interface rather than a specific implementation. That interface should return you an Aggregate<T>, were T is the CustomerAggregate in your example. That should give you the possibility to call the updateAddresss() function as desired.

Probable answers

  1. In short, I think you're thus wiring the wrong Repository implementation in that snippet to load your aggregate. Just using the Repository i.o. the EventSourcingRepository directly should do fine.
  2. The message in the UnitOfWork your asking about can be any implementation of the Message interface. So that can be a Command, Event or a Query.

Suggestion 1. I regularly suggest to not use the Repository directly though. Axon Framework can do the heavy lifting there, by letting you publish command on the CommandBus/CommandGateway. That, together with the @CommandHandler annotation on your command handling functions, makes it so that the framework will automatically load the right aggregate from the Repository and call that annotated function.

Upvotes: 2

Related Questions