Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In multi-tier architecture with a service layer, is it acceptable to have one service call another service?

I have a multi-tiered app with a data layer containing repositories.

On top of this, I have a service layer. My understanding is that there should be a single service for each repository.

Is it ok to have Service A make a call to another method in ServiceB? This would, of course, create a dependency on Service B in Service A(I am using interfaces and DI).

In my example I have a User service which handles, adding users, authenticating users, finding a user by ID, etc. I also have a Book service which allows me to add book for a specific user.

Should the book service make a call to the user service to retrieve a User instance for which to add books to?

like image 353
stephen776 Avatar asked Oct 26 '11 18:10

stephen776


1 Answers

Short answer: yes

A bit less short:

Your alternatives are to either let the "book service" directly access the "user repository" or to extend the user service so it is able to add a book - neither a good option... so it is correct to do what you describe (Book Service accesses user service)... a more "pure option" would be to create a controller/aggregate/transaction/coordinator service that uses the book service and the user service to achieve what you describe, this way both "direct services" (book and user) stay "dependency-free"...

like image 198
Yahia Avatar answered Sep 17 '22 23:09

Yahia