Inheritance of common interfaces with various common parameters

I'm not sure if this is just something for which I am not using the correct syntax, or if I lack a concept.

So, the basic design is:

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 for the controllers in my MVC project that use this.

So what I wanted to do is

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 is not possible due to the fact that a generic type must be applied in ListModelBase.

The goal is to make sure that I can use the IListController interface to mean that I can assume that it controller.GetList(1, 10)will return a ListModelBase to me, and then I can assume that the returned model will have an identifier on each item in the list.

? , ?

:

, , , , .

, , :

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

IListController - ( List<T> , , IEnumerable):

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();
    }
}

- :

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

All Articles