Skip to content

BindingScope.SINGLETON scope not working as expected? #6728

@jagzmz

Description

@jagzmz

Hi Team,

I am developing a component which takes binding keys for repositories from the configuration, and I am fetching those repositories by code like below,

const repository = await this.application.get<TRepo>(
        `repositories.${config.modelRepository}`
);

this instantiates the repository and gets back the expected result. After this, I am manually setting some properties on the repository class so that it could be used ahead in the future.

But when I hit an controller endpoint for the first time which injects this repository ,

export class UserController extends AuthenticationControllerMixin<
  User,
  Constructor<Object>
>(Object, options) {
  constructor(
    @inject('repositories.UserRepository')
    public userRepository: UserRepository,
    @service(ImagekitProvider) public imagekit: Imagekit
  ) {
    super();
}

it creates a new instance of the Repository again, but further API hits don't.

What would be the reason the new Repository instance is getting generated 2 times

  1. with the application.get.
  2. when hitting a Controller endpoint for the first time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    IoC/Context@loopback/context: Dependency Injection, Inversion of Control

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions