I've been given a simple (or not) task to perform and I'm wondering how I can realise the implementation in the most flexible way possible. So, suppose I'm given an interface
public interface IPerson
{
int GetId();
string GetFirstName();
string GetLastName();
DateTime GetDateOfBirth();
int GetHeight();
//More methods can be added later...
}
In short, there could be many properties added which can define an object Person. I've been asked to implement a custom collection named "PersonCollection" with the following rules: 1) It should utilize an "Add method" - adds the person object which is given as input. 2) (And this is the trickiest part) Remove - removes the person object with the maximum value and returns it. Now, what does it mean that a person has the maximum value? There may be many possible "algorithms" to determine that e.g. by first name/ last name/ height/ or any new properties of this interface that will be added in the future; you do not know in advance what algorithms may apply and which one should be used. Therefore, the implementation must be as flexible as possible by supporting all potential algorithms. For example: if the developer of Person interface somehow changes its properties, then this should still not affect your implementation of PersonCollection in any way! So, how can I make one "generic" method of finding the max value of any given property? Is it even possible?
This is a beginning of my implementation of the class:
public class PersonCollection : IEnumerable<IPerson>
{
private PersonCollection _pCollection;
public PersonCollection(PersonCollection pCollection)
{
_pCollection = pCollection;
}
public async Task Add(IPerson person)
{
await _pCollection.Add(person);
}
public IEnumerator<IPerson> GetEnumerator()
{
return _pCollection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
At first I thought of using Linq queries, but each query can access only one property using a lambda, so if someone will add a property to IPerson then my implementation will not cover it.
Any ideas or suggestions will be greatly appreciated Thank you