Hi,
first of all I am completely new on developing cqrs with java and axon.
Introduction:
I have a bounded context named carpark. When a admin create a new carpark, he can choose auto generated parking lots or does special configurations how to create the lots.
In my opinion its not the right place to use CQRS, because a carpark will be created one time and have in rare cases some changes. Nevertheless I decided to use it, because the lots should be later reserved by the reservation bounded context, which its perfectly suited for the cqrs pattern.
Conditions:
The lots could not been created standalone and belongs always to a carpark. So lots are an AggregateMember.
The command AddCarPark get only the amount of how many lots should be generated (more complex example is out of scope for the moment).
What if have done:
public class CarParkAggregate {
@AggregateIdentifier
CarParkId carParkId;
@AggregateMember
Map<CarParkId, Lot> lots;
...
@EventSourcingHandler
public void on(CarParkAdded e) {
log.debug("APPLYING {}", e);
this.initCarparkEvent(e);
for(int i = 1; i < e.getLotAmount(); i++) {
Lot lot = new Lot(
new LotId(),
e.getCarParkId(),
i
);
lots.put(e.getCarParkId(), lot);
}
}
...
}
The AggregateMember class:
@RequiredArgsConstructor
@AllArgsConstructor
public class Lot {
@NonNull
@EntityId
LotId lotId;
@NonNull CarParkId carParkId;
ReservationId reservationId;
@NonNull Integer lotNumber;
}
The CarParkProjection should save the new carpark to the database, which is getting the event.
public class CarParkProjection {
...
@EventHandler
public void on(CarParkAdded e) {
log.debug("PROJECTION {}", e);
CarParkAddress address = mapper.map(e.getAddress(), CarParkAddress.class);
CarParkView carpark = new CarParkView(
e.getCarParkId(),
e.getIataCode(),
e.getName(),
e.getDescription(),
address,
e.getSupportEmail(),
e.getSupportPhone(),
e.getTax(),
e.getState()
);
carParkService.addCarpark(carpark);
}
...
}
But the lots are not saved, because i haven’t this information on the event. I don’t know how I can solve this!
Thanks for advice!