الانتقال إلى المحتوى
Software Architectureمبادئ التصميم· 7 دقائق للقراءة

Coupling and Cohesion in Practice: Why More Layers Don't Mean Looser Code

Coupling is measured by how far a change ripples, not by how many boxes the diagram has. Keep what changes together in one module, and keep what crosses module lines small.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

Product wants customers to pick a delivery date at checkout. It's one new field, and the pull request touches four projects: a request contract in Api, a DTO and a pass-through service in Application, a domain record, an EF Core row class in Infrastructure, and three mappings between them. The team calls the solution loosely coupled because each layer only talks to the next one through an interface. Yet nothing in it can change alone.

The common misconception

The belief: loose coupling comes from separation, so the more classes, layers and projects you split code into, the looser it gets. Every box between two pieces of code decouples them a little more.

There's real experience behind it. Layering is one of the most useful ideas in application design: Martin Fowler says he regularly uses and encourages presentation, domain and data layers. Microsoft's architecture guide recommends keeping business rules in their own project. Projects let the compiler enforce references, and a diagram with many boxes and one-way arrows looks tidy.

The half-truth: a well-placed layer does shield code from a specific detail, such as the UI from the database. But a layer only reduces coupling when it hides something. A layer that passes the same data through adds one more place to change.

What actually happens

Coupling is counted in changes, not boxes

Coupling and go back to Larry Constantine, who developed them in the mid-1960s and first published them in 1968; the 1974 paper "Structured Design" explained them to a much wider audience. Fowler's 2001 column boils down to one test: "If changing one module in a program requires changing another module, then coupling exists." He adds a sharper point: "Duplication always implies coupling, because changing one piece of duplicate code implies changing the other." Four types with the same fields are the same knowledge written four times, which is the problem the DRY article earlier in this path is about. Adding a fifth layer of mirror types makes every change bigger.

You can't remove coupling, only choose where it lives

Fowler is clear that zero coupling isn't the goal. Modules must communicate, otherwise "you'd just have multiple programs", and "if you ban coupling between modules, you have to put everything in one big module. Then, there would be lots of coupling--just all hidden under the rug." What matters is the shape: "I don't worry about the number of modules coupled together, but I look at the pattern of dependency relationship between the modules." Code that always changes together should sit inside one module, where that coupling is cheap. What crosses module boundaries should be small and stable.

Cohesion is the other half

A solution split into Controllers, Services and Repositories at the top scatters each feature across every folder, so cohesion per feature is low. Fowler's layering article keeps the layers but moves them down a level: "once any of these layers gets too big you should split your top level into domain oriented modules which are internally layered." Ordering, Billing and Shipping become the top-level modules, each with its own layers inside.

What layers actually buy you

Fowler's main reason for layers is focus: they let him "reduce the scope of my attention" and think about one concern at a time. Swapping implementations is a weaker reason in practice: he rarely hears of anyone actually replacing their data source. And the interface that breaks a dependency doesn't need its own project; in his words, "there are separate packages, but this isn't necessary." Microsoft's guide names the property that really loosens coupling: encapsulation, where components "adjust their internal implementation without breaking their collaborators as long as external contracts are not violated."

The fix: modules by domain, small public surface

C#
// Api: the request contractpublic sealed record CreateOrderRequest(int CustomerId, DateOnly DeliverOn);// Application: the same fields again, behind a pass-through servicepublic sealed record CreateOrderDto(int CustomerId, DateOnly DeliverOn);// Domain: the same fields againpublic sealed record OrderData(int CustomerId, DateOnly DeliverOn);public interface IOrderDomainService { Task<int> CreateAsync(OrderData d); }public sealed class OrderAppService(IOrderDomainService domain){    public Task<int> CreateAsync(CreateOrderDto dto) =>        domain.CreateAsync(new OrderData(dto.CustomerId, dto.DeliverOn));}// Infrastructure: the same fields again, for EF Corepublic sealed class OrderRow{    public int CustomerId { get; set; }    public DateOnly DeliverOn { get; set; }}// One new field: four types, three mappings, four projects

The delivery date now changes the Ordering module and its public contract, and nothing else. The module can still separate its endpoint, domain and data access inside; the difference is that those layers serve one domain, and the rest of the system sees only IOrdering. Fewer boxes on the diagram, smaller ripples on every change.

MythReality
More layers mean looser couplingCoupling is how far a change ripples; mirror types in every layer make each change touch all of them
Good design has no couplingFowler: modules must communicate; banning coupling just hides it in one big module
Every layer needs its own modelCopies of one shape are duplicated knowledge, and duplication always implies coupling
Separate projects are what decouple codeAn interface breaks a dependency; Fowler says separate packages aren't necessary
The top level should be Controllers, Services, RepositoriesFowler: once layers grow, split the top level into domain modules that are layered inside

How to apply it in your project

  1. Trace one recent changePick a small feature that shipped last month and list every file it touched. Mark the edits that only copied a field or forwarded a call.
  2. Remove pass-through layersDelete services that only forward calls and types that mirror another type field for field, unless the copy guards a contract other people depend on.
  3. Make the top level follow the domainGroup code by business area first, such as Ordering, Billing and Shipping, and layer inside each module only as much as it needs.
  4. Shrink each module's public surfaceMake module internals internal, expose one small interface or set of messages, and stop modules from reading each other's tables.

Key takeaways

  • Coupling means one change forces another; count ripples, not boxes.
  • A layer that re-declares the same data or only forwards calls adds coupling.
  • You can't remove coupling, only decide where it lives: inside cohesive modules, not across them.
  • Split the top level by domain and layer inside each module, as Fowler recommends for growing apps.
  • Loose coupling comes from small, stable contracts and hidden internals, not from the number of projects.

📚 المصادر

  1. Reducing Coupling — Martin Fowler, IEEE Software (2001)
  2. PresentationDomainDataLayering — Martin Fowler, bliki (2015)
  3. Larry L. Constantine — Computer Pioneers, IEEE Computer Society
  4. Architectural principles — .NET Architecture Guides, Microsoft Learn

التعليقات (0)