說我對暴露分頁列表如下界面模式的揭露通用接口的非通用版本
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)
編輯
我想我也想補充我已經解決了這個問題,目前的方式,並邀請它是否是一個合適的解決方案評論:
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>
到非泛型類/函數
'PagedListPager'類或方法聲明? –
@Gregg抱歉,我會編輯它。 – fearofawhackplanet
我不喜歡在兩個接口中定義屬性,因爲您可以明確地實現它們來執行不同的操作。例如,IPagedList.PageSize {get {return 8;}} IPageList .PageSize {get {return this.PageResults。Count();}}你有第二個接口的唯一原因是提供了強大的輸入,所以Marc的答案似乎消除了該類在屬性上應該有不同結果的能力。 –
MPavlak