Skip to content

Instantly share code, notes, and snippets.

@seadowg
Created December 11, 2024 10:05
Show Gist options
  • Save seadowg/a61930ff691ad75079019c38dd93e7c1 to your computer and use it in GitHub Desktop.
Save seadowg/a61930ff691ad75079019c38dd93e7c1 to your computer and use it in GitHub Desktop.
Very simple DI for Android
class MyApplication : Application() {
var dependencies: Dependencies = DefaultDependencies()
}
interface Dependencies {
val thing: Thing
}
fun Activity.dependencies(): Dependencies {
return (this.application as MyApplication).dependencies
}
class DefaultDependencies(
override val thing = ThingImplementation()
}
// App module
class MyApplication : Application(), OtherModuleDependencyProvider {
var dependencies: Dependencies = DefaultDependencies()
override val otherModuleDependencies = object : OtherModuleDependencies {
override val thing = dependencies.thing
}
}
interface Dependencies {
val thing: Thing
}
fun Activity.dependencies(): Dependencies {
return (this.application as MyApplication).dependencies
}
class DefaultDependencies(
override val thing = ThingImplementation()
}
// Other module
interface OtherModuleDependencies {
val thing: Thing
}
interface OtherModuleDependencyProvider {
val otherModuleDependencies: OtherModuleDependencies
}
fun Activity.dependencies(): OtherModuleDependencies {
return (this.application as OtherModuleDependencyProvider).otherModuleDependencies
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment