K
K
Konstantin2021-01-20 11:02:06
Angular
Konstantin, 2021-01-20 11:02:06

Should I use dependency inversion?

Angular is known for the DI principle. For example, injecting a specific service that makes requests to an API.

Class UsersService {}

Should I use the dependency inversion principle for all services? And do not substitute a specific implementation. and abstraction interface.

Constructor (private user: UserServiceAbstract) {}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2021-01-20
@Junart1

DI provides the ability to shove anything :)
In principle, if you really need it, you can use an abstract class as a token, pass specific implementations on it and use it as consumers.
In Angular itself, this is all the time, for example, ChangeDetectorRef, Injector, Renderer2, ViewContainerRef, TemplateRef, DomSanitizer, UrlHandlingStrategy and many others are abstractions behind which Angular hides its current implementations and they can change in different versions while maintaining the interface.
But this does not mean that this is required everywhere. If you are writing a general-purpose lib that provides its own services, then it is still appropriate, but for a specific single UserService it will be too much.
All the same, you can always feed, if necessary, another UserService to the downstream component by replacing the object in the injector, di allows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question