具有清晰架构的Android分页库 [英] Android paging library with clean architecture

本文介绍了具有清晰架构的Android分页库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Android体系结构组件中的分页库,但是我有怀疑将其集成到基于干净架构的项目中.

I was trying the paging library from Android Architecture Component but I have doubts integrating it in a clean architecture based project.

通常我有3个模块:

  • 主模块(应用程序)
  • 数据模块(具有网络和数据库依赖性的Android模块)
  • 域模块(纯Kotlin模块)

为了引入分页,我不得不考虑将 PagedList< T> 类作为域类.(IMO并不是一个糟糕的主意,因为最后是一个列表,并且数据源是抽象的)

In order to introduce pagination, I had to consider PagedList<T> class as a domain class. (IMO is not a terrible idea since in the end is a list and the data source is abstracted)

因此在域层中,我可以有一个像这样的笔迹:

So in the domain layer I can have a repostiory like:

interface ItemRepository {
    fun getItems():PagedList<Item>
}

然后在数据模块中创建如下实现:

And then in the data module create the implementation like this:

class ItemRepositoryImpl: ItemRepositoy(private val factory:ItemDataSourceFavtory) {
    fun getItems():PagedList<Item> {

  val pageConfigurations = PagedList.Config.Builder()
            .setPageSize(10)
            .setInitialLoadSizeHint(15)
            .setPrefetchDistance(5)
            .setEnablePlaceholders(false)
            .build()

    return RxPagedListBuilder(locationDataSourceFactory, pageConfigurations)
            .setInitialLoadKey(1)
            .buildObservable()
}

到目前为止,一切都很好.我的疑问是,当我们需要转换表示层的域模型时(假设我的项目需要知道是否已选中以显示选中的图标),通常我会将我的域模型映射到一个表示层中.

So far so good. My doubt is when we need to transform the domains model for the presentation layer (let's say my item needs to be aware if was checked to show a checked Icon) normally I would map my domain model into a presentation one.

我知道的 DataSourceFactory 具有 map mapByPage 方法,但是工厂位于数据层.我的Viewmodel消耗了来自模型层的数据,在这种情况下,该数据将是 PagedList ,据我所知,分页列表不支持映射.

Im aware DataSourceFactory has map and mapByPage methods, but the factory resides in the data layer. My Viewmodel consumes data from the model layer which in this cases would be a PagedList, and as far as I know, paged list doesn´t support mapping.

那么在这种情况下应该做些什么呢?

So what would be the appropiate thing to do in this situation?

推荐答案

您无法将 PagedList 映射到演示模型中,因为 PagedListAdapter 需要 PagedList 加载下一页/上一页.

You cannot map PagedList into presentation model, since PagedListAdapter need the PagedList to load next/previous pages.

PagedList具有两个主要功能,首先是它是一个数据结构,用于使用保留要分页的项目的 List (部分存在和不存在).snapshot(),您可以轻松获取当前项目,将域模型映射到演示文稿中,然后将其传递给

The PagedList have two main function, firstly it's a data structure to hold a List of items that are paged (Part present and part absent), using snapshot() you can easily get the present items, map domain model into a presentation one and pass it to the ListAdapter to show list of items.

其次,当需要更多页面时,它必须警告 DataSource . PagedListAdapter 收到一个 PagedList ,并且在绑定项目时,它依赖于 PagedList loadAround()方法来检测新的页面是必需的.

Secondly it has to alert DataSource when more pages are needed. PagedListAdapter receive a PagedList, and upon binding items it depend on loadAround() method of PagedList to detect when new pages are needed.

免责声明:这只是我的观点,可以讨论但默认情况下 PagingLibrary 并不是一个干净的解决方案,最简单的方法是在获取模型(网络或数据库)后在 DataStore 中映射 domain 模型并将具有 presentation 模型的 PagedList 传递到表示层.映射PagedList本身不合逻辑(尽管可能),因为它与 View 中的 PagedListAdapter 紧密耦合.

DISCLAIMER: This is just my opinion and open to discussion but PagingLibrary is not a clean solution by default, the easiest way is to map domain model inside DataStore upon fetching them (either network or db) and pass the PagedList with the presentation models to the presentation layer. Mapping PagedList itself is not logical (though possible) as it is tightly coupled with PagedListAdapter in View.

这篇关于具有清晰架构的Android分页库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆