You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android MVVM项目中ItemsPagingSource文件的合理放置位置

MVVM架构下ItemsPagingSource的合理放置位置

你的项目现有结构如下:

src
 \_ domain
     \_ model
     \_ repository
          \_ ItemsRepository
 \_ data
     \_ repository
          \_ ItemsRepositoryImpl
 \_ presentations
     \_ items
          \_ ItemsScreen
          \_ ItemsViewModel

合理的放置位置

将ItemsPagingSource放在data层下新增的paging子目录中,调整后的结构如下:

src
 \_ domain
     \_ model
     \_ repository
          \_ ItemsRepository
 \_ data
     \_ repository
          \_ ItemsRepositoryImpl
     \_ paging
          \_ ItemsPagingSource
 \_ presentations
     \_ items
          \_ ItemsScreen
          \_ ItemsViewModel

原因说明

  • ItemsPagingSource是Jetpack Paging库中负责具体分页数据加载的组件,属于数据层的实现细节,不涉及业务规则或抽象定义,完全符合data层的职责范围(处理数据获取、缓存、框架相关的数据操作)
  • domain层的ItemsRepository是抽象接口,只定义业务所需的数据契约,不需要依赖PagingSource这类框架特定类;而data层的ItemsRepositoryImpl作为接口的实现类,会直接使用ItemsPagingSource构建分页数据流,这符合依赖倒置原则
  • 单独用paging目录归类分页相关组件,能让data层结构更清晰,便于后续维护和扩展其他分页逻辑

内容的提问来源于stack exchange,提问作者Always Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 16:32:00