To define a singleton, should I use Kotlin object declaration or to make an ordinary Kotlin class and inject it using dagger? In my opinion the first option is definitely easier but there may be a reason to use dagger in this situation that I'm not aware of.
Option 1 (notice object
keyword):
object SomeUtil {
// object state (properties)
fun someFunction(number: Long) {
// ...
}
}
Option 2 (notice class
keyword):
class SomeUtil {
// object state (properties)
fun someFunction(number: Long) {
// ...
}
}
@Module
class AppModule {
@Provides
@Singleton
internal fun provideTheUtil() = SomeUtil()
}
class MainActivity : BaseActivity() {
@Inject internal lateinit var util: SomeUtil
}
UPDATE 2019-07-03
@Blackbelt said in comments that we should prefer option 2 for testability. But libraries like MockK can mock object
s too. So do you still think option 2 is the preferred one?
this situation that I'm not aware of
, testability ? – Blackbelt