Say I have scope S1
which has the module with the binding:
bind(Repository.class).to(RepositoryImpl.class).singletonInScope()
Then S2
scope gets opened with S1
as a parent (S1 -> S2
) and S2
defines the same binding (because it's independent and knows nothing of S1
):
bind(Repository.class).to(RepositoryImpl.class).singletonInScope()
By default Toothpick overrides parent scope dependencies, so S2
will have a new RepositoryImpl
created.
Question: Is there a way to reuse the one created in S1
and ignore an S2
binding?
This requirement comes from the fact that sometimes there are independent application components which reside in different scopes and which share that Repository
dependency. They know nothing of each other. These components can also be created in different order, depending on the scenario and use case.
So the only rule which I want to impose is this: some component (it is unknown exactly which one) creates Repository
, all which are created later in current and child scopes - reuse it.