GuidesHooks

Dependency Hooks

If you want to insert some behavior at certain times (e.g., immediately configuring it after a dependency is instantiated), you can use the following dependency hooks.

onInstantiation

For ClassDependencyItem and FactoryDependencyItem, if you want to do something immediately after the dependency is instantiated, you can pass an onInstantiation hook.

const configService = [
  IConfigService,
  {
    useClass: ConfigService,
    lazy: true,
    onInstantiation: (s: IConfigService) => {
      s.setConfig();
    },
  },
];

This function will be called immediately after the constructor or factory is executed. If a ClassDependencyItem is lazily instantiated, it will be called immediately after lazy instantiation.