通用接口问题 [英] Generic interface problem

查看:165
本文介绍了通用接口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个接口为我所有网格tasks.The任务实现这个接口相关的:

I'd like to have one interface for all my grid related tasks.The tasks implement this interface:

public interface IDataForGrid<T>
{
    IGridResponse<T> GetList(IGridRequest request);
}

的T类始终是一个DTO类。我cant't创建此DTO的一个通用的接口,因为他们没有什么common.Just哑DTO具有特殊属性。

The T type is always a DTO class. I cant't create a common interface for this DTOs because they have nothing common.Just a dumb DTO with particular properties.

我想用这样的:

public class Service1
{
    public IGridResponse CreateResponse(IGridRequest request)
    {

        ...
        IDataForGrid<T> aa;

        if(request == 1) aa = new CustomerGridData;
        if(request == 2) aa = new OrderGridData;

        var coll = aa.GetList();
    }
}

public class CustomerGridData : IDataForGrid<CustomerDTO>
{
   ...
}

现在的问题是我不知道把,而不是T.

The problem is I don't know what to put instead of the T.

推荐答案

也许我错过认识你,但想不出你犯了一个超类,所有的DTO的如 BaseDTO

Maybe Im miss-understanding you, but couldnt you make a super class that all of your DTO's like BaseDTO

然后使用它像这样:

public class CustomerDTO : BaseDTO {}

IDataForGrid<BaseDTO> aa;

var coll = aa.GetList();

这样,你的科尔变量的类型的 IGridResponse&LT; BaseDTO&GT; 所有的DTO对象扩展从。

This way, your coll variable will be of type IGridResponse<BaseDTO> which all of your DTO object extend from.

这有意义吗?

这篇关于通用接口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆