Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject container in dependency #1486

Open
Deathrage opened this issue Nov 15, 2022 · 3 comments
Open

Inject container in dependency #1486

Deathrage opened this issue Nov 15, 2022 · 3 comments

Comments

@Deathrage
Copy link

Deathrage commented Nov 15, 2022

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 ()
at planner.js:109:1
at Array.forEach ()
at _createSubRequests (planner.js:89:1)
at plan (planner.js:131:1)
at container.js:598:31

Your Environment

  • Version used: 6.0.1
@vhiairrassary
Copy link

vhiairrassary commented Feb 2, 2023

👍 We are encountering the same issue while migrating from typed-inject which provides this feature. Did you find a workaround @Deathrage?

@Deathrage
Copy link
Author

Deathrage commented Feb 2, 2023

@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).

@vhiairrassary
Copy link

Thank you so much 🤩 Hopefully the feature will be implemented at some point in the project 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants