Aggregate
The aggregate is the centerpiece of the domain layer in the Codebricks framework.
Aggregates execute commands that encapsulate domain logic and produce events. In a CQRS/ES (Command Query Responsibility Segregation / Event Sourcing) application, an aggregate serves as a transaction boundary. This means that any given aggregate should be able to execute its commands without needing to communicate with other aggregates.
Since the write side is used solely to perform commands, your aggregate can be compact and only needs to maintain the state required for command execution.
For a more in-depth understanding, refer to Martin Fowler's definition of aggregates within the DDD (Domain-Driven Design) paradigm: Martin Fowler on DDD Aggregates.
Usage in Planning
In the Codebricks Platform, aggregates form the deployment units. This means every use case needs to be connected to an aggregate and is generated and deployed together. Essentially, you can think of an aggregate as a microservice within your system.
Use cases assigned to the same aggregate share the same code base and utilize the same domain layer and most of the infrastructure layer. This allows for the reuse of classes and adapters across similar use cases.
Add/Select Aggregate
The first step for each use case is to define the aggregate it belongs to.
You can select or create aggregates using the bottom menu on the left side.
To open the menu, click on the bottom-most box on the left side of the screen.
This action will open a dialog where you can select an existing aggregate or create a new one by clicking "Add."
After confirming your new aggregate, it will be saved and automatically selected for your use case.
Define the Aggregate Load Type
After creating a Command Use Case, you need to define your aggregate load type:
- Create: This option will create a new aggregate with a generated ID.
- Load: This will load an aggregate by ID from the database and will throw an error if the aggregate is not found.
- Upsert: This will try to load an aggregate by ID from the database and will create a new aggregate with that ID if it is not found.
The
Model the Aggregate State
The aggregate state is a critical part of the aggregate. It holds all the information necessary to perform the next business decisions.
Therefore, it needs to be modeled precisely.
You can model the aggregate state in the same way as other Use Case Details. (See Usecase Schema)
As multiple command use cases of the same aggregate share the same implementation and the same aggregate state, changing it in one use case will also change it in the others. Treat the aggregate state with care.
Conclusion
Aggregates are fundamental in structuring your domain logic and ensuring transactional integrity within your microservices architecture. By properly defining and managing aggregates within the Codebricks framework, you can maintain a clean, efficient, and reusable codebase.