Compare base class part of sub class instance to another base class instance
- by Anders Abel
I have number of DTO classes in a system. They are organized in an inheritance hierarchy.
class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string ListName { get; set; }
}
class PersonDetailed : Person
{
public string WorkPhone { get; set; }
public string HomePhone { get; set; }
public byte[] Image { get; set; }
}
The reason for splitting it up is to be able to get a list of people for e.g. search results, without having to drag the heavy image and phone numbers along. Then the full PersonDetail DTO is loaded when the details for one person is selected.
The problem I have run into is comparing these when writing unit tests. Assume I have
Person p1 = myService.GetAPerson();
PersonDetailed p2 = myService.GetAPersonDetailed();
// How do I compare the base class part of p2 to p1?
Assert.AreEqual(p1, p2);
The Assert above will fail, as p1 and p2 are different classes. Is it possible to somehow only compare the base class part of p2 to p1? Should I implement IEquatable<> on Person? Other suggestions?