Search code examples
c#architectureonion-architecture

Can a layer method call another method on the same layer in an onion architecture?


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?


Solution

  • 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.

    1. 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.

    2. Check out also - Does Vertical Slice Architecture Violate the DRY Principle?