模式的揭露通用接口的非通用版本 [英] Pattern for exposing non-generic version of generic interface

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

问题描述

说我对暴露分页列表如下界面

Say I have the following interface for exposing a paged list

public interface IPagedList<T>
{
    IEnumerable<T> PageResults { get; }
    int CurrentPageIndex { get; }
    int TotalRecordCount { get; }
    int TotalPageCount { get; }        
    int PageSize { get; }
}   

现在我想建立一个呼叫控制

Now I want to create a paging control

public class PagedListPager<T>
{
    public PagedListPager<T>(IPagedList<T> list)
    {
        _list = list;
    }

    public void RenderPager()
    {
        for (int i = 1; i < list.TotalPageCount; i++)
            RenderLink(i);
    }
}



寻呼控制有没有兴趣 T (列表中的实际内容)。它只需要的页数,当前页面等,所以唯一的原因 PagedListPager 是通用的,这样它会与普通的 IPagedList<编译; T> ; paramater

The paging control has no interest in T (the actual contents of the list). It only requires the number of pages, current page etc. So the only reason PagedListPager is generic is so that it will compile with the generic IPagedList<T> paramater.

这是一个代码味道?如果我不在乎,我有效地具有冗余通用?

Is this a code smell? Should I care that I effectively have a redundant generic?

有没有在这样的情况下暴露接口的额外的非通用版本的标准模式,所以我可以消除对寻呼机泛型类型?

Is there a standard pattern in a case like this for exposing an additional non-generic version of the interface, so I can remove the generic type on the pager?

public class PagedListPager(IPagedList list)






修改

我想我也想补充我已经解决了这个问题,目前的方式,并邀请它是否是一个合适的解决方案意见:

I thought I'd also add the current way I've solved this problem and invite comments on whether it's a suitable solution:

public interface IPagedList // non-generic version
{
    IEnumerable<object> PageResults { get; }
    int CurrentPageIndex { get; }
    int TotalRecordCount { get; }
    int TotalPageCount { get; }        
    int PageSize { get; }
}


public class ConcretePagedList<T> : IPagedList<T>, IPagedList
{
    #region IPagedList<T> Members

    public IEnumerable<T> PageResults { get; set; }
    public int CurrentPageIndex { get; set; }
    public int TotalRecordCount { get; set; }
    public int PageSize { get; set; }

    #endregion

    #region IPagedList Members

    IEnumerable<object> IPagedList.PageResults
    {
        get { return PageResults.Cast<object>(); }
    }

    #endregion
}



现在,我可以通过 ConcretePagedList< T> 来非泛型类/函数

推荐答案

在这里,我的做法是使用重新申报 PageResults ,并露出 T 键入

My approach here would be to use new to re-declare the PageResults, and expose the T as a Type:

public interface IPagedList
{
    int CurrentPageIndex { get; }
    int TotalRecordCount { get; }
    int TotalPageCount { get; }        
    int PageSize { get; }

    Type ElementType { get; }
    IEnumerable PageResults { get; }
}   

public interface IPagedList<T> : IPagedList
{
    new IEnumerable<T> PageResults { get; }
}  

这将,但是,需要显式接口实现,即

This will, however, require "explicit interface implementation", i.e.

class Foo : IPagedList<Bar>
{
    /* skipped : IPagedList<Bar> implementation */

    IEnumerable IPagedList.PageResults {
        get { return this.PageResults; } // re-use generic version
    }
    Type IPagedList.ElementType {
        get { return typeof(Bar); }
    }
}

这方法使得通过API完全可用两个通用和非通用API。

This approach makes the API fully usable via both the generic and non-generic API.

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

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