Lazy injection
Lazy injection allows you to defer the creation of services, by returning a function instead. When invoked, this function will create the service on demand. Lazy injection could aso be a solution to enable circular dependencies.
Usage
In order to use lazy injection, just pass { lazy: true }
:
ts
import { inject } from "@needle-di/core";
import { FooService } from "./foo.service";
class MyService {
constructor(
private fooService = inject(FooService, { lazy: true }),
) {}
public doSomething() {
// invoking the function will trigger the creation of `FooService`
this.fooService().someMethod();
}
}
Rules and behaviour
- Lazy injection can be combined with optional injection. In that case, it would the function above would be
() => FooService | undefined
; - Lazy injection can also be combined with async injection. In that case, it would the function above would be
() => Promise<FooService>
;