leosan
03/13/2018, 4:10 PMbind<Job>(MyJob.TAG) with singleton { MyJob(instance()) }
bind<Job>(MyJob2.TAG) with singleton { MyJob2() }
class JobCreator(private val kodein: Kodein) : JobCreator {
override fun create(tag: String): Job? {
return kodein.instance(tag)
}
}
But I wonder if there’s a way to create this dependency in a transitive way without passing the kodein instance to the job creator…Andreas Sinz
03/13/2018, 4:42 PMJobCreator
based on a TAG
?leosan
03/13/2018, 4:51 PMAndreas Sinz
03/13/2018, 4:59 PMbind<Job> with factory { tag: String -> kodein.instance<JobCreator>().create(tag) }
with kodein.instance<JobCreator>(MyJob.TAG)
?leosan
03/13/2018, 5:25 PMonCreate(tag)
it’s handled by the libraryleosan
03/13/2018, 5:27 PMJob
a tag but the create
method is called internallyAndreas Sinz
03/13/2018, 5:45 PMleosan
03/13/2018, 5:46 PMevernote-job
libraryleosan
03/13/2018, 5:50 PMJobManager.create(this).addJobCreator(MyJobCreator())
class MyJobCreator : JobCreator
class MyJob : Job
So whenever a create a Job the JobManager will call the create
as I passed the MyJobCreator
to it.Andreas Sinz
03/13/2018, 5:51 PMleosan
03/13/2018, 5:52 PMbind<JobCreator>() with singleton { MyJobCreator(this) }
and singletons bindings for different jobsleosan
03/13/2018, 5:53 PMJobManager.create(this).addJobCreator(instance())
JobManager.instance()
Andreas Sinz
03/13/2018, 5:53 PMevernote-job
gets your jobs directly from your MyJobCreator
leosan
03/13/2018, 5:54 PMleosan
03/13/2018, 5:55 PMevernote-job
will cal the on create but I have to create the instance by myselfleosan
03/13/2018, 5:55 PMleosan
03/13/2018, 5:56 PMclass MyJobCreator : JobCreator {
override fun create(tag: String): Job? {
return when (tag) {
MyJob1.TAG -> MyJob()
else -> null
}
}
}
leosan
03/13/2018, 5:58 PMJobCreator
Andreas Sinz
03/13/2018, 5:59 PMJobCreator
must provide the Job
and therefore you can either pass in all possible jobs as constructor parameters class MyJobConstructor(val job1: MyJob, val job2: MyJob2)
or you give JobCreator
an instance of Kodein
and it retrieves the instance itselfleosan
03/13/2018, 6:01 PMleosan
03/13/2018, 6:02 PMAndreas Sinz
03/13/2018, 6:03 PMevernote-job
is designed, there is no other way to do it. If you'd use constructor parameters and you have like 20 different jobs it gets messy pretty fastAndreas Sinz
03/13/2018, 6:07 PMJobCreator
per Job