你如何组织你的匕首2模块和组件? [英] How do you organise your Dagger 2 modules and components?

查看:136
本文介绍了你如何组织你的匕首2模块和组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你有一个特定的软件包,你把所有的匕首相关的类?

或者,你把他们旁边,他们注入相关的类,如:如果你有一个 MainActivityModule MainActivityComponent ,你把它们放在同一个包你的 MainActivity

另外,我见过不少人定义组件内部类,如:一个 ApplicationComponent 这是应用程序中定义的类。你认为这是一个好的做法呢?

解决方案
  

你有,你把所有的匕首有关特定包   班?

     

或者,你把他们旁边,他们注入相关的类,如:如果你   有MainActivityModule和MainActivityComponent,你把他们的   同一个包你MainActivity。

我没有带那么多的经验,但我可以告诉你我的做法。 也许有些人有更多的经验,可以提高该解决方案或提供自己的观点。

我平时组织匕首2班这样的:

   - 二叔
|
+  -  ApplicationComponent类
|
+  - 模块
   |
   +  -  AndroidModule类
   |
   +  -  WebServiceModule类
   |
   +  -  ...
   |
 

  • 包中包含与匕首2和依赖注入相关的类。
  • 在大多数情况下,Android应用程序通常有一个组件 - 在这里它的命名 ApplicationComponent - 我还没有创建Android应用程序有许多匕首但2组件和我见过解决方案,只有一个组成部分。
  • 模块包中包含匕首2模块

我不创造每个活动模块。模块组特定的功能。例如。与系统的界面的共享preferences,EventBus元素强连接(如果您使用的是类似的东西),网络连接等可能位于 AndroidModule 。如果您的项目具有重要的接口WebService的或有很多人,你可以将它们在 WebServiceModule 。如果您的应用程序,例如负责分析网络,并拥有了相关的网络类似的任务了很多接口,你可以组 NetworkModule 这些接口。当你的应用程序很简单,它可能会发生,你将有只有一个模块。当它的复杂,你可以有很多的模块。在我看来,你不应该在一个单独的模块许多接口。当有这样的情况,你可能会考虑将其拆分成独立的模块。您也可以将特定的一些业务逻辑,在一个单独的模块项目。

  

另外,我见过不少人定义组件内   类,例如这里面所定义的ApplicationComponent   应用程序类。你认为这是一个好的做法呢?

我不知道它是否是一个好或坏的做法。我认为没有必要那样做。您可以创建公共静态的get()方法类延伸应用程序类,它会返回<$ C $的实例中C>应用程序作为一个单身。这是非常简单的解决方案,我们应该有一个应用程序类的一个实例。如果我们想嘲弄语境在单元测试中,我们可以接受上下文作为参数,并在应用程序中code,通过应用程序上下文或活动上下文视情况而定。

请注意,这只是我的方法和一些有经验的开发人员可以组织自己的项目在一个不同的,更好的方式。

Do you have a specific package where you put all the Dagger related classes?

Or do you put them next to the relevant class they inject, e.g. if you have an MainActivityModule and MainActivityComponent, you put them in the same package as your MainActivity.

Also, I've seen quite a few people defining components as inner classes, e.g. an ApplicationComponent that is defined inside the Application class. Do you think this is a good practice?

解决方案

Do you have a specific package where you put all the Dagger related classes?

Or do you put them next to the relevant class they inject, e.g. if you have an MainActivityModule and MainActivityComponent, you put them in the same package as your MainActivity.

I don't have much experience with that, but I can show you my approach. Maybe some people with more experience can improve that solution or provide their point of view.

I usually organize Dagger 2 classes like that:

- di
|
+-- ApplicationComponent class
|    
+-- modules
   |
   +-- AndroidModule class
   |
   +-- WebServiceModule class
   |
   +-- ...
   |

  • di package contains classes related with Dagger 2 and dependency injection.
  • in most cases Android application usually has one Component - here it's named ApplicationComponent - I haven't created an Android application with many Dagger 2 components yet and I've seen solutions with only one component.
  • modules package contains Dagger 2 modules

I don't create module per Activity. Modules group specific functionality. E.g. elements strongly connected with system like interface for SharedPreferences, EventBus (if you are using something like that), network connectivity, etc. may be located in AndroidModule. If your project has important interfaces for WebService or there's a lot of them, you can group them in WebServiceModule. If your application is for example responsible for analyzing network and has many interfaces for similar tasks related to the network, you can group these interfaces in NetworkModule. When your application is simple, it may happen that you will have only one module. When it's complicated, you can have many modules. In my opinion, you shouldn't have many interfaces in a single module. When there's such situation you may consider splitting them into separate modules. You can also keep some business logic specific for your project in a separate module.

Also, I've seen quite a few people defining components as inner classes, e.g. an ApplicationComponent that is defined inside the Application class. Do you think this is a good practice?

I'm not sure if it's a good or bad practice. I think there's no need to do that. You can create public static get() method inside class extending Application class, which will return instance of Application as a singleton. It's much simpler solution and we should have only one instance of an Application class. If we want to to mock Context in a unit tests, we can accept Context as a parameter and in an application code, pass Application Context or Activity Context depending on the situation.

Please note, it's just my approach and some more experienced developers may organize their projects in a different and better way.

这篇关于你如何组织你的匕首2模块和组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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