简体   繁体   English

"C# - 我可以从具有通用基类型的类派生吗?"

[英]C# - can I derive from class with generic base type?

Intro介绍

I am creating an ASP.NET Web API application with Entity Framework.我正在使用实体框架创建一个 ASP.NET Web API 应用程序。 What I need to do is return different representations of the same resource for one URI, depending on user role.我需要做的是为一个 URI 返回同一资源的不同表示,具体取决于用户角色。 For example, api/employees/1 will return two different objects for admin and standard user:例如, api/employees/1将为管理员和标准用户返回两个不同的对象:

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创建泛型 SortModel 作为所有排序模型的基本类型

     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.但是当我开始实现这个时,我无法弄清楚实现第 4 步的方法。要么我在我的 C# 知识中遗漏了一些东西(这很可能),要么这在我试图做到这一点的方式中是不可能的。

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?所以问题是:我可以创建一个具有泛型类型的基类,从它派生出基本资源作为一种类型,然后再派生一次扩展类吗?

Holy goodness this is a complicated question.天哪,这是一个复杂的问题。 The generics are a huge red herring.仿制药是一个巨大的红鲱鱼。 Ignore the generics;忽略泛型; the problem is more fundamental.问题更为根本。 Let's simplify it greatly.让我们大大简化它。

class Animal {}
class Mammal : Animal {}
class Tiger : Mammal {}

class Shape {}
class Square : Shape {}
class GreenSquare : Square {}

class B
{
  public virtual Mammal Frob(Square s) { ... }
}

class D : B
{
  public override SomeReturnType Frob(SomeArgumentType m) { ... }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 4.0 中,是否可以从泛型类型参数派生 class? - In C# 4.0, is it possible to derive a class from a generic type parameter? C#:如何从基类属性中导出new属性的值? - C#: how can I derive the value of new attribute from the base class attribute? 如何从基类派生并在C#中实现接口? - How can I derive from a base class and implement an interface in C#? 我想在C#中从onLoad()上的基类访问派生类的控件(母版页从一个masterClass派生) - I want to access controls of derive class from base class on onLoad() override in c# (Master page derive from one masterClass) 如何找到其泛型类型派生自给定基类型的所有DbSet? - How can I find all DbSets whose generic types derive from a given base type? 如何找到当前项目的所有引用,并确定哪些引用源自C#中的特定基类? - How can I find out all the references for the current project and tell which ones derive from a particular base class in C#? 我可以在C#中为一个班级派生一个孙子吗? - Can I derive a grandchild for a class in C#? 获取派生自基类c#的组件 - Get components that derive from base class c# 基类中的C#泛型类型 - C# generic type in a base class 如何从基础泛型类派生泛型类? - How to derive generic class from base generic class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM