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

Ability to inject dependencies for dynamicValue from same scope #1143

Open
thynson opened this issue Sep 2, 2019 · 2 comments
Open

Ability to inject dependencies for dynamicValue from same scope #1143

thynson opened this issue Sep 2, 2019 · 2 comments

Comments

@thynson
Copy link

thynson commented Sep 2, 2019

Make toDynamicValue binding able to injects its dependencies, to ensure its dependencies are
resolved in same scope.

Motivation

I'm trying to implements a context logger.

container.bind('ContextID')
  .toDynamicValue(()=> uuid())
  .inRequestScope();
container.bind('Logger')
  .toDynamicValue((context)=> {
    const targetName = (context.currentRequest.parentRequest.serviceIdentifier as Function).name;
    return new Logger(
      context.container.get('ContextID'),
      targetName
    )
  })
  .inTransientScope();

class FooComponent {

  @inject('Logger')
  loggerA: Logger;

}
class BarComponent {
  @inject('Logger')
  loggerB: Logger;
}

loggerA and loggerB cannot have same context id, even if they are resolved in same request, because context.container.get('ContextID') create a new request scope.

Possible Solution

  1. Allow inject depedencies into factory function, like nestjs does.
container.bind('Logger').toDynamicValue({ inject: ['ContextID'], factory: (ctx)=> newLogger(ctx, ...)});
  1. Make context itself be able resolve dependencies, in its own scope.
container.bind('Logger')
  .toDynamicValue((context)=> return new Logger(context.resolve('ContextID'), ...))
  .inRequestScope();
@tonyhallett tonyhallett mentioned this issue Sep 4, 2019
11 tasks
@talha5389
Copy link

I also need this functionality. Right now, there does not seem to be a way to get a dependency already resolved in request scope in toFactory, toDynamicValue etc without resetting/recreating resolved object in request scope

@dustinlacewell
Copy link

Oh god this hurts so bad. :(

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

No branches or pull requests

3 participants