What is the good example of using DI (Dagger) in t

2019-08-09 06:15发布

I'm trying to create modular architecture for my Android app and now I'm looking into Dagger DI library. It's really awesome tool, but I don't want to misuse it and currently I have numerous questions like:

  • Should I wrap everything (like Activities or Fragments, or event separate Views) into modules or only common used parts of app (likes services for querying data with login, data layer)?
  • Better use with Android annotations. Android Annotations is another cool library I faced but it seems to play well with UI layer making easy ealing with Views, Fragments and removing some boilerplate code. But how doest it play with Dagger?

If you have a good example of open source project using DI please refer to me. I really want to follow best practices.

2条回答
Rolldiameter
2楼-- · 2019-08-09 06:29

Dependency injection is usually used to add a layer of separation and makes things even less coupled. For Android the library I recommend is Dagger2.

I would really suggest you to check this boilerplate since it is fully based on DI using Dagger2.

The dependencies currently handled by the boiler plate are the following:

  • Database dependency: encapsulates all the database operations.
  • Shared preferences dependency: deals with shared preferences.
  • Local files dependency: which deals with saving on files.
  • Analytics dependency: covers all the operation of reporting events to your analytics backend (GA, Segment, FB, Flurry ..)
  • Logging dependency: encapsulates all the operations related to logging to your console
  • Api dependency: encapsulates all the API related operations

The power of dependency injection comes really handy especially for testing since you can easily switch your dependencies in the test environment to dummy dependencies.

查看更多
甜甜的少女心
3楼-- · 2019-08-09 06:46

Take a look at:

查看更多
登录 后发表回答