Search code examples
design-patternsdependenciesdependency-inversion

Finding the High-Level and Low-Level modules in dependencies for applying Dependency Inversion Principe


The Dependency Inversion Principle say:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend upon details. Details should depend upon abstractions.

How can I practically find the High-level and the Low-level modules in my applications, is there any clear definition for them?


Solution

  • This is how usually I refer them:

    High Level Module --> this module represent more business aspect rather than technical aspect. It can be refered as an abstraction rather than implementation, and usually achieved through interfaces.

    Some example maybe: RegisterAccount, PostAnswer, PostQuestion, AddComment, InsertComment.

    Since Low Level Module represent more technical aspect rather than the business aspect. Say for example we take the InsertComment HLM. The LLM should be:

    1. Open database connection
    2. Execute insert statement
    3. Close database connection

    A High Level Module can be a Low Level Module in another context. Taking another example, AddComment's LLM should be:

    1. Validate the comment (e.g. 15 char min) --> this will be another HLM
    2. Insert comment to database --> this will be another HLM (InsertComment)
    3. Add notification to involved user --> this will be another HLM

    The same apply for other HLM as well.