I have a situation where I am mapping classes that have an inheritance hierarchy. I have a list of items that each class knows that it needs. For example:
public class PersonMapper { private string[] attributes = new[] {'firstName', 'lastName'}; } public class UserMapper : PersonMapper { private string[] attributes = new[] {'username'}; } public class EmployeeMapper : PersonMapper { private string[] attributes = new[] {'employeeId'}; }
I would like to make a method call that returns the aggregate of all of the super classes like this:
EmployeeMapper mapper = new EmployeeMapper(); string[] attributes = mapper.GetAttributes(); Assert.That(attributes, Has.Member('firstname'));
I can think of some solutions, but they seem overly complicated. I feel like there is an elegant solution to this that I am missing. Can anyone help?
Thanks in advance!
You can have a virtual method/property on the superclass called GetAttributes that returns the private array. Then, in each subclass, override it like so. First call the base GetAttribute method to get the base classes array, and the combine that with the subclasses array, and return the new array.
Code sample: