Bundle together Services in Angular

142 Views Asked by At

I'm trying to find a nice way bundle together several services.

I have several services that communicate with my backend (which are provided by my backend team) and I want them to be accessible through a single service, something like: BackendService which would be a container for, say: FooService, BarService and DogService.

Ideally I'd be able to call the functions found within these services directly, something like: backendService.dogBark() <- this function is found in dogService.

I've tried going the Object.assign route, and I've looked into Typescript mixins but these options don't work / aren't quite what I'm after.

Implementing the functions in the BackendService isn't an option either, as our backend is growing.

The "best" way I've found so far is to simply inject the Services I want to bundle together into my BackendService and then inject BackendService wherever needed, this gives me the latter option where I can make calls like: backendService.dogService.dogBark()

As I said, a liveable solution, but I'm hoping someone might be able to point me towards something more elegant.

Any guidance / tips would be greatly appreciated.

1

There are 1 best solutions below

0
Apoorva Chikara On

Services are put to setup communication b/w components, directives or modules. You can follow any approach that suites the best for you. I would recommend you to go with strategy design pattern which selects the dynamic services based on the selection from the user.

Definitely, you need to write a logic to make a selection of services for every input. You should check the link for how to implement it.