I'm learning DDD. For a practice, I'm trying to convert my own app to DDD aggregates.
What I understand so far on aggregate.
- Aggregate defines transactional boundary
- Design aggregate as small as possible
- If there is no invariant to protect, I might not need an aggregate.
- You need to load everything from an aggregate.
I have a simple example. When a user uploads an image and information on the image, my app creates products based on the image.
I have Asset
aggregate and Asset has many Product
entities (?)
class Asset
{
public function newProduct(ProductType $productType) {
// return a new Product object then save id internally
}
}
When I add a new product to Asset, I save product id to Asset. When I load Asset from its own repository, I get a problem. Since I have more than 30+ product types, it is not optimal to load all products together. Since a Product can't exist without an Asset and a Product id must be persisted to an Asset, I have a business logic to protect.
Do I really need to load all products when I load an Asset?
Can I have a method in Asset that accepts a DAO to retrieve a product on demand? or a domain service to retrieve a product from an Asset?
class Asset
{
public function getProduct(ProductType $productType, aDAO $dao){
}
}
Update: Asset can't have duplicate Product
Asset
protects?Asset
need the product ID?CQRS
, do you?