I'm looking at the implementation of in-memory-web-api and there is the following code:
@Injectable()
export class InMemoryBackendService {
protected config: InMemoryBackendConfigArgs = new InMemoryBackendConfig();
^^^^^^
...
constructor(
@Inject(InMemoryBackendConfig) @Optional() config: InMemoryBackendConfigArgs
^^^^^^
) {
...
As I understand the pattern is the following:
- Defined class property and instantiate a dependency without using DI
- Optionally inject dependency
If a user provides modified dependency through DI, it will be injected and the default one instantiated without DI will be overridden. I suspect something similar maybe with RequestOptions
in HTTP
module.
Is this a common pattern?
EDIT:
It turns out that in-memory-web-api
is not exactly the pattern I'm asking about. Suppose, I have a class A
that uses instance of class B
injectable with the token B
. So they are both registered with the root injector:
providers: [A, B]
Now, if a user wants to customize B
, he can register the customized version under the same token, thus effectively overrriding the original B
:
providers: [{provide:B, useClass: extendedB}]`
This is how RequestOptions
can be extended in http
module.