1

What is the best way to name two classes that describe the same object, but where one of those classes does not hold the complete information? And should I make one of the classes inherit from the other?

I want to have (for the sake of example) an API that gets all people to display a list of names, and then when a person has been selected, another API that gets the detailed content for that person.

Here is an example of the classes I am currently using, and demonstrating my current decisions: the naming conventions, and that one should derive from the other.

    public class PersonBase
    {
        public int PersonId { get; set; }
        public string Name { get; set; }
    }

    public class Person : PersonBase
    {
        public Date DateOfBirth { get; set; }
        // Other properties...
    }

EDIT: The reason I have chosen to separate into two classes for the same entity is because the database query for the complete Person is a lot more complex/intensive, and therefore I want a lightweight version that is used when searching for a particular person in a list.

Note that what I have called PersonBase actually contains some other properties that are used to filter on (you could imagine IsManager or similar), so in my opinion it does represent the person, but simply does not have the full information.

  • 4
    Instead of a class hierarchy, use containment. So, PersonBase has a method getDetails() which returns Optional<Details>. – BobDalgleish Feb 13 at 18:14
  • @BobDalgleish that’ll be worth an answer! – Christophe Feb 13 at 18:58
  • @Mecki (and BobDalgleish) I've added some context for why I currently have two classes, and why I think they represent the same object. – mft25 Feb 14 at 8:58
  • @BobDalgleish I don't quite understand how your answer would look in code, I found this thread which discusses optional properties but I don't think it has any useful info for me. For the record I read the query results directly into the class using Dapper, so I can't easily set some kind of value indicating that all details are included. I will have a mapping layer, but the problem is still there for the DTO classes. – mft25 Feb 14 at 8:58
2

Details on why you actually want to split an object into two objects would have been helpful to answer that question.

In your given example, I would name the objects PersonName and Person, as that's what the objects are: One just represents the name of a person and the other one a person as a whole. And this naming implies that inheritance is not meaningful as a person is not a person name and a person name is not a person. The correct relationship is a person has a person name.

| improve this answer | |
1

You shouldn't automatically use inheritance to represent variable amounts of data. It would be better to provide optional values from the base class, using Optional results.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.