Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC practices. Service within another service

Service1 injects Repository1. Service2 injects Repository2.

Suppose two different scenarios:

1) Some method of Service2 needs to retrieve data from Repository1. Should Service2 inject Service1 or Repository1 when both of them provide respective get() method?

2) Some method of Service1 at it's end should call another method from Service2. Is it a bad practice to inject Service2 to Service1 for such needs? Is it a good practice to use event listen techniques like AOP for such needs?

like image 230
serhii Avatar asked Sep 26 '15 14:09

serhii


1 Answers

There are many factors to consider here when we talked about best practices.

As a good start, try to understand the concept of SOLID principles. Generally, it is good to have multiple classes with very focused roles that calls the other rather than combining all functionalities in one class. High reusability and least code duplication which in turn gives maintainability.

For scenario 1.)

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. This follows the DRY principle, no redundant codes.

But it is also perfectly fine to just directly call the Dao from a service instead of calling a different service to do that for you if it is just a simple call with no further business logic. Especially if the two services are in the same module anyway, there is no strong reason to make another service a bridge class for an obvious simple single line of code unless you want to abstract it, but in your case, its just a simple get call.

For scenario 2.)

But another thing to consider is modularity and direction of dependency. If each service calls each other, there could be problem in your design, as much as possible avoid circular dependency on different modules because this could lead to spaghetti code, better to extract same code to a different class declared on common module that can be shared by many modules.

Final note, as what Robert Martin says, you won't be able to code at once the cleanest code in one round. Best codes are forged by continuous refactoring and code cleanup. To quote Robert Martin,

The Boy Scouts have a rule: "Always leave the campground cleaner than you found it."

like image 132
vine Avatar answered Oct 17 '22 19:10

vine