Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC : Can a service depend on other service?

I am pretty new working with the MVC pattern (Spring MVC). I have a very simple question. Can a service have a dependency on another service? something like:

@Service
public class MyFirstService{

   .....

   @Autowired
   private MySecondService secondService;

   ......
 }

Is this a "good practice" or is something that should be avoided?

Thanks!

like image 496
mario595 Avatar asked Jul 05 '13 09:07

mario595


People also ask

Can a service depend on another service?

Services can have dependencies on other services. A service that other services depend on is called an antecedent service. A dependent service depends on the antecedent service to be running so that the dependent service can function properly.

Can a service call another service MVC?

It is perfectly fine to have a service calling another service if that business code defined in the method is the same business functionality needed by the other service.

Can service call another service spring?

It is not any restriction calling a service from another one. Unless you make circular dependency between services.

How do I inject a service to another service in C#?

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.


2 Answers

There is a simple answer: yes.

One service depending on another service makes sense. Else it is possible that you have code duplications.

One example that comes into mind is having an EmailService. I don't want to write email sending code several times so that I create a service out of this. This service would be called by other services naturally.

like image 68
Uwe Plonus Avatar answered Sep 21 '22 20:09

Uwe Plonus


Yes. the expectation of service layer is to implement business logics of the application. assume that one business logic (implemented in one service) may need to use other service to do its operations. for example, LoanService module may access the InterestService to calculate the interest of the loan.

@Service
public class LoanService{

   .....

   @Autowired
   private InterestService interestService;

   ......
}
like image 21
Chathuranga Tennakoon Avatar answered Sep 19 '22 20:09

Chathuranga Tennakoon