General list in the interface

I am trying to implement an interface class containing a list of objects. How to create a general list so that the implementation class defines the list type:

public interface IEntity
{
    Guid EntityID { get; set; }
    Guid ParentEntityID{ get; set; }
    Guid RoleId { get; set; }

    void SetFromEntity();
    void Save();
    bool Validate();
    IQueryable<T> GetAll(); // That is what I would like to do
    List<Guid> Search(string searchQuery);
}
public class Dealer : IEntity
{
   public IQueryable<Dealer> GetAll() { }
}
+3
source share
4 answers

You can do something like this:

public interface IEntity<T>
{
    IQueryable<T> GetAll();
}

public class Dealer : IEntity<Dealer>
{
   public IQueryable<Dealer> GetAll() { }
}
+11
source

You just need to make IEntity itself. Then use the type parameter in the definition of GetAll ().

Here you can change your code:

public interface IEntity<TListItemType>
{
     // stuff snipped

     IQueryable<TListItemType> GetAll();
}

public class Dealer : IEntity<Dealer>
{
   public IQueryable<Dealer> GetAll() { // some impl here }
}
+3
source

Save(), Validate() .. (, -, Dealer), , SRP.

+1

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


All Articles