Example application demonstrating the use of CQRS and Event Sourcing within the domain of hotel reservations. #NoFrameworks
This project is a sandbox for exploring how CQRS+ES affects the design of a system. The hypothesis is that it will lead to a better design than a typical database-centric approach; a design that is easily testable and does not detiorate as features are added. To answer that question, the problem being solved needs to be complex enough.
This project strives to differ from your typical toy examples in that the problem domain is complex enough to warrant all the techniques being used. The solution has been simplified, but the implemented features are production quality.
- technical features
- event store
- aggregate roots (write model)
- projections (read model)
- process managers
- GDPR compliance
- business features
- making a reservation
- room allocation
- payment
- check-in, check-out
- changing the departure date
- changing the room
Here are some pointers for where to look first in the code.
The web application's entry point is index.js and the entry points for each page are in routes.js. The UI is a single-page application which uses React and Redux but otherwise tries to avoid frameworks.
The backend application's main method is in Application.java and the entry points for each operation are in ApiController.java. External dependencies are wired with Spring in Application
, but the application core is wired in ApiController
constructor. See there the command handlers and query handlers which are the entry point to the business logic.
The framework code is in the fi.luontola.cqrshotel.framework package. It contains in-memory and PostgreSQL implementations of the event store (the latter's PL/SQL scripts are in src/main/resources/db/migration), and base classes for aggregate roots and projections. CQRS with event sourcing requires very little infrastructure code, so you can easily write it yourself without external frameworks, which helps to reduce complexity.
To learn how the write models work, read how a reservation is made, starting from SearchForAccommodationCommandHandler and MakeReservationHandler. The handlers contain no business logic. Instead, they delegate to Reservation which does all the work. Read the AggregateRoot base class, including its documentation, to understand how it should be used.
Of particular interest is how easy it is to unit test event sourced business logic. See SearchForAccommodationTest and MakeReservationTest. The given/when/then methods are in the simple AggregateRootTester base class.
To learn how the read models work, read ReservationsView and the base class Projection. Unit testing is again simple: ReservationsViewTest. Unlike aggregate roots, projections can listen to all events in the system; for example CapacityView is based on events from both Rooms and Reservations.
You must have installed Java 8, Maven 3.2.5, Node.js 6.8.0, Yarn 1.0, Docker 1.12 or higher versions of those. The project can be built with the ./build.sh
script. Below are the commands for developing this project.
Start the database:
docker-compose up -d db
Start the application:
mvn spring-boot:run
Start the web frontend:
yarn install
yarn start
The application will run at http://localhost:8080/
This example was mostly inspired by the following resources.
For more resources visit Awesome Domain-Driven Design. Ask questions at the DDD/CQRS discussion group.