How partial properties injection can be done with Inversify?
Let's say we have class
class MyClass {
constructor(
@inject(EXTERNAL_A) private externalA: ExternalClassA,
private b: string
) {}
}
How can I use this MyClass with inversify in other classes in case if all possible values of b are known on compile time. So, I need just to instances of MyClass one with b = "a"
and another one with b = "b"
.
The only solution that I found at the moment is two define two different bindings for that or using factory to directly call new MyClass
.
In the first case I need to write something like that only
container.bind<MyClass>(CLASS_A).toConstantValue(
new MyClass(container.get<ExternalClassA>(EXTERNAL_A), "a")
);
container.bind<MyClass>(CLASS_B).toConstantValue(
new MyClass(container.get<ExternalClassA>(EXTERNAL_A), "b")
);
Looks pretty messy and not solving next issue, as well as factories not solving it. If I have deep objects hierarchy in this case, I need to construct them all through this chain of manual objects construction.
What is the best way here?
Task with a star, if it possible to resolve some dependencies tree with replacement of the same single dependency with provided one? Like, we can have something like
|-----B-----e
A|-----C-----e
|-----D
|-----e
So I wan't to replace e
dependency with my constructed one. How can I achieve this?