Don't seem to find a good explanation for this. Is it a bad practice to have e.g. a method on one layer call another method on the same layer?
- Yes it can, Very common with application services.Code Name Jack– Code Name Jack2023-11-08 10:44:24 +00:00Commented Nov 8, 2023 at 10:44
- So it is a good practice if I got an order service method with its own dependencies I can call a user service with some different dependencies ? Is there a documentation for this answer ?Magni Hansen– Magni Hansen2023-11-08 10:52:43 +00:00Commented Nov 8, 2023 at 10:52
- wouldn't make much sense, if model can ever only talk to view-model and vice versa, would it? Of course you can have deep call-stacks within the same "layer".MakePeaceGreatAgain– MakePeaceGreatAgain2023-11-08 11:16:52 +00:00Commented Nov 8, 2023 at 11:16
- You should include the exact scenario you have in the question with real examples. Architecture guidelines work best when these details are known. On a Higher level, As long as you follow SRP and good Encapsulation it should work.Code Name Jack– Code Name Jack2023-11-08 12:37:05 +00:00Commented Nov 8, 2023 at 12:37
1 Answer
In short - it depends.
The main concern is to prevent circular dependencies between same level components (which is not an issue when performing cross-layer calls for obvious reasons) i.e. ServiceA needs ServiceB which needs ServiceA. To prevent this I personally either introduce a "sub-layer" for shared functionality (something similar to the enterprise and application services separation in service oriented architecture) or move them into some kind of helper methods.
P.S.
Sometimes it is ok to duplicate similar code (instead of moving it into some shared component/invoking existing one). The famous Don't repeat yourself (DRY) principle is quite often should be applied not to similar code but "business knowledge" - i.e. the same functionality/concept should be coded only once.
Check out also - Does Vertical Slice Architecture Violate the DRY Principle?