Article · Blog
The dependency you don't see because it's hiding behind a type you created yourself
Clean Architecture promises dependency inversion. In practice, most mobile codebases have concrete dependencies disguised as abstractions. The problem isn't the architecture. It's that the type you created to hide the detail became the detail itself.
The dependency you don't see because it's hiding behind a type you created yourself
In a financial app I maintained for almost two years, there was a protocol called PaymentGatewayProtocol. Sixteen methods. Its own enumerations for transaction status, card type, error code. All of it inside the domain module, just like Clean Architecture says it should be.
The problem showed up when we tried to swap the payment gateway.
Replacing the external SDK took two days. Adapting the enumerations and domain types took three weeks.
The protocol was in the domain. The abstractions were in the right place. But the types that protocol used carried the exact format the original gateway returned. Dependency inversion was there on paper. In practice, the domain had been modeled on top of the gateway, not the other way around.
What most people call an abstraction
When you create a protocol or an interface to isolate an external dependency, the intention is right. The domain shouldn't know about Stripe, Firebase, or Alamofire.
But the mistake happens one level below.
You create UserRepository as a protocol in the domain. Fine. But the method returns a UserDTO that came from the API. Or it returns a UserEntity that has a providerID field which only exists because Firebase uses that name. Or it throws a RepositoryError with cases that map exactly to the HTTP codes you get from the backend.
The protocol is in the right place. The types it carries are in the wrong place.
And those types are invisible as a dependency because they don't import any framework. They're pure Swift. Innocent structs you wrote yourself. Nobody is going to flag them in a review.
Why this is worse than a direct dependency
A direct dependency is easy to spot. import Alamofire in the middle of a UseCase jumps out at you. Any developer on the team will question it.
A type you own that carries the shape of an external system doesn't jump out anywhere. It looks like domain. It has the right name, it's in the right package, there's no external import. But it was shaped from the outside.
At Compass UOL, when I worked on registration and profile editing modules that were reused across 23 teams, this pattern showed up constantly. Modules that looked independent, but were carrying response models straight from the backend's API contract. When the backend changed a field, it didn't break one module. It broke all 23.
The dependency was hiding inside the types.
The real problem behind the problem
Clean Architecture solves where things live. It doesn't solve how types are designed.
You can have every layer in the right place, every protocol where it belongs, every import pointing in the right direction, and still have a domain that can't survive a backend change.
What determines whether an abstraction actually protects the domain isn't where the protocol is declared. It's whether the types it uses were designed from what the business needs or from what the external system delivers.
That distinction seems obvious when written out like this. In production, under delivery pressure, it disappears. You get the API response, create a model, put it in the domain because "it's a domain entity," and move on.
Three months later, the backend changes the contract. And you realize the domain is coupled to something that should never have gotten in there.
What actually works in practice
The question I started asking before any type enters the domain is: was this type designed outside-in or inside-out?
Outside-in: you look at what the external system delivers and create a model that represents that.
Inside-out: you define what the use case needs and create a model that represents that.
The difference is subtle on day one. It's enormous by the tenth change.
In practice, this means the mapping between the external format and the domain format has to happen in the data layer, not in the domain. The mapper is the pressure point. It's the one that takes the hit when the backend changes, not the UseCase, not the screen.
This became clear when I worked on the account module for Banco do Brasil. Banking APIs have a specific, verbose contract, with historical fields that exist purely for compatibility. If you let that contract bleed into the domain, every screen that shows a balance or a statement ends up coupled to the bank's internal structure. The mapper absorbs the change. The domain never knows it happened.
The architectural decision that changes everything
Explicit mapping at every layer boundary.
This isn't a new idea. It's in any Clean Architecture book. The problem is that it's tedious to do and easy to skip when you're on your fifth sprint in a row with no breathing room.
What I've learned is that the mapper isn't overhead. It's the investment that makes the abstraction worth what you paid for it.
Without a mapper, you have a protocol that looks like an abstraction but works like an alias. With a mapper, you have a real boundary. The domain can evolve independently of the backend. The backend can change its contract without breaking the business logic.
The trade-off is code volume. You'll have duplicate structs. One for what the API returns, another for what the domain uses. You'll have conversions that feel redundant. On teams with less experienced developers, that generates pushback.
The alternative is worse. I've seen it. Reusing the API model directly across the whole app looks efficient at first and creates a rigidity that locks the entire team down when the product needs to move fast.
What I would do differently today
I would introduce the mappers from the very first module, not after the problem surfaced.
And I would explicitly separate types into two groups: domain types, which represent what the business needs, and integration types, which represent the format of external systems. No integration type enters the domain. No exceptions.
Sounds obvious. But delivery pressure creates exceptions. And every exception is a dependency you won't see until you try to remove it.
Dependency inversion without model inversion isn't Clean Architecture
It's decoration.
You can spend years convinced your architecture is correct because the imports point in the right direction, the protocols are in the domain, and the frameworks live in the data layer.
But if the models traveling between those layers were shaped by external systems, you don't have a domain. You have a presentation layer with more files and a nicer name.
The dependency you don't see is more dangerous than the one you import directly. Because it doesn't show up in the dependency graph. It doesn't show up in the review. It shows up when you try to change something and discover that everything is connected in a way nobody can explain.