A Pattern for Wicket Data Providers
01 Dec 2016Apache Wicket provides the AjaxFallbackDefaultDataTable1 for displaying a paged view of a large data set, and this component requires an implementation of ISortableDataProvider to populate the rows. This data provider has several responsibilities:
- Keep track of the current sort order.
- Store any search parameters in a
Serializable
form. - Fetch a count of rows that match the search parameters
- Provide an iterator over a subset of rows that match the search parameter, ordered by the current sort.
If you are satisfied with sorting on a single column at any time, the best approach is to extend the abstract class SortableDataProvider, which handles all the work involved in tracking the single sort property.
As the data provider must be Serializable
, it cannot reference the database directly, but must find a database connection via some type of service locator. I’ve used Wicket-Spring to inject a repository object that handles all search and sorting logic. A single instance of the repository object can be shared between many data providers, so the search and sorting parameters have to be passed in with every request:
The data provider is then only responsible for passing parameters to the repository. ClientSearchParams
is a serializable bean class with get and set methods for each search field. Note that the ClientData
returned from this data provider is Serializable
and small, so can be stored directly in a model. More complex return types might benefit from wrapping the data in a LoadableDetachableModel
.
While the data provider could access a database connection directly via @SpringBean
, this would make unit test of pages containing the data provider extremely difficult. Instead, unit tests can simply mock ClientSearchBackend
and return fake data for the count
and list
calls.
For completeness, the Kotlin version of the data provider looks like this. Mostly similar, but handling the possibility of a null sort
property is less verbose:
Originally published by Adrian Cox at https://adrianathumboldt.github.io/.
-
Other repeating views are available. ↩