Should a DI framework be handling which implementa...
# android-architecture
d
Should a DI framework be handling which implementation to give for each platform, or should that always be delegated to a factory method?
c
platform as in... api level? or platform as in kotlin multiplatform, platform?
d
I have a bunch of criteria, one is api level, versions of other related apps... Webview version... On one hand the DI IS a type of factory, on the other it doesn't seem too SRP to let it decode these things. Where should the line be drawn?
k
Can't DI function returning an object be a factory method? It's not mutually exclusive.
d
Yeah, the thing is that DI frameworks aren't so testable, so when there's a bit more logic... but then what's the point of DIs?
g
I believe all this logic should be encapsulated to a factory and DI could just call this factory to create an instance, so you will be able to test this logic
So it’s like Merle suggested, but instead of injecting factory, you inject actual instance
d
Oooh, I missed that, using the DI to pass the FACTORY METHOD... I thought he meant the DI's method IS the factory method... and your point @gildor, is a good one, you win the best of both worlds... I once thought it was taboo to actually run something in DI, that it's only to passively create valid objects, and running a factory is also running something, but I guess you're right they probably complete each other in this case. Thanks!
g
Using a factory from DI looks exactly the same for me as using constructor from DI
d
So you really only use DI to rig things together but NOT to RESOLVE which implementations to give?
👌 1
Or are there exceptions?
g
I personally do not have such cases when I need to provide different implementation on runtime, usually it's solved on compile time for me (different apps/components require different implementation and it encoded on level of DI, so resolved on compile time (with dagger)), but if I would have such case I would extract it to some factory as suggested above
running a factory is also running something
It's also true for constructor, you run init of this method, factory is like a "constructor" for an interface Also you can use another class as a facade for other implementation and just delegate to those platform specific implementation, if you don't want to have explicit factory
d
Interesting, thanks!