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

I have a layered application with a data layer containing repositories.

In addition, I have a service level. I understand that there must be one service for each repository.

Can I use Service A to call another method in ServiceB? This, of course, would create a dependency on service B in service A (I use interfaces and DI).

In my example, I have a User service that processes, adds users, checks users, finds a user by ID, etc. I also have a Book service that allows me to add a book for a specific user.

If the book service makes a call to the user service to get the user instance for which to add books?

+4
source share
3 answers

Short answer: yes

Slightly less:

Your alternatives should either allow the "book service" to directly access the "user repository", or expand the user service so that it can add a book - not a good option ... so do what you describe correctly (Book Service accesses the service user) ... a cleaner option would be to create a controller / aggregate / transaction / coordinator service that uses the book service and user service to achieve what you describe in this way, direct services "(book and user) remain I "without depending on" ...

+6
source

Yes, this is acceptable if it makes sense to do so.

If the book service requires the logic to run from the user service and the book service does not call the user service, you will have to duplicate the logic of the user service in the book service. This means that if your business logic changes for what the service logic does, you must make sure to update the book service with the logic, otherwise you will get errors and inconsistent (and unexpected) behavior.

However, if you find that this happens a lot, you may have to start exploring what functionality each service performs, and perhaps break them down into smaller services.

+1
source

The user mentioned that he used EF. In this case, he can either transfer the User object to the Book Service or the User ID and create a user object in the Book Repository before saving.

0
source

All Articles