For me it feels like the behaviour of JS/Native is...
# kodein
y
For me it feels like the behaviour of JS/Native is natural, as you declared
logger
as CommonLogger. And in general depending on concrete class is anti-pattern, so if I were you I would bind to
CommonLogger
in JVM.
u
What would be the pattern if I need the same instance for its interface methods and for it's actual implementation? In my UseCase, the Interface provides some logging methods which are used by common code. The ConsoleLogger just does some println to implement those. The AndroidLogger additionally provides methods to add listeners where the current activity can register and do the actual logging to a TextView, whenever its registered listener is called. To implement this, I bind an AndroidLogger in Application's onCreate and inject the logger by Interface to common code and by it's real type into the activity so that add-/remove listener methods are visible to the activity.
y
I would just create another binding(not tested)
Copy code
bind<AndroidLogger>() with singleton { AndroidLogger() }
bind<CommonLogger>() with provider { instance<AndroidLogger>() }
u
Sounds cool. I'll try
Thx
As kodein/JVM resolves AndroidLogger also to CommonLogger this does not work:
Copy code
org.kodein.di.Kodein$NotFoundException: 2 bindings found that match bind<CommonLogger>() with ?<InjectedLogger>().? { ? }:
            bind<CommonLogger>() with provider { AndroidLogger }
            bind<AndroidLogger>() with singleton { AndroidLogger }
y
Yeah, you’re right. It looks odd but if these are considered as bindings for same type, maybe you can use tag to distinguish binding
Copy code
bind<AndroidLogger>(tag="android") with singleton { AndroidLogger() }
bind<CommonLogger>(tag="common") with provider { instance<AndroidLogger>(tag="android") }
and then
Copy code
object InjectedLogger : KodeinGlobalAware {
    val logger: CommonLogger by instance(tag="common")
}
you also need to specify “common” tag for CommonLogger’s web/native configuration.
u
Thanks @yshrsmz, i kind of expected tags to work around the issue. My point is, that this can be solved with the type system alone. Only that neither my solution, nor your suggestion (without tags) work on all platforms. I consider this an issue that should probably be fixed by making the behavior the same on all platforms. Thanks again for your support!
y
yes, my understanding is the same as yours. this behaviour should be fixed, or at least documented specifically.