I am working with an injectable service that utilizes the useFactory
attribute to determine whether it should be injected or if an implemented type should be used instead.
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { signatureConfigurationServiceFactory } from '../../environments/services.factories';
import { RestService } from '.';
@Injectable({
providedIn: 'root',
useFactory: signatureConfigurationServiceFactory,
deps: [Router, RestService]
})
export class SignatureConfigurationService {
constructor(public router: Router, public restService: RestService) {
}
// ...
}
The factory function is located in a separate file named services.factories.ts. I have set it up this way so that I can easily swap it out using fileReplacements
during the ng build
process with another similar file containing a different factory function.
import { Router } from '@angular/router';
import { RestService } from '../app/services';
import { SignatureConfigurationService } from '../app/services/signature-configuration.service';
export let signatureConfigurationServiceFactory = (router: Router, restService: RestService) => {
return new SignatureConfigurationService(router, restService);
};
However, I am encountering a circular reference issue since my service references the factory and vice versa.
While the Angular documentation suggests using forwardRef to resolve such issues, the examples provided did not seem applicable to my scenario.
What is the best approach to break out of this circular dependency while still keeping the factory method in a separate file?