面向第三方 API 的防腐层
通过构建防腐层,将外部模型中的陌生概念转换为您自己的概念,从而保护整洁领域免受混乱的外部模型影响。
面向第三方 API 的防腐层 是 CoddyKit 上的免费 Clean Architecture & Design Patterns in Practice 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clean Architecture & Design Patterns in Practice 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
When the Outside World Is Messy
Repositories and gateways shield you from where data lives. But external APIs also impose their own vocabulary and shape — often inconsistent or poorly designed.
An Anti-Corruption Layer (ACL) stops that mess from leaking into your domain.
The Concept
The term comes from Domain-Driven Design. An ACL is a translation boundary between your model and a foreign model.
- Your core speaks its own language.
- The ACL converts to and from the external language.
What Goes Wrong Without One
If a third-party JSON shape spreads through your code, every quirk of their API becomes your problem. A rename on their side breaks dozens of your files.
The ACL concentrates that coupling in one replaceable place.
Your Domain Model
Define the clean model your core actually wants.
class Customer {
final String id;
final String fullName;
Customer(String id, String fullName) {
this.id = id; this.fullName = fullName;
}
}The Foreign Model
The external service returns something awkward and unstable.
class ExternalUserDto {
public String usr_id;
public String fname;
public String lname;
public int status_code;
}The Translator
The ACL maps foreign concepts to your domain, hiding all the quirks.
class CustomerTranslator {
Customer toDomain(ExternalUserDto dto) {
return new Customer(dto.usr_id, dto.fname + " " + dto.lname);
}
}Wiring It Behind a Gateway
The ACL lives behind a gateway interface defined by your core, so the rest of the app never sees the foreign type.
interface CustomerGateway { Customer findById(String id); }
class HttpCustomerGateway implements CustomerGateway {
private final CustomerTranslator translator = new CustomerTranslator();
public Customer findById(String id) {
ExternalUserDto dto = callApi(id);
return translator.toDomain(dto);
}
private ExternalUserDto callApi(String id) { return new ExternalUserDto(); }
}ACL vs Plain DTO Mapping
A simple DTO mapper just renames fields. An ACL goes further: it can reconcile conflicting concepts, default missing data, and reshape relationships so the external model truly cannot corrupt yours.
Handling Semantic Mismatches
External systems may model the world differently — different status codes, different units, different identity rules.
The ACL is where you resolve these semantic gaps, presenting one consistent meaning to your core.
Keeping the Boundary Honest
Rules for a healthy ACL:
- Foreign types never cross into the domain.
- The domain never imports the external SDK.
- All translation logic lives in the ACL, fully testable in isolation.
The Replaceability Payoff
When the third party changes — or you switch vendors entirely — you rewrite one translator.
Your entities, use cases, and the rest of the application remain untouched. That is the whole point.
Quick Check
Test your understanding of anti-corruption layers.
Recap
You learned to defend the domain with an anti-corruption layer.
- It translates foreign models into your own vocabulary.
- It sits behind a core-owned gateway interface.
- It localizes vendor coupling so changes touch one place.
用 AI 导师学习 Clean Architecture & Design Patterns in Practice — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「面向第三方 API 的防腐层」课时是免费的吗?
是的 — 「面向第三方 API 的防腐层」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clean Architecture & Design Patterns in Practice 课程的其余内容,请升级到 CoddyKit PRO。 Clean Architecture & Design Patterns in Practice 课程共包含 4 节课。
「面向第三方 API 的防腐层」这节课中我会学到什么?
通过构建防腐层,将外部模型中的陌生概念转换为您自己的概念,从而保护整洁领域免受混乱的外部模型影响。 你通过在浏览器中直接运行的动手代码来练习 Clean Architecture & Design Patterns in Practice,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Clean Architecture & Design Patterns in Practice 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Clean Architecture & Design Patterns in Practice 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「面向第三方 API 的防腐层」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Clean Architecture & Design Patterns in Practice 课中编写并运行代码吗?
能。每节 Clean Architecture & Design Patterns in Practice 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 整洁架构中的仓储模式
- 面向外部系统的网关接口
- 数据映射器与 DTO
- 面向第三方 API 的防腐层