• Jan 16, 2025

Why Your Software Is Hard to Change?

For many years, I misunderstood the Dependency Inversion Principle (DIP). This led to design mistakes that often caused rigid systems.

For many years, I misunderstood a core principle of software design—the Dependency Inversion Principle (DIP). This misunderstanding led to design mistakes that often caused rigid systems.

But the Dependency Rule, as taught by Uncle Bob (Robert C. Martin), offers a clear path to a more adaptable architecture. Let's explore what not to do, and see how following the Dependency Rule can transform our software designs.

The Dependency Rule

At its heart, the Dependency Rule states that high-level policies must not depend on low-level details. Instead, low-level details should depend on high-level policies.

This means that the core logic driving your application should remain insulated from changes in databases, frameworks, or external systems.


What Are High-Level Policies?

High-level policies consist of business rules that make or save money for your company. They define the key operations of your domain and encapsulate your business's most important processes.

High-level policies form the backbone of your application, unlike low-level details like database connections or specific message brokers, which should act as supporting tools, not primary decision-makers.


Differentiating Business Rules

Business rules can be broadly divided into application-specific and domain-specific categories:

  • Domain-specific business rules encapsulate the core business logic that remains consistent across applications. These rules define the foundational operations of your domain and can function independently of any automated system.

    For example, in an accounting domain, rules governing double-entry bookkeeping, account balancing, or tax calculations could be documented on paper manually. This illustrates how domain-specific rules are inherently tied to the business itself, rather than the technology used to automate it.

  • Application-specific business rules govern how users interact with your system. These rules are specific to the automated applications where users engage, such as mobile apps, Web APIs, Web MVC applications, or worker services. They handle user inputs, responses, and other interactions while relying on the core business rules to perform their logic.


The Mistake I Made

One significant anti-pattern I struggled with was placing both the interface (e.g., IService) and its concrete implementation classes in the same DLL/JAR package.

By organizing interfaces and their implementations together, you end up with a design that makes it difficult to swap out implementations without potentially disrupting business rules. This mistake leads to systems that less adaptable to evolving requirements.

Look at this ordering service example: the business layer is tightly coupled to changes made in the database. If a database column changes, my business rules would also need to change in this flawed design. This coupling makes the system brittle, making even minor changes potentially expensive.

What not to do with software design


What Does "Easy to Change" Mean?

Software must be "soft"—easily changeable to adapt to new business requirements.

When high-level policies are kept insulated, and low-level details are structured as interchangeable parts, modifying one aspect of the system becomes far easier.

For example, swapping out a database, changing an external system integration, or updating a message broker should not ripple through the core logic of the application. Instead, these low-level details should function as replaceable plugins, seamlessly integrating with the interfaces defined in your business layer.

This approach ensures that changes are isolated, making maintenance faster, reducing risk, and enabling smoother adaptations over the lifespan of your software.

Software must be soft

Applying Dependency Inversion Correctly

When done correctly, Dependency Injection (DI) allows low-level details like databases, external systems, and message brokers to act as plugins. These details implement interfaces defined in the business layer, ensuring that the core logic remains insulated from technical complexities.

Now look at the following design: With this change, when a database column changes, the business rules remain unaffected. The business rules now depend on nothing external, while the data layer depends on the business rules.

The data mapper implements interfaces that reside in the business layer, not the other way around. This approach insulates the business rules from changes in the database or external system integrations, providing adaptability to the overall system.


Conclusion

Understanding the Dependency Inversion Principle is transformative for software design. By ensuring that high-level policies remain independent of low-level details, you create systems that are flexible and resilient to change.

Embracing this principle means designing software where low-level details act as swappable plugins that implement well-defined interfaces in your business layer. This keeps your core logic clean while insulating it from technical complexities.

Ultimately, by applying these concepts, you can make your software truly "soft"—ready to meet evolving business needs.

Related Courses

Watch me build a clean architecture from scratch. Starting with a blank solution, learn why good software design begins with correctly inverting dependencies between layers. → Dependency Inversion Hands-On

.

If you are designing systems like the ones discussed here, this toolbox might help.

  • Free email delivery

The Software Architect Toolbox

The set of diagram pieces you can use to create awesome architecture visuals. The library package is regularly updated. Every time I find a new artifact or draw one myself, I add it because I think you should have it in your arsenal.

You're signing up to receive emails from Justified Code.