简体   繁体   English

Presenter中的资源/应用上下文(MVP架构)

[英]Resources/App Context in Presenter (MVP architecture)

I need to sort the list using Comparator which depends on Resources . 我需要使用Comparator对列表进行排序,该列表取决于Resources The list (data) is loaded from the repository (Model layer). 列表(数据)是从存储库(模型层)加载的。 How can I do this if a Presenter should not have access to Context ? 如果演示者不应该访问Context怎么办? Should I inject already created Comparator ? 我应该注入已经创建的Comparator吗?

new ProductsPresenter(new ProductsRepository(), productsFragment, new ProductsComparator(context))

(prodcutsFragment is a View layer) (prodcutsFragment是一个View层)

  1. Is it correct? 这是正确的吗?
  2. What if the Presenter needs to use more Context depended classes? 如果Presenter需要使用更多的Context类,该怎么办? If I inject all of them, the constructor will have a lot of parameters. 如果将它们全部注入,则构造函数将具有很多参数。

Here is the code from Android blueprints where for example I'd like to add sorting the list with Comparator depended on Resources. 是Android蓝图中的代码,例如,我想根据资源来添加使用Comparator对列表进行排序。

1 - It's correct. 1-是的。 An improvement would be to use some kind of dependency library, like Dagger, to inject the instances directly, so you don't have to write new in constructor 一种改进是使用某种依赖库(例如Dagger)直接注入实例,因此您不必在构造函数中编写new

2 - IMHO the best way is to inyect classes directly, without Context , to make your Presenter decoupled of Android SDK, so you'll be able to test it more easily 2-恕我直言,最好的方法是在没有Context情况Context直接注入类,以使Presenter与Android SDK脱钩,因此您可以更轻松地对其进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM