项目作者: beyama

项目描述 :
Kotlin Dependency Injection
高级语言: Kotlin
项目地址: git://github.com/beyama/winter.git
创建时间: 2017-10-20T06:46:30Z
项目社区:https://github.com/beyama/winter

开源协议:Apache License 2.0

下载


Winter

Kotlin 1.3.40
Maven Central
Travis
MIT License
GitHub issues

Kotlin Dependency Injection

Winter is a fast and intuitive dependency injection library for Kotlin on Android and the JVM.

It offers an idiomatic Kotlin API as well as optional JSR-330 support
with annotation processor.

Winter documentation

Installation

  1. dependencies {
  2. // Core
  3. implementation 'io.jentz.winter:winter:0.7.0'
  4. // AndroidX support
  5. implementation 'io.jentz.winter:winter-androidx:0.7.0'
  6. // RxJava 2 disposable plugin
  7. implementation 'io.jentz.winter:winter-rxjava2:0.7.0'
  8. // Java support
  9. implementation 'io.jentz.winter:winter-java:0.7.0'
  10. // JUnit 4 test support
  11. testImplementation 'io.jentz.winter:winter-junit4:0.7.0'
  12. // JUnit 5 test support
  13. testImplementation 'io.jentz.winter:winter-junit5:0.7.0'
  14. // Optional JSR-330 support
  15. kapt 'io.jentz.winter:winter-compiler:0.7.0'
  16. }

Quickstart Android

Given is an Android application with an application component, a presentation subcomponent that
survives orientation changes and an activity subcomponent.

  1. class MyApplication : Application() {
  2. override fun onCreate() {
  3. // define application component
  4. Winter.component(ApplicationScope::class) {
  5. singleton<HttpClient> { HttpClientImpl() }
  6. singleton<GitHubApi> { GitHubApiImpl(instance()) }
  7. // define presentation subcomponent
  8. subcomponent(PresentationScope::class) {
  9. singleton<ReposListViewModel> { RepolistViewModel(instance<GitHubApi>()) }
  10. // define activity subcomponent
  11. subcomponent(ActivityScope::class) {
  12. singleton { Glide.with(instance()) }
  13. }
  14. }
  15. }
  16. // Configure the injection adapter to use
  17. Winter.useAndroidPresentationScopeAdapter()
  18. // Create application dependency graph
  19. Winter.inject(this)
  20. }
  21. }
  22. class MyActivity : Activity() {
  23. private val viewModel: RepoListViewModel by inject()
  24. private val glide: RequestManager by inject()
  25. override fun onCreate(savedInstanceState: Bundle?) {
  26. Winter.inject(this)
  27. super.onCreate(savedInstanceState)
  28. }
  29. }

Read more

Winter is used in production since end of 2017. The API is considered mostly stable but there will
be no guarantee before version 1.x.

Winter supports only Android and JVM right now but support for native (iOS) and JavaScript is planed.

Winter documentation

License

  1. Copyright 2017 Alexander Jentz
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.