This section explains how to use the ABP for background paging, and incidentally explains how the ABP background paging logic is implemented. It also demonstrates how to use x.pagedlist for front page paging
Complete the task List of additions and deletions, let's talk about the necessary paging function.
First of all, we are glad that ABP has helped us encapsulate the paging implementation, which is really intimate.
Come on, let's go through this section. How to use an ABP for paging.
1. Paging Request DTO Definition
Data transfer objects (data Transfer Objects) are used for data transfer in the application and presentation layers.
The presentation layer incoming data Transfer object (DTO) invokes an application service method, and then the application service executes some specific business logic through the domain object and returns the DTO to the presentation layer. So the presentation layer and the domain layer are completely separated. In applications with good layering, the presentation layer does not directly use domain objects (warehouses, entities).
When we have a paging request in the presentation layer, we typically need to specify two parameters, one to mark the first few pages, and one for each page (typically configuration file configuration).
Because paging is a very common feature, it is advisable to have a corresponding dto to encapsulate both parameters when we have paging requests on the presentation layer.
There is no such common class encapsulated in the ABP source code, but the corresponding paging dto is defined in the Abpzero project.
The corresponding class diagram is as follows:
pagination sorting filter the corresponding DTO class diagram
From there, you can see the following four common DTO definitions, which are mainly:
-
Pagedinputdto: Paging Request DTO
-
Pagedandsortedinputdto: Pagination Sort DTO
-
Pagedsortedandfilteredinputdto: Pagination sorting to filter DTOs
-
Pagedandfilteredinputdto: Pagination Filtering DTOs
is not very practical, our paging is generally used in combination with filtering sorting.
The main attributes are defined as follows:
-
Maxresultcount: The number of rows per page that can be read from a defined configuration.
-
Skipcount: The number of jumps, the general calculation formula is Skipcount=page*maxresultcount (number of pages * lines).
-
Filter: Filtering strings
-
Sorting: How to sort
Concrete implementation is not detailed, I believe that carefully read the class diagram, you can do their own hands-on implementation. Abpzero these public dtos in the application service layer's DTO folder, such as the specific path.
2. How to use pagination DTO
Take our task List example, we modify the createdGetTaskInputDto, let it inherit fromPagedSortedAndFilteredInputDto, so that weGetTaskInputDtohave to do the paging sorting filter needs to use the properties.
public class GetTasksInput : PagedSortedAndFilteredInputDto
{
public TaskState? State { get; set; }
public int? AssignedPersonId { get; set; }
}
3. Return paged result DTO
The ABP has defined a generic pagedresultdto for us to wrap the returned paged results. The main includes two attributes, the total number of int totalcount save,ireadonlylist<t> Items Save the paged result set returned.
4. Application Service Layer Paging logic implementation
(1). Define the interface in Itaskappservice
pagedresultdto<taskdto> getpagedtasks (gettasksinput input);
(2). Implement the interface in Taskappservice:
Public PagedResultDto<TaskDto> GetPagedTasks(GetTasksInput input)
{
//Priority filtering
Var query = _taskRepository.GetAll().Include(t => t.AssignedPerson)
.WhereIf(input.State.HasValue, t => t.State == input.State.Value)
.WhereIf(!input.Filter.IsNullOrEmpty(), t => t.Title.Contains(input.Filter))
.WhereIf(input.AssignedPersonId.HasValue, t => t.AssignedPersonId == input.AssignedPersonId.Value);
//sort
Query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(input.Sorting) : query.OrderByDescending(t => t.CreationTime);
/ / Get the total
Var tasksCount = query.Count();
//Default paging mode
//var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
/ / ABP provides an extension method PageBy paging mode
Var taskList = query.PageBy(input).ToList();
Return new PagedResultDto<TaskDto>(tasksCount,taskList.MapTo<List<TaskDto>>());
}
The implementation of paging is very simple, first filtering, sorting, then paging, and finally using Pagedresultdto package paging results.
Careful you may have found two not used in LINQ methodWhereIfandPageBy, yes, this is the ABP provides extension method, interested can see the specific implementation of the source code, in fact, theQueryableExtensionsimplementation is very simple, but we usually use LINQ but may not be able to imagine.
Here are a few questions to ask:
How many queries do you have in this code?
What paging technology is used in the code? (True paging?) A fake page break? )
5. Using X.pagedlist for front page paging
There is a series of open-source implementations for front-end paging in ASP. I use open source X.pagedlist paging in my demo. For specific source code, please refer to x.pagedlist GitHub.
(1). Install the X.PAGEDLIST.MVC NuGet package in your Web project yourself
X.PAGEDLIST.MVC NuGet Package
(2), using the method provided by X.pagedlist in the controller to construct the paging results for the front-end use
Because we have implemented the paging logic manually in the application service layer, we need to construct the staticpagedlist as the return result according to the X.pagedlist website example.
Public ActionResult PagedList(int? page)
{
/ / number of lines per page
Var pageSize = 5;
Var pageNumber = page ?? 1;//Pages
Var filter = new GetTasksInput
{
SkipCount = (pageNumber - 1) * pageSize, / / ignore the number
MaxResultCount = pageSize
};
Var result = _taskAppService.GetPagedTasks(filter);
/ / has been manually completed in the application service layer paging logic, so you need to manually construct the paging results
Var onePageOfTasks = new StaticPagedList<TaskDto>(result.Items, pageNumber, pageSize, result.TotalCount);
/ / Put the paging results into the ViewBag for View use
ViewBag.OnePageOfTasks = onePageOfTasks;
Return View();
}
You can see from the code that we've constructed the pagination results provided by x.pagedlist and placed it in viewbag for use by the view.
(3). Add a paging control to the view
The code for the Pagedlist view is as follows:
@using X.PagedList.Mvc;
@using Abp.Web.Mvc.Extensions
@using X.PagedList;
<link href="~/Content/PagedList.css" rel="external nofollow" rel="stylesheet" />
<ul class="list-group">
@foreach (var task in ViewBag.OnePageOfTasks)
{
<li class="list-group-item">
<p class="btn-group pull-right">
<button type="button" class="btn btn-info">Edit</button>
<button type="button" class="btn btn-success">Delete</button>
</p>
<p class="media">
<a class="media-left" href="#" rel="external nofollow" >
@*<i class="fa @Model.GetTaskLable(task) fa-3x"></i>*@
</a>
<p class="media-body">
<h4 class="media-heading">@task.Title</h4>
<span class="text-muted">@task.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")</span>
</p>
</p>
</li>
}
</ul>
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfTasks, page => Url.Action("PagedList", new { page }))
The last sentence of the code is used to generate the paging control.
to sum up
This section mainly explains how to use ABP for background paging, and explains the implementation of ABP background paging logic. It also demonstrates how to use X.PagedList for front-end paging.