You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to override an interface binding bind to a @Inject dependency?
Scenario
A library that exposes a @Component and all its dependencies are declared only with @inject and bindextension for interfaces.
An app that consumes that library and wants to override just one interface so when any @Inject class requiring that interface gets the app impl instead of the one provided by the library.
@Inject
@AppScope
classAppImpl : SomeInterface {
overridefunsomeFunction() {
println("app impl")
}
}
@Scope
annotationclassAppScope
@Component
@AppScope
abstractclassAppComponent : LibComponent {
// Throws error -> Cannot provide: SomeInterface as it is already provided// val AppImpl.bind: SomeInterface// @Provides// get() = AppImpl()// Throws error -> Cannot provide: SomeInterface as it is already provided// @Provides// fun provideAppImpl(): SomeInterface = AppImpl()
}
@KmpComponentCreate
expectfun AppComponent.Companion.createKmp(): AppComponent
Issues
Didn't find a way to replace SomeInterface implementation with the AppImpl since the signature for binds is using an extension and you can't override extensions.
This doesn't compile because I'm using both AppScope and LibScope but I want to be able to inject classes in my library and made them "singletons" (' LibScope' ) and do the same in the app, I can't just use the library scope, it would be wrong and not possible if I had two libs exposing their components.
I've tried composition instead of inheritance but still didn't find a way of overriding SomeInterface, I would be creating new instances instead of overriding it.
Solutions
Create @Bind annotation so it could be a normal function and could be overrided on children, also it would be nice to add an optional parameter to the inject annotation @Inject(SomeInterface::class) class SomeImpl for bindings.
Currently, unless I missed something, the only thing that I can think of is not using @inject and bind on the library and provide manually each dependency, and then override what I want in the app, but is really missing the @Injectfeature which helps a lot reducing the boilerplate and maintenance time.
The text was updated successfully, but these errors were encountered:
Is it possible to override an interface binding bind to a
@Inject
dependency?Scenario
A library that exposes a
@Component
and all its dependencies are declared only with@inject
andbind
extension for interfaces.An app that consumes that library and wants to override just one interface so when any
@Inject
class requiring that interface gets the app impl instead of the one provided by the library.Lib code
App code
Issues
SomeInterface
implementation with theAppImpl
since the signature for binds is using an extension and you can't override extensions.AppScope
andLibScope
but I want to be able to inject classes in my library and made them "singletons" (' LibScope' ) and do the same in the app, I can't just use the library scope, it would be wrong and not possible if I had two libs exposing their components.SomeInterface
, I would be creating new instances instead of overriding it.Solutions
@Bind
annotation so it could be a normal function and could be overrided on children, also it would be nice to add an optional parameter to the inject annotation@Inject(SomeInterface::class) class SomeImpl
for bindings.@inject
andbind
on the library and provide manually each dependency, and then override what I want in the app, but is really missing the@Inject
feature which helps a lot reducing the boilerplate and maintenance time.The text was updated successfully, but these errors were encountered: