'C# - can I derive from class with generic base type?

Intro

I am creating an ASP.NET Web API application with Entity Framework. What I need to do is return different representations of the same resource for one URI, depending on user role. For example, api/employees/1 will return two different objects for admin and standard user:

Standard user

public class EmployeeBasic 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Admin

public class EmployeeExtended : EmployeeBasic 
{
    public decimal Salary { get; set; }
}

The idea and the attempts

For each resource representation, I will need to provide some related classes, let's say Sort Models for example. I was wondering if it is possible to use generic types and inheritance to create a generic repository methods for related representations. I thought of the following way of doing this:

  1. Create some base interface for Sort Models:

     public interface ISortModel<out TBusinessEntity> 
     {
         //
     }
    
  2. Create generic SortModel as a base type for all sort models

      public abstract class SortModel<TDBEntity, TBusinessEntity> : ISortModel<TBusinessEntity>
      {
           // Database sorting
           public abstract IQueryable<TDBEntity> ApplyToQuery(IQueryable<TDBEntity> query);
    
           // Local sorting
           public abstract IEnumerable<TBusinessEntity> ApplyToLocal(IEnumerable<TBusinessEntity> localList);
    
           // ...
           // Some private logic (expression mappers, etc.)
     }
    
  3. Create sort model for basic resource

     public class EmployeeBasicSortModel : SortModel<DBModel.Employee, EmployeeBasic>
     {
         public int FullName { get; set; }
    
         public override IQueryable<DBModel.Employee> ApplyToQuery(IQueryable<DBModel.Employee> query) 
         {
             // implementation
         }
    
         public override IEnumerable<EmployeeBasic> ApplyToLocal(IEnumerable<EmployeeBasic> localList) 
         {
             // implementation
         }
     }
    
  4. Extend the basic sort model and add sorting for the extended resource properties

     public class EmployeeExtendedSortModel : EmployeeBasicSortModel //, ... Is it possible to somehow do that?
     {
         public override IEnumerable<EmployeeExtended> ApplyToLocal(IEnumerable<EmployeeExtended> localList) 
         {
             var partiallyOrderedList = base.ApplyToLocal(localList);
    
             // Add extended sorting
         }
    
         // ... ?
     }
    
  5. Use the above classes to create generic service:

     class EmployeesService() 
     {
         public IList<TEmployee> GetAll<TEmployee>(ISortModel<TEmployee> sortModel)
                where TEmployee : BasicEmployee
         {
             // implementation
         }
     }
    

The problem

When I thought about it for the first time, it seemed pretty simple. But when I started implementing this, I couldn't figure out the way to implement Step 4. Either I am missing something in my C# knowledge (which is quite possible) or this is not possible in the way I am trying to do this.

So the question is: can I create a base class with generic type, derive from it with basic resource as a type and derive one more time with the extended class?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source