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

Constructor injection with inheritance not working as expected #75

Open
XaeroDegreaz opened this issue Sep 14, 2020 · 1 comment
Open

Comments

@XaeroDegreaz
Copy link

"typescript-ioc": "^3.2.2"

export abstract class BaseRepository<T extends BaseModel> {
  protected readonly collection: Collection<T>

  constructor( @Inject private readonly connectionManager: ConnectionManager )
  {
    //# Weird -- Why can't I inject this in this constructor, instead?
    //const connectionManager = Container.get( ConnectionManager );

    //TypeError: Cannot read property 'db' of undefined
    this.collection = this.connectionManager.db.collection( this.getCollectionName() )
  }

  abstract getCollectionName(): string;
}

@Singleton
export class TileRepository extends BaseRepository<Tile> {
  getCollectionName()
  {
    return 'tiles';
  }

  test()
  {

    console.log( this.collection.collectionName );
  }
}

I've tried several variations of the constructor including a simple @Inject connectionManager: ConnectionManager to no avail. I've also tried field injection, and that didn't work, but I suspect that information wouldn't be available in the constructor anyhow.

If I add a constructor in TileRepository and @Inject the ConnectionManager, then pass it to super() it will work, but I don't want to lead that implementation detail.

@jlurbe
Copy link

jlurbe commented Jan 15, 2021

Almost the same here. Same version of typescript-ioc.

import { Get, Route } from "tsoa";
import { Inject, Singleton } from "typescript-ioc";

@Singleton
class Katana {
    public hit() {
        return "cut!";
    }
}

@Singleton
class Shuriken {
    public throw() {
        return "hit!";
    }
}

@Singleton
@Route('/ninja')
export class NinjaController {

    private _katana: Katana;
    private _shuriken: Shuriken;

    constructor(@Inject katana: Katana, @Inject shuriken: Shuriken) {
        this._katana = katana;
        this._shuriken = shuriken;
    }


    @Get('/fight')
    public fight() { return this._katana.hit(); };
    @Get('/sneak')
    public sneak() { return this._shuriken.throw(); };

}

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

2 participants