I am looking at code that is essentially passing an id around, for example:
GetPersonById(int personId)
But instead of using an int, it used a PersonId object.
GetPersonById(PersonId personId)
The PersonId object is just an int with some hand-cranked code to make it nullable. So was this created in old .NET when nullable ints weren't available or is there a higher purpose for wrapping simple types in a class?
public sealed class PersonId {
private PersonId() {
_isNull = true;
_value = 0;
}
private PersonId(int value) {
_isNull = false;
_value = value;
}
// and so on!
}
In DDD code like this is used to make the type this ID references more explicit.
Especially in the context of well designed aggregates, references to other aggregates are very often by ID, not by object reference. In this case, those ID value objects can be used to avoid accidential mixing of these IDs and - as said before - to make it more explicit what aggregate is being referenced.
Using a class is a nice way to simplify further refactoring if the fields used to identify a person need to change. Suppose tomorrow the system reaches Int32.MaxValue
people and you want to use an Int64
or a string to identify them. Another case would be, when the requirements for the system are not 100% accurate, and they didn't specify if a person would be identified by an ID, a PIN, or whatever combination of fields that you can think of. The author of this code, might have though of preventing a major refactoring by creating this class.
The answer could be the semantics as well!
Imagine that you have some int
value. Is it an ID
? Or is it age, height in cm, temperature in centigrade, number of TV program? Well, you can give a good name to the variable, but this won't protect you if you make a mistake.
On the contrary, if a function requires an ID class instance (which is, well, int
in disguise) and not just int
, but you are trying to pass X-coordinate value by mistake (which happens to be int
as well, or instance of Coordinate
, or whatever), compiler will catch it and warn you. And besides that, anyone reading the code can clearly see that the needed value is not just int, but an ID -- you cannot do it better with documentation, noone reads it anyway.
(Side note: you cannot imagine, how much headache I had trying to refactor the code which used double
for the angles in both degrees and radians! It was a nightmare! After switching to a class Angle
, everything went much better.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With