Dependency Injection with Koin [SUBSCRIBER]

栏目: IT技术 · 发布时间: 5年前

Dependency Injection

What is Dependency Injection, and why would we want to use it? In this episode, learn the principles of Dependency Injection, Inversion of Control and testing.

Find out about other popular Dependency Injection solutions, notibly Dagger, and learn why Koin is a better solution.

Learn what Koin is, how it works, and the basic injection method: modules. Learn about the Domain Specific language that Koin uses.

Learn about the sample app, which uses a NASA API to retrieve a daily photo and description and inject resources into your app.

Learn about the NASA API for retrieving the daily photo and description and find out how to obtain your key.

Add the Koin libraries and learn about the existing libraries needed to run the app.

Create a Network Module

Learn about Singles and Factories, then create a module with the Single & Factory DSL calls.

Add the base url needed for the NASA API. This will be the starting point for injection calls.

Create a logging interceptor to see the requests and responses from the API. This will be added to OkHttpClient.

Create the OkHttpClient class with the builder, set network timeouts, and add a logging interceptor.

Create an instance of Moshi with its builder, which is used to convert from JSON to the model classes.

Create an instance of retrofit using the components created earlier. Add the Base Url, Moshi and OkHttpClient.

Create an instance of our NASA API Interface. This is the class that will be returning the photos and descriptions.

Activity Problem

Since the system creates Activities, this episode shows how to add a special class that allows Koin to inject Activities.

Create an AppModule with the DefaultCurrentActivityListener and ActivityRetriever used to inject Activities.

Space Daily

Start Koin

2:05

Learn how to start Koin: adding logging, a context and modules. Then learn to register activity lifecycle callbacks for injecting Activities.

Add the NASAAPIInterface to the MainViewModel and call getDailyPhoto to get the photo and give it to the view.

Create DailyPhotoView for showing the photo and description, and retrieve the ViewModel using our Activity injector.

Testing

Create a test to test the Koin modules, implementing the KoinTest interface. This will test that our modules are defined correctly.

Set up tests by using the @before and @After annotations to start and stop Koin, and use Koin to inject test fields.

Create tests to make sure all the network singles were correctly created. This will make sure each class was created and set up properly.

Who is this for?

This course is for intermediate to advanced users who are interested in using Dependency Injection in their project or that want to replace Dagger 2. The user should be familiar with Android Studio and the Android Framework.

Covered concepts

  • Dependency Injection
  • Adding Koin as a dependency
  • Creating Modules
  • Starting Koin
  • KoinComponent
  • Using a network module with Retrofit

以上所述就是小编给大家介绍的《Dependency Injection with Koin [SUBSCRIBER]》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

离心力:互联网历史与数字化未来

离心力:互联网历史与数字化未来

[英] 乔尼·赖安(Johnny Ryan) / 段铁铮 / 译言·东西文库/电子工业出版社 / 2018-2-1 / 68.00元

★一部详实、严谨的互联网史著作; ★哈佛、斯坦福等高校学生必读书目; ★《互联网的未来》作者乔纳森·L. 齐特雷恩,《独立报》《爱尔兰时报》等知名作者和国外媒体联合推荐。 【内容简介】 虽然互联网从诞生至今,不过是五六十年,但我们已然有必要整理其丰富的历史。未来的数字世界不仅取决于我 们的设想,也取决于它的发展历程,以及互联网伟大先驱们的理想和信念。 本书作者乔尼· ......一起来看看 《离心力:互联网历史与数字化未来》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具