I have a Person class which contains information like:
- personId;
- firstName;
- lastName;
- birthDate;
- identityCardNumber;
- address (contained Address object);
- ... a lot more data about the person.
Whenever I need to perform operations on a person’s data, an object from this class provides all the info I need.
Now, I also have a page where a list of persona data is displayed. The list contains basic information about a person, allowing a user to select a row from the list and see detailed info about that person.
Because I do not want to return full Person objects from the database (could be for example 100 persons, but the user will just look at the details of a few) I am using another object for the list elements. This object is an instance of PersonData, which contains data such as:
- personId;
- firstName;
- lastName;
- ... some more data also found on Person, but on a much much smaller scale.
Because these classes share the same information and basically represent the same data, to avoid code duplication I was thinking of making PersonData the super class of Person. But, Person IS A PersonData does not sound right.
The question is, should I inherit from PersonData for the Person class or should I keep both separate? I could return incomplete Person objects for the list, instead of PersonData objects, but I don’t like that either.
How can I remove the code duplication but also have a clean design in the end, something intuitive and easy to maintain?
How would you tackle such a situation?