Where to declare a generic provider


I’ve made a generic data provider that does a few common things and I want to be able to use it multiple times, like this:-

constructor( contactSrv: DataProvider, supplierSrv : DataProvider, … ){
contactSrv.init( “contact/url”…);
supplierSrv.init( “supplier/url” …);

contactData = contactSrv.getData()…
suppliertData = supplierSrv.getData()…

So far I’ve had to declare it in app.module.ts but that creates me a singleton which obviously means I get only one service.
Where should I declare this so I can use it as many times as I need?




I think this is a good explanation.

Best regards, anna


I think what you are trying to do is sort of fighting against the framework. A major point of DI is to get rid of those init() methods. You could probably hack around this with InjectionToken, but I would just make ContactProvider and SupplierProvider separate classes.


I’ve fixed it another way, by making the provider take in everything it need for each call.
The point is that the class does lots of things, replicating it for each service (of which there are many, not just two) would be a massive overhead for maintenance. I was hoping that I could inject the same class multiple time called different things and set each up differently.
Thanks for you help anyway,


I’m not convinced of that. Each service could inject the “generic” service in turn.