假设我有以下用于公开分页列表的界面
public interface IPagedList<T>
{
IEnumerable<T> PageResults { get; }
int CurrentPageIndex { get; }
int TotalRecordCount { get; }
int TotalPageCount { get; }
int PageSize { get; }
}
现在我想创建一个分页控件
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>
范围。
这是代码味道吗?我应该关心我实际上有一个多余的泛型吗?
在这样的情况下是否有一个标准模式用于公开接口的附加非通用版本,以便我可以删除寻呼机上的通用类型?
public class PagedListPager(IPagedList list)
Edit
我想我还应该添加目前解决这个问题的方法,并邀请大家评论它是否是一个合适的解决方案:
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>
到非泛型类/函数