设计微服务应用层与 Web API

Use SOLID principles and Dependency Injection    运用 SOLID 原则与依赖注入

SOLID principles are critical techniques to be used in any modern and mission-critical application, such as developing a microservice with DDD patterns. SOLID is an acronym that groups five fundamental principles:

SOLID 原则是在任何现代且关键业务应用(例如使用 DDD 模式开发微服务)中都必须使用的核心技术。SOLID 是由五个基本原则组成的缩写词:

  • Single Responsibility principle

    单一职责原则

  • Open/closed principle

    开闭原则

  • Liskov substitution principle

    里氏替换原则

  • Interface Segregation principle

    接口隔离原则

  • Dependency Inversion principle

    依赖倒置原则

SOLID is more about how you design your application or microservice internal layers and about decoupling dependencies between them. It is not related to the domain, but to the application's technical design. The final principle, the Dependency Inversion principle, allows you to decouple the infrastructure layer from the rest of the layers, which allows a better decoupled implementation of the DDD layers.

SOLID 原则关注的是如何设计应用程序或微服务的内部层级,以及如何实现它们之间的依赖解耦。它与业务领域无关,而是关乎应用程序的技术设计。上述原则中的最后一条——依赖倒置原则,它允许你将基础设施层与其余层级进行解耦,从而实现 DDD 层级结构的更好分离。

Dependency Injection (DI) is one way to implement the Dependency Inversion principle. It is a technique for achieving loose coupling between objects and their dependencies. Rather than directly instantiating collaborators, or using static references (that is, using new…), the objects that a class needs in order to perform its actions are provided to (or "injected into") the class. Most often, classes will declare their dependencies via their constructor, allowing them to follow the Explicit Dependencies principle. Dependency Injection is usually based on specific Inversion of Control (IoC) containers. ASP.NET Core provides a simple built-in IoC container, but you can also use your favorite IoC container, like Autofac or Ninject.

依赖注入(Dependency Injection, DI) 是实现“依赖倒置原则”的一种方式。它是一种在对象与其依赖项之间实现松耦合的技术。具体做法是:类不再通过直接实例化协作对象或使用静态引用(即 new...)来获取所需对象,而是通过构造函数等方式,由外部将这些对象“注入”到类中。大多数情况下,类会通过构造函数来声明其依赖项,这遵循了“显式依赖原则”。依赖注入通常基于特定的“控制反转(IoC)容器”来实现。ASP.NET Core 提供了一个简单的内置 IoC 容器,但你也可以使用自己喜爱的 IoC 容器,比如 Autofac 或 Ninject。

By following the SOLID principles, your classes will tend naturally to be small, well-factored, and easily tested. But how can you know if too many dependencies are being injected into your classes? If you use DI through the constructor, it will be easy to detect that by just looking at the number of parameters for your constructor. If there are too many dependencies, this is generally a sign (a code smell) that your class is trying to do too much, and is probably violating the Single Responsibility principle.

遵循 SOLID 原则,你的类自然会变得小巧、结构清晰且易于测试。但是,你如何判断是否向类中注入了过多的依赖项呢?如果你通过构造函数使用 DI,只需查看构造函数的参数数量就能轻易察觉。如果参数过多,这通常是一个警示信号(代码异味),表明你的类试图承担太多职责,很可能违反了单一职责原则

It would take another guide to cover SOLID in detail. Therefore, this guide requires you to have only a minimum knowledge of these topics.

要深入详尽地讲解 SOLID 原则需要另一份专门的指南。因此,本指南只要求你对这些主题具备最基本的认知即可。

posted @ 2026-05-19 11:03  菜鸟吊思  阅读(8)  评论(0)    收藏  举报