NoorDeen's avatar

why implementing DDD in php is so complex ?

every time I read about Domain Driven Design I feel I make my application structure more complex . like when using value objects .

if I have user Entity and user have email and password . in the Domain Driven Design I need to make new Email value object to hold the email string . why ?

can any one explain to me this ?

0 likes
13 replies
kramove's avatar

The only reason I can think of is maybe because you want to sanitize your email separately in its own class.

2 likes
bostinait's avatar
Level 6

Like @kramove said really, but also if you need to attach any kind of logic/functionality to a value.

Imagine you have a product weight you need to store. Within the value object you can have methods to convert to another unit type, or a formatting method that outputs the weight with the unit initials for the UI.

Currency conversation is another good example.

Pretty simple to have all that logic within a value object rather than in a controller or the UI itself, and it certainly isn't the responsibility of the model object to have that kind of logic.

1 like
pmall's avatar

@codesq Yes but why not having this logic in the product object ? In case a weight or a currency is only used for a product of course.

bostinait's avatar

In my opinion, I don't think weight or currency conversations are the responsibility of the product object, plus having that functionality in there as well could make for a pretty big class.

I used the weight/currency conversations as they wouldn't solely be used on a product object but in a cart object as well, and would therefore justify a value object.

If there was some logic relating solely to a product then of course there would be an argument to include the functionality directly within the product object, but I would still refer to part of my first point and large classes.

1 like
pobble's avatar

You don't have to do anything or follow any pattern. If you don't see any benefit to using a value object then you are just adding extra complexity. But always consider the big picture, I often sit here and wonder why the hell I'm using repositories when I can't see myself using anything other than Eloquent. But I have sites I have to work on that were written with no thought to maintainability and that make me want to hang myself!

1 like
bostinait's avatar

Exactly @pobble, that's one point I didn't mention and should have, if you don't think its necessary to use a value object for your particular app then don't do it.

pmall's avatar

I'm not very into DDD. But I always wondered : why not making a package containing all kind of basic value objects like email / weight / currencies etc ?

NoorDeen's avatar

thank you @codesq for explaining why and when to use value objects .

thank you @pobble for remembering me about maintainability .

@pmall you can't because any business has it own rules .

pmall's avatar

Yes but an email, phone, weight, etc value object is still an email etc regardless of the business

NoorDeen's avatar

@pmall some times email is required for user sometimes it not <- this is business rule

@codesq very much .

thepsion5's avatar

@pmall Is this a US phone number or a globally-valid phone number? Should the phone number allow extensions? Sometimes it's way easier to create a six-line value object rather than include a package and then extend the USPhoneNumberWithoutExtension value object. ;)

Please or to participate in this conversation.