0

I have a generic data service which has its configuration injected as per below:

export default class GenericDataService<T> implements IGenericDataService<T> {
  constructor(private readonly httpClient: HttpClient,
  private readonly config: GenericDataServiceConfig
) { }

GenericDataServiceConfig has things like baseUrl, endpoint etc.

This data service is injected into a shared lookup component like so:

export class GenericLookUpComponent<T> implements OnInit {
  constructor(private readonly dataService: GenericDataService<T>) {}
...
}

The service is also injected into a component onto which the GenericLookupComponent is placed:

export class LookupDemoComponent implements OnInit {
  constructor(private readonly nominalService: GenericDataService<Nominal>) { }
...
}

So I have two instances of the GenericDataService but both need different configurations, one on the shared lookup component and one on the demo component

I know I can inject the configuration in the component providers like so:

@Component({
  selector: 'app-lookup-demo',
  templateUrl: './lookup-demo.component.html',
  providers: [
    GenericDataService,
    { provide: GenericDataServiceConfig, useValue: nominalConfig }
  ]
})

...but both instances of the service are picking up the same configuration.

How can I specify different configuration for the different instances of the service?

atamata
  • 997
  • 3
  • 14
  • 32
  • is that true that basically you need one config for one "entyty", where I mean basically that "endpoint" shoud decide the type T in `GenericDataService`? – Andrei Mar 21 '23 at 18:32
  • from the question I would say my solution from [this](https://stackoverflow.com/questions/75632359/angular-dependency-injection-inheritance-and-services) ticket would work for you – Andrei Mar 21 '23 at 18:44
  • Why do you say, you have two instances? I think, you have only one instance provided in the `LookupDemoComponent`. You basically say, that `GenericLookUpComponent` is a child of `LookupDemoComponent`. Well, what happens, if you provide the dependency for `GenericDataService` only in the `LookupDemoComponent`? You can guess. So, if you want a different config provided in the `GenericLookUpComponent`, then provide the dependency there. Because Angular looks the component tree, finds the dependency in the `GenericLookUpComponent`, but no provider, then looks further up the tree. – derstauner Mar 21 '23 at 19:01
  • @derstauner thanks for your reply. I don't want to provide the config in `GenericLookUpComponent` as that would mean it was no longer generic. I want to provide the config where the `GenericLookUpComponent` is being used, ie `LookupDemoComponent`. So basically I need to have two sets of configuration, one used by `GenericDataService` in `LookupDemoComponent` and the second used by `GenericDataService` in `GenericLookupService` – atamata Mar 22 '23 at 07:12
  • @Andrei thanks for your reply. Basically I need to have two sets of configuration, one used by `GenericDataService` in `LookupDemoComponent` and the second set of configuration used by `GenericDataService` in `GenericLookupService` – atamata Mar 22 '23 at 09:26
  • but how should `GenericLookupService` know which config to provide if it is Generic as well? – Andrei Mar 22 '23 at 09:37
  • @Andrei thanks again for your reply. Apologies I should have said `GenericLookupComponent` not `GenericLookupService`. I had thought that I could use the provides in `LookupDemoComponent` to inject different configurations. One configuration for the data service in `GenericLookupComponent` and 2nd configuration for the data service in `LookupDemoComponent` (since it's a child component). I have been looking into using `InjectionToken` to achieve this but haven't worked it out yet. – atamata Mar 22 '23 at 10:48
  • I am not qute sure I 100% understand what you are trying to achieve, but I believe understanding these things would help you: 1st - "In one injector node each token (class reference in your case) would always be resolved to the same item (or array of items in case multi: true is defined in the provider config).". 2 - "Service and Service is basically one reference, because they are not different when compiled to js". 3 - "each component creates an injector node, which has reference to the parent's components injector" – Andrei Mar 22 '23 at 15:03

0 Answers0