A view repository is the outbound-adapter component of a CQRS view that updates and queries the view database. It hides the database behind an API used by event handlers and query handlers, mapping between their data types and the database API. section 13.2.1 section 13.3.3
The repository supports two paths. Event handlers extract data from provider events and call create, update, or delete methods; those methods map the data to the view schema and write it. Query handlers call find methods to retrieve results from the already populated view. section 13.3.3 section 13.3.4
Figure 13.7 The view repository sits between the view’s event and query handlers and its database, providing CUD methods that the event handlers invoke and find methods that the query handlers invoke. The CUD and find methods invoke the database’s operations.
The repository sits between the handlers and database on both paths. section 13.3.3
Design the repository around the view’s queries and event stream:
- Provide a find method for each system query. It accepts query parameters and maps database records to the expected result type. If the database cannot complete the query, apply the remaining filtering or sorting in application code. section 13.3.3
- Provide create, update, and delete methods for event types. Account for concurrent updates, out-of-order delivery, and duplicate events. Idempotency usually belongs where the event is applied, although an event handler can implement it instead. section 13.3.3 section 13.3.4
- Consider how clients cope with eventual consistency. A command can return an event ID or aggregate version as a token that the view uses to verify whether it is up to date with that command. section 13.3.3
Order history
An Order History Service subscribes to provider events and stores joined data in a view database. Its findOrderHistory() method accepts a consumer ID, pagination, and a filter; with suitable indexes, it uses a DynamoDB Query operation to retrieve a filtered page sorted by order creation date. section 13.2.1 section 13.3.3
Before creating a repository for a new view, check whether an existing view can support the query. Reuse avoids another service and datastore, but adding an unrelated query can reduce cohesion and couple queries that need to evolve independently. Choose the database and schema alongside the repository operations: a simple normalized relational schema may suffice for small, infrequent queries, while larger workloads may justify denormalization, indexes, or precomputation. section 13.3
Agree on provider-event contracts with the teams that own the data. Events need the properties required to identify and update view records, as well as values needed by later queries. section 13.3.4
