Skip to content

Commit 7e3dee1

Browse files
committed
Refactored Pager interface. Removed overloads and only the signature with required parameters is left. Optional parameters are now added with a fluent interface.
1 parent a96b3fe commit 7e3dee1

File tree

9 files changed

+344
-283
lines changed

9 files changed

+344
-283
lines changed

Screenshot.png

28.8 KB
Loading

src/MvcPaging.Demo/Views/Paging/ProductsByCategory.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</tbody>
3232
</table>
3333
<div class="pager">
34-
@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, new { categoryname = ViewBag.CategoryDisplayName } )
34+
@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount).Options(o => o.AddRouteValue("categoryname", ViewBag.CategoryDisplayName))
3535
Displaying @Model.ItemStart - @Model.ItemEnd of @Model.TotalItemCount item(s)
3636
</div>
3737
}

src/MvcPaging.Demo/Views/Shared/_ProductGrid.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
</tbody>
1717
</table>
1818
<div class="pager">
19-
@Ajax.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, "AjaxPage", new AjaxOptions { UpdateTargetId = "gridcontainer"})
19+
@Ajax.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, new AjaxOptions { UpdateTargetId = "gridcontainer"}).Options(o => o.Action("AjaxPage"))
2020
</div>

src/MvcPaging.Tests/PagerTests.cs

+95-71
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@
77

88
namespace MvcPaging.Tests
99
{
10-
[TestFixture]
11-
public class PagerTests
12-
{
13-
14-
internal class PagionationComparer : IComparer
15-
{
16-
17-
public int Compare(object x, object y)
18-
{
19-
PaginationModel first = (PaginationModel)x;
20-
PaginationModel second = (PaginationModel)y;
21-
22-
var displayTextResult = first.DisplayText.CompareTo(second.DisplayText);
23-
if (displayTextResult != 0) return displayTextResult;
24-
25-
if (!first.PageIndex.HasValue && second.PageIndex.HasValue) return -1;
26-
if (first.PageIndex.HasValue && !second.PageIndex.HasValue) return 1;
27-
if (first.PageIndex.HasValue && second.PageIndex.HasValue)
28-
{
29-
var pageIndexResult = first.PageIndex.Value.CompareTo(second.PageIndex.Value);
30-
if (pageIndexResult != 0) return pageIndexResult;
31-
}
32-
var activeResult = first.Active.CompareTo(second.Active);
33-
if (activeResult != 0) return activeResult;
34-
var isCurrentResult = first.IsCurrent.CompareTo(second.IsCurrent);
35-
if (isCurrentResult != 0) return isCurrentResult;
36-
37-
return 0;
38-
}
39-
}
40-
41-
[Test]
42-
public void Can_Build_Correct_Model_For_5_Items_With_2_Item_Per_Page()
43-
{
44-
// Assemble
45-
var pager = new Pager(null, 2, 1, 5, null, null);
46-
var expectedPagination = new List<PaginationModel>()
10+
[TestFixture]
11+
public class PagerTests
12+
{
13+
14+
internal class PaginationComparer : IComparer
15+
{
16+
17+
public int Compare(object x, object y)
18+
{
19+
PaginationModel first = (PaginationModel)x;
20+
PaginationModel second = (PaginationModel)y;
21+
22+
var displayTextResult = first.DisplayText.CompareTo(second.DisplayText);
23+
if (displayTextResult != 0) return displayTextResult;
24+
25+
if (!first.PageIndex.HasValue && second.PageIndex.HasValue) return -1;
26+
if (first.PageIndex.HasValue && !second.PageIndex.HasValue) return 1;
27+
if (first.PageIndex.HasValue && second.PageIndex.HasValue)
28+
{
29+
var pageIndexResult = first.PageIndex.Value.CompareTo(second.PageIndex.Value);
30+
if (pageIndexResult != 0) return pageIndexResult;
31+
}
32+
var activeResult = first.Active.CompareTo(second.Active);
33+
if (activeResult != 0) return activeResult;
34+
var isCurrentResult = first.IsCurrent.CompareTo(second.IsCurrent);
35+
if (isCurrentResult != 0) return isCurrentResult;
36+
37+
return 0;
38+
}
39+
}
40+
41+
[Test]
42+
public void Can_Build_Correct_Model_For_5_Items_With_2_Item_Per_Page()
43+
{
44+
// Assemble
45+
var pager = new Pager(null, 2, 1, 5);
46+
var expectedPagination = new List<PaginationModel>()
4747
{
4848
new PaginationModel { Active = false, DisplayText = "«" },
4949
new PaginationModel { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true },
@@ -52,21 +52,20 @@ public void Can_Build_Correct_Model_For_5_Items_With_2_Item_Per_Page()
5252
new PaginationModel { Active = true, DisplayText = "»", PageIndex = 2 }
5353
};
5454

55-
// Act
56-
var result = pager.BuildPaginationModel();
57-
58-
// Assert
59-
60-
Assert.AreEqual(expectedPagination.Count, result.Count);
61-
CollectionAssert.AreEqual(expectedPagination, result, new PagionationComparer());
62-
}
63-
64-
[Test]
65-
public void Can_Build_Correct_Model_For_10_Items_With_2_Item_Per_Page()
66-
{
67-
// Assemble
68-
var pager = new Pager(null, 2, 3, 10, null, null);
69-
var expectedPagination = new List<PaginationModel>()
55+
// Act
56+
var result = pager.BuildPaginationModel();
57+
58+
// Assert
59+
Assert.AreEqual(expectedPagination.Count, result.Count());
60+
CollectionAssert.AreEqual(expectedPagination, result, new PaginationComparer());
61+
}
62+
63+
[Test]
64+
public void Can_Build_Correct_Model_For_10_Items_With_2_Item_Per_Page()
65+
{
66+
// Assemble
67+
var pager = new Pager(null, 2, 3, 10);
68+
var expectedPagination = new List<PaginationModel>()
7069
{
7170
new PaginationModel { Active = true, DisplayText = "«", PageIndex = 2 },
7271
new PaginationModel { Active = true, DisplayText = "1", PageIndex = 1 },
@@ -77,22 +76,22 @@ public void Can_Build_Correct_Model_For_10_Items_With_2_Item_Per_Page()
7776
new PaginationModel { Active = true, DisplayText = "»", PageIndex = 4 }
7877
};
7978

80-
// Act
81-
var result = pager.BuildPaginationModel();
79+
// Act
80+
var result = pager.BuildPaginationModel();
8281

83-
// Assert
82+
// Assert
8483

85-
Assert.AreEqual(expectedPagination.Count, result.Count);
86-
CollectionAssert.AreEqual(expectedPagination, result, new PagionationComparer());
87-
}
84+
Assert.AreEqual(expectedPagination.Count, result.Count());
85+
CollectionAssert.AreEqual(expectedPagination, result, new PaginationComparer());
86+
}
8887

8988

90-
[Test]
91-
public void Can_Build_Correct_Model_For_33_Items_With_2_Item_Per_Page()
92-
{
93-
// Assemble
94-
var pager = new Pager(null, 2, 13, 33, null, null);
95-
var expectedPagination = new List<PaginationModel>()
89+
[Test]
90+
public void Can_Build_Correct_Model_For_33_Items_With_2_Item_Per_Page()
91+
{
92+
// Assemble
93+
var pager = new Pager(null, 2, 13, 33);
94+
var expectedPagination = new List<PaginationModel>()
9695
{
9796
new PaginationModel { Active = true, DisplayText = "«", PageIndex = 12 },
9897
new PaginationModel { Active = true, DisplayText = "1", PageIndex = 1 },
@@ -111,14 +110,39 @@ public void Can_Build_Correct_Model_For_33_Items_With_2_Item_Per_Page()
111110
new PaginationModel { Active = true, DisplayText = "»", PageIndex = 14 }
112111
};
113112

114-
// Act
115-
var result = pager.BuildPaginationModel();
113+
// Act
114+
var result = pager.BuildPaginationModel();
115+
116+
// Assert
117+
Assert.AreEqual(expectedPagination.Count, result.Count());
118+
CollectionAssert.AreEqual(expectedPagination, result, new PaginationComparer());
119+
}
116120

117-
// Assert
121+
[Test]
122+
public void Can_Build_Correct_Model_For_33_Items_With_2_Item_Per_Page_And_Max_5_Pages()
123+
{
124+
// Assemble
125+
var pager = new Pager(null, 2, 1, 33).Options(o => o.MaxNrOfPages(5));
126+
var expectedPagination = new List<PaginationModel>()
127+
{
128+
new PaginationModel { Active = false, DisplayText = "«" },
129+
new PaginationModel { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true },
130+
new PaginationModel { Active = true, DisplayText = "2", PageIndex = 2 },
131+
new PaginationModel { Active = true, DisplayText = "3", PageIndex = 3 },
132+
new PaginationModel { Active = true, DisplayText = "4", PageIndex = 4 },
133+
new PaginationModel { Active = true, DisplayText = "5", PageIndex = 5 },
134+
new PaginationModel { Active = true, DisplayText = "..." },
135+
new PaginationModel { Active = true, DisplayText = "16", PageIndex = 16 },
136+
new PaginationModel { Active = true, DisplayText = "17", PageIndex = 17 },
137+
new PaginationModel { Active = true, DisplayText = "»", PageIndex = 2 }
138+
};
118139

119-
Assert.AreEqual(expectedPagination.Count, result.Count);
120-
CollectionAssert.AreEqual(expectedPagination, result, new PagionationComparer());
121-
}
140+
// Act
141+
var result = pager.BuildPaginationModel();
122142

123-
}
143+
// Assert
144+
Assert.AreEqual(expectedPagination.Count, result.Count());
145+
CollectionAssert.AreEqual(expectedPagination, result, new PaginationComparer());
146+
}
147+
}
124148
}

src/MvcPaging/MvcPaging.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
<Compile Include="IPagedList.cs" />
7373
<Compile Include="PagedList.cs" />
7474
<Compile Include="Pager.cs" />
75+
<Compile Include="PagerOptions.cs" />
76+
<Compile Include="PagerOptionsBuilder.cs" />
7577
<Compile Include="PaginationModel.cs" />
7678
<Compile Include="PagingExtensions.cs" />
7779
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)