Where () at an abstract level to bring back a new

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 
{
    // !!!NOT REAL CODE YET!!!
    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>, , , ,

, ?
, ?

+3
1

, "" (, this "" ):

public abstract class Entity : IComparable
{
    protected int _ID;
    public abstract int ID { get; }
}

public abstract class SortedEntities<TEntities, TEntity> : IEnumerable<TEntity>
    where TEntities : SortedEntities<TEntities, TEntity>, new()
    where TEntity : Entity
{
    Dictionary<int, TEntity> _Entities;

    public TEntities GetFromIDList(string IDCSV)
    {
        List<string> ids = IDCSV.Split(',').ToList<string>();
        return new TEntities
        {
            _Entities = this.Where(entity => ids.Contains(entity.ID.ToString()))
                            .ToDictionary(e => e.ID)
        };
    }
}

:

public class Contact : Entity
{
}

public class Contacts : SortedEntities<Contacts, Contact>
{
}

, TEntities SortedEntities<TEntities, TEntity>. , TEntities , , X SortedEntities<X, Y>, .

new(), "current".

- , . !

+1

Source: https://habr.com/ru/post/1765379/


All Articles