Ive a question about extension functions that use dependencies,
and is it even appropiate to try to use them in this manner.
I'm using manual DI with a container class thats a property of the Application class.
Say I have a util class like JobUtils I inject into a few different classes.
Instead of injecting JobUtils and doing jobUtils.start(job) I really want to
just do job.start(). But when I implement the extension functions it feels wrong,
I'm creating hidden dependencies and they're really awkward to test.
Am I barking up the wrong tree using extension functions for this type of situation
or is there a nice way to do this?
class JobUtils(
private val jobWorkManager: JobWorkManager, // myApp.container.jobWorkManager,
private val jobRunner: JobRunner, // JobRunner(x,y,z)
private val jobRepository: JobRepository // myApp.container.jobRepository,
) {
suspend fun start(job: Job) {
jobWorkManager.startBackgroundJob(job.id)
job.status = jobStatus.Running
jobRepository.update(job)
}
suspend fun stop(job: Job) {
jobRunner.stop()
job.status = JobStatus.Stopped
JobRepository.update(job)
}
}
.
.
.
// this feels great :)
job.start()
.
.
.
// This feels wrong :(
suspend fun Job.start() {
// get context
.
.
.
val container = (context.applicationContext as myApp).container
val util = JobUtils(container.jobWorkManager,
JobRunner(container.x,container.y,container.z),
container.jobRepository)
util.start(job)
}
suspend fun Job.stop() {
// get context
val container = (context.applicationContext as myApp).container
val util = JobUtils(container.jobWorkManager,
JobRunner(container.x,container.y,container.z),
container.jobRepository)
util.stop(job)
}