Lilly
02/20/2023, 6:07 PMpublic class that depends on internal implementations for instantiation from the outer world. Example:
_moduleB_:
class MyClass(private val InternalA, private val InternalB)
_moduleA_:
val myClass = MyClass(?, ?) // unknown reference
MyClass needs the dependencies but on the other hand the dependencies should not be known to the outer world. I'm currently aware of 2 options:
1. instead of constructor variables, define them as properties
2. public wrapper/factory class or wrapper/factory function that takes care of the instantiation
What makes me shaky with first option is a statement from stackoverflow:
Constructor injection is used when the class cannot function without the dependent class.
Property injection is used when the class can function without the dependent class.MyClass cannot function w/o the dependencies so option 1 is bad according to the statement What would you advice?
s3rius
02/20/2023, 9:58 PMpublic fun MyClass() = MyClass(<somehow get the dependencies>).
Then moduleA can just call val myclass = MyClass() // this is actually the function, not the classLilly
02/20/2023, 10:24 PM// Implementation of IFormatter
class SpecificDataFormatter(
private val stateMachine: StateMachine, // internal
private val dao: SpecificDataSource, // internal
) : IFormatter { }
Do you see a violation of any principle here?
Since I'm using Koin I never had to think about it.s3rius
02/21/2023, 6:55 PM@Factory you don't need a factory for your formatter either, if you want multiple instances.Lilly
02/21/2023, 11:52 PMSo in that case I'd just provide the date formatter (as SpecificDataFormatter or IFormatter, depending on your needs) as an additional dependency to koinExactly this is the reason why I initially started the discussion. The big disadvantage I see here is that I have to make
StateMachine and DAO public so Koin is able to construct them, when SpecificDataFormatter/IFormatter is constructed. Do you have an advice?s3rius
02/22/2023, 4:58 PMinternal instead of public should be enough.Lilly
02/22/2023, 5:17 PMIf you declare the Koin AppModule in moduleB and include it in your koin application in moduleA, you run into that problem?This works as you described, even with making
StateMachine and Dao internal but tbh I don't know why it works. I already opened a discussion about this here: https://kotlinlang.slack.com/archives/C67HDJZ2N/p1677000079591509?thread_ts=1676844091.055129&cid=C67HDJZ2N
What do I miss?