Skip to content

Constructor injection with inheritance not working as expected #75

@XaeroDegreaz

Description

@XaeroDegreaz

"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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions