This is a normal solution. You have an IPerson interface and you want to access each parent. Therefore, it would be wise to have an interface declaration like this:
interface IPerson { IList<IPerson> GetAllParents(); }
Now you can get the parents from these parents, and then get the parents ... I hope you have an idea. This design is very flexible because it allows you to simulate deep dynamic structures using simple static models.
The implementation is very simple:
class Person : IPerson { IList<IPerson> parents; public Person(IList<IPerson> parents) { this.parents = parents; } public IList<IPerson> GetAllParents() { return parents; } }
In a sense, you need to create some people without parents (some kind of Adam and Eve), and then add children by keeping links to their parents. As you can see, my naive model can handle randomly deep family structures with a very simple interface that is open to the outside.
source share