Created
December 11, 2024 10:05
-
-
Save seadowg/a61930ff691ad75079019c38dd93e7c1 to your computer and use it in GitHub Desktop.
Very simple DI for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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