*/
if (! newOffer.sameAs(seenOffer, 5))//TODO load delta from conf.
throw new OfferChangedExcpetion(reservation.getAggregateId(), seenOffer, newOffer);
Client client = loadClient();//create per logged client, not reservation owner
Purchase purchase = purchaseFactory.create(reservation.getAggregateId(), client, seenOffer);
if (! client.canAfford(purchase.getTotalCost()))
throw new DomainOperationException(client.getAggregateId(), "client has insufficent money");
purchaseRepository.save(purchase);//Aggregate must be managed by persistence context before firing events (synchronous listeners may need to load it)
/*
* Sample model where one aggregate creates another. Client does not manage payment lifecycle, therefore application must manage it.
*/
Payment payment = client.charge(purchase.getTotalCost());
paymentRepository.save(payment);
purchase.confirm();
reservation.close();
reservationRepository.save(reservation);
clientRepository.save(client);