简体   繁体   English

使用不同的泛型参数继承通用接口

[英]Inheriting generic interfaces with different generic parameters

I'm not sure if this is just something that I'm not using the right syntax for, or if I'm missing a concept. 我不确定这是否只是我没有使用正确的语法,或者如果我错过了一个概念。

So the basic is design is this: 所以基本设计是这样的:

public abstract class ListItemModelBase
{
    Guid id { get; set; }
}

public abstract class ListModelBase<T> where T : ListItemModelBase
{
    List<T> Items { get; set; }
}

public class OrderListModel : ListModelBase<OrderListItemModel>
{
}

This all works fine, but now I want to add some inheritance to the controllers in my MVC project that use this. 这一切都运行正常,但现在我想为我的MVC项目中使用它的控制器添加一些继承。

So what I wanted to do was 所以我想做的是

public interface IListController<T> where T : ListModelBase<ListItemModelBase>
{
    T GetList(int Page, int ItemsPerPage);
}

Then I could do: 然后我可以这样做:

public class OrderListController : IListController<OrderListModel>, BaseController
{
    public OrderListModel GetList(int Page, int ItemsPerPage)
    {

    }
}

But obviously this isn't possible due to the fact that ListModelBase needs to have a generic type applied. 但显然这是不可能的,因为ListModelBase需要应用泛型类型。

The goal here is to make it so that I can use the IListController interface to mean that I can assume that controller.GetList(1, 10) will return me a ListModelBase, then I can subsequently assume that The model returned will have a an Id on each of the Items in the list part. 这里的目标是使我能够使用IListController接口来表示我可以假设controller.GetList(1, 10) 1,10 controller.GetList(1, 10)将返回一个ListModelBase,然后我可以随后假设返回的模型将具有一个Id在列表部分中的每个项目上。

Is this possible? 这可能吗? or is there a better way of doing what I want to do? 或者有更好的方式做我想做的事情吗?

UPDATE: 更新:

To be clear, I know that I can define multiple types at the controller level, but that isn't what I want. 要清楚,我知道我可以在控制器级别定义多种类型,但这不是我想要的。

So I know I can do this: 所以我知道我可以这样做:

public interface IListController<T, TItemModel> 
      where T : ListModelBase<TListModel>
      where TItemModel : ListItemModelBase
{
    T GetList(int Page, int ItemsPerPage);
}

Loose the constraint on IListController and do something like (don't expose List<T> on an interface if you can make due with a lesser type, like IEnumerable ): 松开IListController上的约束并做类似的事情(如果你可以使用较小的类型,比如IEnumerable ,则不要在接口上公开List<T> ):

public abstract class ListItemModelBase
{
    public Guid Id { get; set; }
}

public class ListItem : ListItemModelBase
{
}

public abstract class ListModelBase<T> where T : ListItemModelBase
{
    public IEnumerable<T> Items { get; set; }
}

public class OrderListModel : ListModelBase<ListItem>
{
}

public interface IListController<T>
{
    T GetList(int page, int itemsPerPage);
}

public abstract class BaseController
{
}

public class OrderListController : BaseController, IListController<OrderListModel>
{
    public OrderListModel GetList(int page, int itemsPerPage)
    {
        return new OrderListModel();
    }
}

Then you can do something like: 然后你可以这样做:

var controller = new OrderListController();
var orderListModel = controller.GetList(0, 10);
foreach (var kindOfA in orderListModel.Items)
{
    doSomeThing(kindOfA.Id);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM