Define one repository per aggregate #141
-
For each aggregate or aggregate root, you should create one repository class. It is recommended, you can see an example by Microsoft: Here #130 it is mentioned that repositories are used to retrieve a domain entity So having multiple repositories with multiple mappers, my doubt is whether this is the correct way: If a customer updates the remark of their order, they would first retrieve the order: const order = orderRepo.findOneById('id') If an order exists everything is fine, I would have partially my order already mapped as "OrderEntity" and I could update the remark order.setRemark(remark)
order.save() In this case there was no problem partially bringing the order without its items, since there is no invariant directly related to the remark, but is lazy loading really good? In this second case, apart from modifying the comment, the client also deletes an item const order = orderRepo.findOneById('id')
const items = orderRepo.findItemsByOrderId('id')
order.addItems(items: OrderItemEntity[]) In this way I would have completed my order and I could carry out operations calmly knowing that the invariants will be fulfilled order.setRemark(remark)
order.removeItem(Item: OrderItemEntity)
orderRepo.save() Is this the correct path suggested? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Do you experience any performance issues that you need lazy loading? I would suggest against it for most cases, you don't need lazy loading unless you have performance issues. Also it's pretty error prone, you can easily forget to load items since you do it manually. |
Beta Was this translation helpful? Give feedback.
1 query is totally fine, I don't see much benefit of creating 2 repositories for this.
N entities doesn't mean you need N repositories, it's N repositories for N aggregate roots.
Order is your aggregate root, so everything that this aggregate needs can be loaded in a single repository and even a single query.
Microsoft diagram that you shared above does not conflict with that as far as I understand.