Inversifyjs: Only one named binding - no error if `getNamed` is called with invalid name.

Created on 9 Dec 2016  路  6Comments  路  Source: inversify/InversifyJS

If i have only one named binding - no error if getNamed is called with invalid name.

Expected Behavior

Error No matching bindings found for serviceIdentifier: Symbol(IControlFlowReplacer) - named: XXX

Current Behavior

In 3.0.0-rc1 returns BinaryExpressionControlFlowReplacer with named binding YYY

Code:

export const nodeControlFlowTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
    bind<IControlFlowReplacer>(ServiceIdentifiers.IControlFlowReplacer)
        .to(BinaryExpressionControlFlowReplacer)
        .whenTargetNamed('YYY');

    bind<IControlFlowReplacer>(ServiceIdentifiers['Factory<IControlFlowReplacer>'])
        .toFactory<IControlFlowReplacer>((context: interfaces.Context) => {
            return () => {
               return context.container.getNamed<IControlFlowReplacer>(
                    ServiceIdentifiers.IControlFlowReplacer,
                    'XXX'
                ); // returns BinaryExpressionControlFlowReplacer
        });
});

Your Environment

  • Version used: 3.0.0-rc1
  • Environment: Node 7.0.0
  • Operating System and version (desktop or mobile): Xubunti 14.04
bug

Most helpful comment

Fixed by https://github.com/inversify/InversifyJS/pull/443 and released as 3.0.0-rc.2 :tada:

All 6 comments

Hi @sanex3339 I don't think that change is is going to be possible. If no bindings match the name no bindings will be returned.

You can try using container.isBoundNamed:

let id = "XXXXX";
let name ="YYYYY";

let val =  container.isBoundNamed(id, name) ? container.getNamed(id, name) : container.get(id);

If no bindings match the name no bindings will be returned.

Looks like you didn't understand me, because my bad english.

The problem is when i have only one named binding 'YYY' with service idendtifier 'A', when i will try to get from container dependency with service identifier 'A' but with name 'XXX' (which is not exist in the container), i will get (only one available in the container) named dependency with name 'YYY', like it is default binding. For my side this is strange, an error should trow.

When i have two or more binding with names 'XXX' and 'YYY' with service identifier 'A', when i will try to get from container dependency with service identifier 'A' but with name 'ZZZ' (which is not exist in the container), i will get an error, as expected.

Sorry for that. I think I understand the problem now. I will test it and fix it during the weekend :+1:

Thank you!

Fixed by https://github.com/inversify/InversifyJS/pull/443 and released as 3.0.0-rc.2 :tada:

Thank you

Was this page helpful?
0 / 5 - 0 ratings