[英]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层)
Context
depended classes? 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.