I've parent abstract service

    @Injectable()
    export abstract class parent {}

and two child services

    @Injectable()
    export class dataService extends parent {}

    @Injectable()
    export class mockService extends parent {}

now I want to use useFactory in app.module based on some constant variable (i.e productionStatus) which means if status is connected then use 'dataservice' else use 'mockservice'

this is how I'm currently doing

core.module:

@NgModule({
  providers: [
    {
      provide: PayoutService,
      useFactory:()=> new FactoryConfigService().getPayoutServiceInstance()
    },
    {
      provide: OrderService,
      useFactory:()=> new FactoryConfigService().getOrderServiceInstance()
    }
  ]
})

FactoryConfigService:

export class FactoryConfigService {
  public productionStatus: Connection.Status = Connection.Status.disconnected;

  constructor() { }

  getOrderServiceInstance() {
    if (this.productionStatus == Connection.Status.connected) {
      return new OrderDataService();
    } else {
      return new OrderMockService();
    }
  }
  getPayoutServiceInstance() {
    if (this.productionStatus == Connection.Status.connected) {
      return new PayoutDataService();
    } else {
      return new PayoutMockService();
    }
  }

}

now my Q is how to use FactoryConfigService in providers[] without 'new' and as singleton/injectable

0 Answers