简体   繁体   English

Kotlin 与 Dagger 2

[英]Kotlin with Dagger 2

We are developing new Android app, only using Kotlin language.我们正在开发新的 Android 应用程序,仅使用 Kotlin 语言。

We want to use Dependency Injection framework.我们想使用依赖注入框架。 Dagger 2 is Java based framework. Dagger 2 是基于 Java 的框架。 As of my knowledge, There is no Kotlin based DI framework from Google.据我所知,谷歌没有基于 Kotlin 的 DI 框架。

  1. Is it advisable to use Dagger 2 with Kotlin?建议将 Dagger 2 与 Kotlin 一起使用吗?
  2. When can we expect Kotlin based DI framework from Google?我们什么时候可以期待 Google 推出基于 Kotlin 的 DI 框架? If so, is it backward compatible with Dagger 2 to adopt quickly?如果是这样,它是否向后兼容 Dagger 2 以快速采用?
  1. Yes, you could use Dagger 2 with Kotlin.是的,你可以在 Kotlin 中使用 Dagger 2。 Kotlin is working over JVM and totally interoperable with Java. Kotlin 在 JVM 上工作,并且可以与 Java 完全互操作。

  2. I haven't heard about such kind of plans.我没有听说过这样的计划。 Just use Dagger 2 on your Kotlin project and you'll see that it works great.只需在您的 Kotlin 项目中使用 Dagger 2,您就会发现它运行良好。

  1. You can use dagger 2 with Kotlin, and there shouldn't be any issues.你可以在 Kotlin 中使用 dagger 2,应该不会有任何问题。
  2. I am not sure if Google is going to develop one, however if dagger compatibility is not an issue then there is an existing DI framework Koin written in Kotlin.我不确定 Google 是否会开发一个,但是如果 dagger 兼容性不是问题,那么现有的 DI 框架Koin是用 Kotlin 编写的。

Dagger 2 should work with Kotlin. Dagger 2 应该与 Kotlin 一起使用。 We know there are a few rough edges for Kotlin users, and we're looking to smooth them over.我们知道 Kotlin 用户有一些粗糙的地方,我们正在努力解决这些问题。

Koin and kodein are not DI containers, but are Service Locators. Koin 和 kodein 不是 DI 容器,而是服务定位器。 If you consider that you need only DI container and Service Locator is not good for you, then probably the Koin/kodein is not a good choice.如果您认为您只需要 DI 容器并且 Service Locator 不适合您,那么 Koin/kodein 可能不是一个好的选择。 The choice is yours.这是你的选择。 The difference can be seen here What's the difference between the Dependency Injection and Service Locator patterns?区别可以在这里看到依赖注入和服务定位器模式之间的区别是什么? . .

Jake Warton conversation also throws light on the same stating杰克沃顿的谈话也揭示了同样的说法

DI pattern > service lookup pattern. DI 模式 > 服务查找模式。 Code generates the boilerplate for manual DI.代码为手动 DI 生成样板。 Compile-time validated for completeness.编译时验证完整性。 Fast at runtime.运行时快。

https://twitter.com/JakeWharton/status/908419644742098944?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E908419644742098944&ref_url=https%3A%2F%2Fmedium.com%2Fmedia%2Ffc6b4ad54c27f761700f5726ed92cfd1%3FpostId%3Df5bc5eaa308e https://twitter.com/JakeWharton/status/908419644742098944?ref_src=twsrc%5Etfw%7Ctwcamp%5Etweetembed%7Ctwterm%5E908419644742098944&ref_url=https%3A%2F%2Fmedium.com%2Fmedia%2Ffc6b4ad54c27f761700f5726ed92cfd1%3FpostId%3Df5bc5eaa308e

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

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