0

I have a DI problem I am trying to resolve without bloating my code:

The setup is as follows:

I have a base form class 'A' that communicates with some of its child components employing service 'B'.

Class 'A' has many (say 50) inheriting form components, and if I want to provide the service to each, I would have to go through each component declaration and provide the service 'B'.

So, why not just provide in root you ask? Well, this service needs to live just for the while the form lives, and although it is possible, it will require me to track the life of the form and on deactivate guard clear the service ... Not a very formidable way of solving the problem (so to speak...).

Another thought that crossed my mind was to provide the service ... dynamically ... But I think this is not supported / the way to do things in Angular? Probably this is just not supported.

I could provide a Service Locator pattern, but this will just compete with the existing DI.

rubmz
  • 1,947
  • 5
  • 27
  • 49
  • 1
    Does this answer your question? [Inject a service manually](https://stackoverflow.com/questions/42461852/inject-a-service-manually) – Igor Apr 06 '20 at 21:08
  • 1
    BTW I believe you are referring to the ["Service Locator Pattern"](https://en.wikipedia.org/wiki/Service_locator_pattern). This is *usually* considered an anti-pattern but that does not mean it should not be used in some situations. – Igor Apr 06 '20 at 21:09
  • Nice suggestion, but more like a bypass that wont be accepted by my peers (antipattern...) – rubmz Apr 06 '20 at 21:33
  • 1
    Maybe a centralized EventService would work? https://dev.to/jwp/the-angular-event-service-ech – JWP Apr 07 '20 at 00:47
  • Seems to me those are all bypasses to a hole in the DI paradigm or implementation in Angular.... – rubmz Apr 07 '20 at 07:50
  • Then maybe I am not understanding what you need because both of those issues address your problem as I understand it. If that is not the case update your question with an [mcve] that illustrates your issue exactly, then it will be easier to comprehend the problem and possibly also the solution. – Igor Apr 07 '20 at 12:07

0 Answers0