In developing an n-tier application, I seem to have hit a scenario where one service say service A needs to consume a method in service B. I do not want to duplicate logic but it does not seem like I should not have services calling one another either. What is the best way to handle this situation without violating any rules? I am thinking about taking the common method out of service B and add to another class and have both services inherit from this class.
Asked
Active
Viewed 395 times
1 Answers
1
Dependency Injection.
Service A expects a well defined service to be injected into it that is expressed with an interface. This way Service B can be injected as well as any other service implementing the same contract.

Wiktor Zychla
- 47,367
- 6
- 74
- 106
-
Am I violating that with the approach I am thinking of doing above? – user1790300 Mar 01 '14 at 22:14
-
DI is perfectly ok. On the other hand, inheritance could hurt as it introduces high coupling. – Wiktor Zychla Mar 02 '14 at 00:04
-
What would a more efficient approach be? – user1790300 Mar 02 '14 at 22:56
-
I don't see any approach more efficient than DI here. – Wiktor Zychla Mar 02 '14 at 23:00