last day (15 days later) » 

1:59 PM
hi
you had a question ?
 
Yes.
ehm
About DDD
I'm assuming you have read the PEAA book of Martin Fowler?
 
yep
 
Well..
In there he talks about Identity fields in entities.
And he also has a solution KeyGenerator
that would automatically map the right ID to newly constructed entities
by taking the last key from a table +1
Are you perhaps using anything similar?
 
no
I've done 2 types of ID generation
through the database (auto invcrement field)
and with .NET's Guids
the "last key from a table +1" seems prone to concurrency problems to me
 
So with the auto increment field, when you instantiate new entities you keep the ID field null?
 
2:11 PM
at first, yes
 
So, you just let the database handle the ID part and keeping them null in the in-memory object.
 
yeah, this typically isn't a problem since you have a reference to the entity you just created
 
I see.
 
would it be, in your case ?
 
It wouldn't. :)
 
2:15 PM
maybe you should look for that on SO, I'm pretty sure it's a known issue
 
It isn't a problem in my case.
 
In any case, implementing a KeyGenerator yourself seems slippery to me
ok
 
I have one last question if you dont mind
 
go ahead
 
About value objects..
 
2:17 PM
yes ?
 
If I have an Entity class with attributes. Do I store the actual VOs in the entity or only the VO's value?
 
er... not sure what you mean by that but I'd say the actual VO :)
you're doing PHP right ?
 
Yes I am.
So instead of the String value for lets say Name, I would rather just store the actual Value Object
?
 
I don't know what the PHP equivalent would be, but in statically typed languages your Entity should have a property of the type of your value object (like Address, Email, etc.)
absolutely
 
Yes, in my setters of the Entity I would type hint the VO in the parameters.
So..
 
2:20 PM
yes
$email = email, email being a parameter of type Email (and not of type string)
or $this.$email or whatever ;)
 
Lets say I have a User entity, and I want to get the first name. I would do something like $user->getFirstname(); but
In the getFirstname() method, I would then have to do something like return $this->firstname->getValue(); //gets value from the VO firstname
?
 
Like I said in my answer, in non-functional languages I wouldn't wrap a single string into a VO
 
Or shortly said I would have to ask the VO for the value.
 
too much ceremony to declare it
 
Why not? In a VO I could apply validation rules in the setters.
 
2:24 PM
However, starting from 2 properties (like Name = FirstName + LastName) it becomes interesting IMO
 
Like not bigger than 35 chars, must be a string etc etc.
 
oh, ok
yeah, then definitely
 
Ok, so that means every VO needs some sort of getValue() method, to get the actual value of the VO, right.
 
and I guess you would have to define a getValue() accessor or something to expose the "real" string then, yes.
 
Yes!
 
2:25 PM
that's it
 
So that would probably be implemented a interface
Okay last one for real now.
 
err wait a minute
 
"every VO needs some sort of getValue()" => yes, except VO's that encapsulate multiple fields, such as Name = FirstName + LastName
in which case you would have $name.getFirstName() and getLastName()
 
ah, so an interface is not really worth it here
 
2:27 PM
and VO's like that are numerous, in my experience
and no, an interface is not worth it IMO
 
Which leads me to another question, is better to have a VO Name that consists out of Firstname and Lastname, or have 2 VO's Name with each containing the first and last name?
 
getX() and getY() are not really behaviors, so no need to define contracts for them
 
Yeah ^
 
definitely Name = FirstName and LastName to me
 
I see.
Ok and the last question I was referring to,
When Im retrieveing entities from my data mappers
That would mean in my data mappers I would have to instantiate all the VO's and pass them to my entity setters to set them, right?
 
2:30 PM
sure
except if you're using an ORM
if that's the kind of data mapper you're referring to
 
Nah, im not using an ORM
im doing it myself
 
so, yes
btw, something about validation...
you mentioned validating string length and stuff like that
 
yea
 
I usually don't do that kind of validation directly in the VO
but on the client side and when the data reaches the server (in Controller for instance in MVC)
I usually don't see it as a Domain rule
but... this might be a borderline case
anyway
 
Well, I guess it boils down to personal perspective
 
2:33 PM
yep
 
I have noticed that
DDD is partial about the way of coding, but mostly its about the mindset
when coding.
 
absolutely, yes
it's just that web and MVC frameworks generally provide convenient helpers to validate that kind of string length thing
and I don't want to pollute" my domain with that
 
That's true. I don't like using libraries in my Models layer
for some reason.
But,,
I'm adapting everything to VO's, but I still havent really understood the benefits from it as opposed to just having string and int values directly in your Entity attributes
I know that VOs supposed to be immutable
But as far as the advantage it makes..
 
type safety
 
Meaning that an Address can't contain a phone number, for example?
 
2:39 PM
for instance, the compiler complaining at build time that you passed an OrderId to that method instead of a UserId
yep
plus, conveying domain intent
 
I guess it makes your domain kind of obeying the domain rules
so that nothing thats not intended to be there
can get in
 
yes
 
In the PEAA book, in the code example
he also got stuff like compareTo() isEqual() etc in his VO's
but I dont think i would be using any of that as I would just do something like
$user1->getName() == $user2->getName()
 
haha
are you sure this would work in PHP ?
 
What you mean with this?
 
2:42 PM
in C# it wouldn't (as far as objects anyway)
 
$user1->getName() == $user2->getName() would work
because
getName() returns a string returned from the
VO
 
does the == operator in PHP compare object addresses or their values ?
oh
 
Both.
 
it shouldn't return a string IMO
 
Hmm..
That means I wouldn't need any getValue() methods
in my VO's
Because I could then just compare the VO object
s
?
 
2:45 PM
getName() returns an object of type Name
otherwise it's just a plain normal accessor, right ?
 
Okay so if it returns an object of type Name. If I would want to print out the values, I would need to do something like $user->getName()->getFirstname ?
 
yes
 
So that means it would be better for the Entities getters to return VOs instead of strings and values
 
about your example, I'm not sure if you were implying that User was a VO or Name was a VO ?
 
Name a VO
 
2:47 PM
okay
so Name needs compareTo() and isEqual() in Java
 
Yea, in PHP == works. :P
 
ok that's cool :)
but you should definitely re-check that, because it will cause weird bugs if it doesn't :)
"it would be better for the Entities getters to return VOs instead of strings and values" => yes. That's the whole point.
As a general rule, there's absolutely no reason to store a field as one type and expose it as another type
so store it as a Name and expose it as a Name
client code will know it is a Name and know what to do with it
 
Okay, I think I know enough for now :D
 
no problem
glad I could help
 
Thank you for your help. I'm glad I could talk to someone experienced in DDD, cuz there are not that many around compared to other design patterns
 
2:53 PM
SO is a great resource if you can express your questions well enough. No doubt you'll find help here
bye, see you later maybe!
 
yes, have a great day!
 
 
4 hours later…
7:22 PM
Hey, me again. :D
 
 
2 hours later…
9:03 PM
I have a question.
If I have a User Entity class that holds a RegisterDate attribute.
Should the VO be called Date
or RegisterDate ?
Or should I call the VO object Date, but have the variable name RegisterDate in my entity?
 
 
1 hour later…
10:11 PM
And second question, where am I supposed to hash my password? In the Password VO or .. ?
thank you in advance.
 

last day (15 days later) »