Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD Value Object shared by multiple entities

I've been studying about DDD and have some questions in respect to Value Object. I created value objects for Email, Phone and Address then added validation to them by using their constructor. So far so good. The thing is that I need the same Value Objects for different aggregate roots. Let's say I have 3 aggregate roots which need the same value objects (Customer, Doctor, Receptionist)

I've seen an example that was created one Address for each aggregate root, but I didn't want to repeat code as those ValueObjects also contains validations. Can I share them among the aggregate roots? If so, how do you structure it?

like image 247
manuc Avatar asked Mar 07 '26 00:03

manuc


1 Answers

Email, phone and address are concepts that don't belong to your domain, that I guess it is something related to hospitals or the like.

They would belong to another domain that we could call "people" or something like that, where you could define contact information concepts like those you mention (email, phone, address).

How to integrate with that domain? Well since they are concepts frequently used in many contexts, you could put them in a shared kernel.

So to the question regarding the structure, which I guess you are referring to folders, put them outside the structure of your "hospital" bounded context.

Create a shared kernel (concepts shared by several domains) with a folder called person/contact and put there your email, phone and address value objects. The shared kernel is like a library you use.

Hope it helps.

like image 95
choquero70 Avatar answered Mar 09 '26 12:03

choquero70



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!