Skip to content

Instantly share code, notes, and snippets.

@xanderblinov
Created February 5, 2017 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xanderblinov/0bb39fcba3b236e023f449062a8709f9 to your computer and use it in GitHub Desktop.
Save xanderblinov/0bb39fcba3b236e023f449062a8709f9 to your computer and use it in GitHub Desktop.
Component Dependency Injection
class App : Application() {
override fun onCreate() {
super.onCreate()
MvpFacade.init()
DI.init(this)
}
}
@InjectViewState
class AuthPresenter : BasePresenter<AuthView>() {
@Inject
lateinit var authRepository: AuthRepository
@Inject
lateinit var infoRepository: InfoRepository
init {
DI.componentManager().businessLogicComponent().inject(this)
}
}
class ComponentManager(val context: Context) {
val internalAppComponent: AppComponent by lazy {
DaggerAppComponent.builder()
.modelModule(ModelModule(context))
.busModule(BusModule())
.build()
}
val internalBusinessLogicComponent by lazy {
internalAppComponent.plus(BusinessLogicModule())
}
val networkSubComponentHolder = ComponentHolder<NetworkSubComponent, Guid> {
internalAppComponent.plus(NetworkModule(it))
}
fun networkComponent(guid: Guid): NetworkSubComponent {
return networkSubComponentHolder.provideComponent(guid)
}
fun onNetworkComponentReleased(guid: Guid) {
networkSubComponentHolder.onDependencyReleased(guid)
}
fun businessLogicComponent() = internalBusinessLogicComponent
fun appComponent() = internalAppComponent
}
object DI {
private lateinit var componentManager: ComponentManager
fun init(context: Context) {
componentManager = ComponentManager(context)
}
fun componentManager(): ComponentManager = componentManager
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment