zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
281 stars 34 forks source link

Adding TagReference as argument programmatically #155

Closed cjuega closed 3 years ago

cjuega commented 3 years ago

Hey this library is just awesome!

I'm in a serverless environment so I opted for managing the container programmatically (so webpack prunes any file which isn't really needed by my functions). I'll give yml files a try anyway. But this was the easiest setup.

Anyhow, the thing is, I'm trying to replicate something similar to the following example (from the wiki):

services:
  repository-manager:
    class: ./../RepositoryManager
    arguments: ["!tagged repository"]

  repository-foo:
    class: ./../RepositoryFoo
    tags:
      - { name: repository }

  repository-bar:
    class: ./../RepositoryBar
    tags:
      - { name: repository }

I believe the code must look like:

    let definition = new Definition(RepositoryFoo);
    definition.addTag('repository')
    container.setDefinition('repository-foo', definition);

    definition = new Definition(RepositoryBar);
    definition.addTag('repository')
    container.setDefinition('repository-bar', definition);

    container.register('repository-manager', RepositoryManager).addArgument(new TagReference('repository'));

The problem with the code above is that TagReference class isn't exported, so I haven't access to it. Is there any other way to achieve the same result without exporting TagReference?

zazoomauro commented 3 years ago

@cjuega The fix for your specific problem should be available in the 2.6.9 version. Enjoy!

cjuega commented 3 years ago

I believe the following is missing in lib/types/index.d.ts

export class TagReference {
    constructor (name: string);

    readonly name: string;
}

I'm glad of submitting a PR

cjuega commented 3 years ago

@zazoomauro could you please release a 2.6.10 version with latest typings? thanks!

zazoomauro commented 3 years ago

@cjuega done