I have this code and I want to keep it graceful.
I am stuck with this inherited issue and I will have to mess up the code if I do this.
Help me keep it elegant. I am not opposed to making changes to the hierarchy; Feel free to change the kernel.
I have these abstract classes (I skipped an unrelated implementation to keep the question content short).
public abstract class Entity : IComparable
{
protected int _ID;
public abstract int ID { get; }
}
public abstract class SortedEntities<T> : IEnumerable<T> where T : Entity
{
Dictionary<int,T> _Entities;
}
And, obviously, an example of inheritance is as follows:
public class Contact : Entity { }
public class Contacts : SortedEntities<Contact> { }
And I also have more than just Contactand Contactsthat inherit from Entityand SortedEntitiesthat all act the same.
At some point in my application, I want to select objects based on and a list of identifiers.
Example code that I want:
Contacts LoadedContacts = new Contacts(); // load and fill somewhere else
// !!!NOT IMPLEMENTED YET!!!
Contacts SelectedContacts = LoadedContacts.GetFromIDList("1,4,7");
Contacts, Contact .
, , SortedEntities, :
public abstract class SortedEntities<T> : IEnumerable<T> where T : Entity
{
public abstract this<T> GetFromIDList(string IDCSV)
{
List<string> idlist = IDCSV.Split(',').ToList<string>();
return this.Where(entity => idlist.Contains(entity.ID.ToString()));
}
}
, , this<T> .
, , - , .
, - LoadedContacts.GetFromIDList("1,4,7"), Contacts, SortedEntities<T>, , , ,
, ?
, ?