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

Option to have target instance or (at least) target class name in factory context #72

Open
koldoon opened this issue Aug 18, 2020 · 0 comments

Comments

@koldoon
Copy link

koldoon commented Aug 18, 2020

I've got a proposal to include target instance (which I suppose is not possible since there isn't async resolution logic yet) or target Class (Type) name (I mean, TargetClass.constructor.name or even TargetClass.constructor) in factory binder context.

One of the most popular commonly used dependency is Logger, but the logger often include extra params, like Category or Module Name, that describes the place module logs comes from. Conventionally it looks like this:

export class SomeService {
    private readonly logger: Logger = Log.createLogger('SomeService');
}

So, with IoC I would like to have smth like this:

  • in main file:
Container.bind(Logger).factory(context => {
    // proposal: 'context.target' here is actual constructor (function) of target class
    return Log.createLogger(context.target.name)); 
}
  • and then in service:
export class SomeService {
    constructor(@Inject private logger:Logger) { }
}

But currently there is no way to get any suitable target instance info within factory context.

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

1 participant