Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefit is there in wrapping a simple type in a class?

Tags:

c#

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!
}
like image 958
Fenton Avatar asked Sep 03 '12 15:09

Fenton


3 Answers

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.

like image 58
Daniel Hilgarth Avatar answered Nov 07 '22 09:11

Daniel Hilgarth


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.

like image 35
alf Avatar answered Nov 07 '22 07:11

alf


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.)

like image 5
Vlad Avatar answered Nov 07 '22 08:11

Vlad