Inject container in dependency
Expected Behavior
Generally IoC container allow self injection. I will demonstrate in C#. This is to demonstrate that this approach is common and supported by other frameworks (example below demonstrate .NET Core's IoC container).
class Foo {
public IServiceProvider Provider { get; }
public Foo(IServiceProvider provider) // this is DI container
{
Provider = provider;
}
}
IServiceCollection services = new ServiceCollection();
services.AddTransient<Foo>(); // register to DI
IServiceProvider provider = services.BuildServiceProvider(); // build container;
var foo = provider.GetService<Foo>();
foo.Provider == provider ; // true
In this case, constructor recieve instance that is constructing given type. This means that child containers propagte their own instance.
var foo1 = provider.GetService<Foo>();
var childProvider = provider.CreateScope().ServiceProvider; // Create child scope/container
var foo2 = provider.childProvdier <Foo>();
foo1.Provider == foo2.Provider; //false
foo2.Provider = childProvider; // true
Usecase in TypeScript:
cosnt container = new Container();
@injectable()
class Foo {
constructor(container: Container) {
this.container= container;
}
}
container.bind(Foo).toSelf();
const foo1 = container.get(Foo);
foo1.container === container; // true
const childContainer = container.createChild();
cosnt foo2 = childContainer.get(Foo);
foo1.container === foo2.container; // false
foo2.container === childContainer; // true
Current Behavior
Uncaught Error: No matching bindings found for serviceIdentifier: Container
at _validateActiveBindingCount (planner.js:59:1)
at _getActiveBindings (planner.js:45:1)
at _createSubRequests (planner.js:86:1)
at planner.js:110:1
at Array.forEach (
Your Environment
- Version used: 6.0.1
👍 We are encountering the same issue while migrating from typed-inject which provides this feature. Did you find a workaround @Deathrage?
@vhiairrassary yes
container.bind(Container).toDynamicValue(ctx => ctx.container as Container);
But this is somewhat a hack as ctx.container is not class Container but interface Container and I cast it to the class (watchout, you cannot be sure that ctx.container is an instance of that class as it might be a different subtype; However, as of now it is instnace of that class).
Thank you so much 🤩 Hopefully the feature will be implemented at some point in the project 🤞