Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting a service into another service

I have an MVC project which has two services an OrganisationService and an AgreementService, my problem is that some of the organisations belong to a group/parent structure, when this is the case I need to get all agreements that belong to any of the organisations within the group.

I already have a method in my OrganisationService that can return a list of all the ids for organisations within the structure:

IEnumerable<int> GetRelatedOrganisationIds(int id)

I could create a method in the AgreementService which accepts the result of this but then I would need to inject both services into my controller and call them in turn e.g.

GetAgreementsByOrganisationIdList(IEnumerable<int> organisationIdList)

Is it ok to inject the OrganisationService into the AgreementService so that it can do the work itself? For example the following method would call GetRelatedOrganisationIds internally:

GetAgreementsByOrganisationId(int id)

Another reason I would like to inject it into the AgreementService is that I would not need to remember to check if the organisation was in a group/parent relationship and look up the ids each time I wanted to get a list of agreements.

I also thought of creating an OrganisationGroupParentInformationProvider and injecting that into the AgreementService instead, I may have spent far too much time thinking about this one.... how would you do it?

like image 494
Newm Avatar asked Dec 05 '13 19:12

Newm


People also ask

Can I inject a service into another service?

Yes, the first thing is to add the @Injectable decorator on each services you want to inject. In fact, the Injectable name is a bit insidious. It doesn't mean that the class will be "injectable" but it will decorate so the constructor parameters can be injected.

Can we inject a service into another service in Angular?

Angular provides the ability for you to inject a service into a component to give that component access to the service. The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency.

Can I Autowire a service in another service?

Of course you can and it is perfectly fine. But I recommend that you use method-injection in order to allow you to set the instance at runtime without using using reflection (you can create an instance manually).

What's the best way to inject one service into another in Angular?

We must tell Angular to instantiate Service1 to be available (instatiated) before Service2. Hence, we declare it as a provider into our module. In this small example application, we have only one module, the app. module.


1 Answers

Yes, it would be fine to inject one service into the constructor of another. However, you might want to consider creating an interface for OrganisationService and having your AgreementService depend upon that abstraction, instead.

Another approach would be to create a new service with dependencies on both AgreementService and OrganisationService, and have that new service carry out the responsibility. The new service would of course be injected into your controller.

For guidance, consider whether having it all under AgreementService would violate the Single Responsibility Principle and/or Interface Segregation Principle. If so, make a new service.

like image 116
Facio Ratio Avatar answered Nov 04 '22 08:11

Facio Ratio