Skip to content

Yummypets/Inje.kt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Inje.kt

Language: Kotlin Platform: Android 8+ License: MIT

Simple Kotlin injection under 30 lines

// Register dependency on App start.
DependencyInjection.register<UserRepository> { ApiUserRepository }

// Get it later on.
val userRepository = DependencyInjection.getDependency<UserRepository>()

About

  • Simple & type-safe api (generics FTW)
  • Lightweight: 1 file ~ 30 lines of pure kotlin
  • Does NOT rely on evil Annotations

Installation

  • Copy and paste Inje.kt file in your project.
  • Make sure you import the kotlin-reflect on which Inject relies:
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
  • There is no step 3 \o/

Usage

1. Register your dependencies on App Start

class MainApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        // Register your dependency by providing
        // a concrete object for a given Interface.
        DependencyInjection.register<UserRepository> { ApiUserRepository }
    }
}

2. Add the HasDependencies interface

3. Get the dependency anywhere you want

class UserFragment : Fragment(), HasDependencies {

  // Get your dependency.
  val userRepository: UserRepository = getDependency()

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)

      // Use it!
      userRepository.doSomeCoolStuff()
  }
}

Contributors

PAFagniez, S4cha

About

Simple Kotlin injection under 30 lines

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages