I partially solved my problem doing this ``` bin...
# kodein
l
I partially solved my problem doing this
Copy code
bind<Job>(MyJob.TAG) with singleton { MyJob(instance()) }
bind<Job>(MyJob2.TAG) with singleton { MyJob2() }
Copy code
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…
a
@leosan Are you trying to retrieve the right job from your
JobCreator
based on a
TAG
?
l
yes
a
you can try
bind<Job> with factory { tag: String -> kodein.instance<JobCreator>().create(tag) }
with
kodein.instance<JobCreator>(MyJob.TAG)
?
l
hmm I don’t control the tag passed to the
onCreate(tag)
it’s handled by the library
I mean I give the
Job
a tag but the
create
method is called internally
a
@leosan internally from where?
l
evernote-job
library
basically I have
Copy code
JobManager.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.
a
and why do you want to bind the jobs then?
l
yes, at the moment I have
bind<JobCreator>() with singleton { MyJobCreator(this) }
and singletons bindings for different jobs
and my Application extends KodeinAware so initialize the `JobManager `like this
Copy code
JobManager.create(this).addJobCreator(instance())
JobManager.instance()
a
yes, but why do you need to bind the different jobs?
evernote-job
gets your jobs directly from your
MyJobCreator
l
because I’m also passing dependencies to these jobs
the
evernote-job
will cal the on create but I have to create the instance by myself
without DI is something like this
Copy code
class MyJobCreator : JobCreator {
    override fun create(tag: String): Job? {
        return when (tag) {
            MyJob1.TAG -> MyJob()
            else -> null
        }
    }
}
I don’t want to instantiate these dependencies on my
JobCreator
a
well, your
JobCreator
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 itself
l
hmm so what I did passing the kodein instance is right then… good to know 😮
I thought it would be wrong give the Kodein instance to it, but now you saying makes sense
a
the way
evernote-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 fast
well, you could also create a
JobCreator
per
Job