

六边形架构:为什么它比 MVC 或Clean架构更好?
source link: https://www.jdon.com/67036.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

六边形架构:为什么它比 MVC 或Clean架构更好?
六边形架构,也称为端口和适配器,是一种促进松散耦合、可测试性和可扩展性的流行方法。这种架构风格使开发人员能够创建健壮且适应性强的系统。
本文探讨了六边形架构的概念,并提供了实际用例、代码示例和比较,以帮助您了解这种方法的好处。此外,它还研究了如何使用 Python 有效地实施这种方法。
了解六边形架构
由 Alistair Cockburn 引入的六边形架构通过将核心业务逻辑与外部依赖项(如数据库、框架或用户界面)解耦来强调关注点分离。该体系结构围绕通过使用端口和适配器定义清晰边界的想法展开。
- 端口:端口定义与应用程序核心交互的接口。它们代表核心完成其任务所需的合同或合同模板。端口可以是入站或出站,具体取决于它们是处理传入数据还是传出数据。
- 适配器:适配器实现端口并充当核心和外部依赖项之间的粘合剂。根据其角色,适配器可以是主要的或次要的。主要适配器处理外部输入,例如 REST API 端点或用户界面,而次要适配器与外部系统交互,例如数据库、外部服务或消息代理。
六边形架构的好处
六边形架构提供了几个有助于提高软件系统整体质量和可维护性的优势:
- 灵活性和敏捷性:通过将核心逻辑与外部问题分离,六边形架构允许更轻松地修改或替换组件。这种灵活性有助于敏捷地适应不断变化的需求或集成新技术。
- 可测试性:六边形架构通过将核心业务逻辑与外部依赖项隔离开来提高可测试性。由于核心不依赖于具体实现,因此测试可以轻松地模拟或存根外部交互,从而实现全面的单元测试和更快的反馈循环。
- 可扩展性:明确的关注点分离可实现可扩展性。核心逻辑可以独立扩展,而不会影响适配器或外部系统。这种灵活性有助于构建能够有效处理不同负载需求的系统。
- 可持续的代码库:六边形架构有助于维护干净且可维护的代码库。关注点分离和松散耦合确保一个区域的更改对系统其他部分的影响最小,从而降低引入意外副作用的风险。
在 Python 中实现六边形架构
为了演示在 Python 中使用六边形架构,我们将使用一个简单的博客应用程序示例。我们的重点将放在核心博客功能上,同时使适配器尽可能简洁。此示例描述了管理博客文章的核心逻辑。主适配器通过 REST API 管理传入的 HTTP 请求,而辅助适配器与 PostgreSQL 数据库交互。
第 1 步:定义端口和适配器
首先,我们将必要的端口定义为接口或抽象类。这些端口作为适配器必须遵守的协议。
class BlogRepository: def get_post(self, post_id: int) -> Optional[Dict[str, Any]]: pass def create_post(self, post_data: Dict[str, Any]) -> int: pass def update_post(self, post_id: int, post_data: Dict[str, Any]) -> bool: pass def delete_post(self, post_id: int) -> bool: pass class BlogService: def get_post(self, post_id: int) -> Optional[Dict[str, Any]]: pass def create_post(self, post_data: Dict[str, Any]) -> int: pass def update_post(self, post_id: int, post_data: Dict[str, Any]) -> bool: pass def delete_post(self, post_id: int) -> bool: pass |
第 2 步:实施适配器
我们现在将创建满足端口合同中规定的要求的适配器。
class BlogRepositoryPostgreSQL(BlogRepository): def get_post(self, post_id: int) -> Optional[Dict[str, Any]]: # Implementation using PostgreSQL database def create_post(self, post_data: Dict[str, Any]) -> int: # Implementation using PostgreSQL database def update_post(self, post_id: int, post_data: Dict[str, Any]) -> bool: # Implementation using PostgreSQL database def delete_post(self, post_id: int) -> bool: # Implementation using PostgreSQL database class BlogServiceHTTP(BlogService): def __init__(self, blog_repository): self.blog_repository = blog_repository def get_post(self, post_id: int) -> Optional[Dict[str, Any]]: # Implementation using HTTP requests self.blog_repository.get_post(post_id) def create_post(self, post_data: Dict[str, Any]) -> int: # Implementation using HTTP requests self.blog_repository.add_post(post_data) def update_post(self, post_id: int, post_data: Dict[str, Any]) -> bool: # Implementation using HTTP requests self.blog_repository.update_post(post_data) def delete_post(self, post_id: int) -> bool: # Implementation using HTTP requests self.blog_repository.delete_post(post_id) |
第 3 步:组装应用程序
为了组装应用程序,我们创建了必要的适配器并连接它们。
def assemble_application(): blog_repository = BlogRepositoryPostgreSQL() blog_service = BlogServiceHTTP(blog_repository) return blog_service |
第 4 步:使用六边形架构
现在组装好应用程序,我们可以利用它来执行与博客相关的操作:
def main(): app = assemble_application() post_data = { "title": "Hello, Hexagonal Architecture!", "content": "This is a blog post created using Hexagonal Architecture in Python.", } post_id = app.create_post(post_data) print("Post created with ID:", post_id) post = app.get_post(post_id) print("Retrieved post:", post) post_data["content"] = "Updated content!" app.update_post(post_id, post_data) print("Post updated!") app.delete_post(post_id) print("Post deleted!") if __name__ == "__main__": main() |
六边形架构与其他模式的比较
Hexagonal Architecture 与 MVC(Model-View-Controller)和 clean Architecture 等其他架构模式有相似之处,但它的不同之处在于关注点分离以及端口和适配器的显式使用。
下面是六边形架构与 MVC 和清洁架构的比较:
- 分离关注:六边形架构有将内外核心非常强地解耦,而MVC只是分离模型 视图和控制器三者,Clean整洁结构只是定义每个层的更清晰的职责。
- 可测试:六边形架构因为松耦合可测试;MVC测试逻辑不那么容易;Clean架构通过分层实现了测试。
- 灵活性:容易修改和替代适配器,MVC的控制器与框架耦合,Clean架构能不影响核心层情况下替换适配器。
- 可伸缩扩展性:六边形允许各自灵活扩展,MVC没有可扩展性;Clean架构的扩展性依赖层。
- 复杂性:引入了端口和适配器等复杂概念,MVC引入中介者 复杂性,clean有高复杂性。
结论
六边形架构提供了一种健壮的方法来构建灵活且可维护的软件系统。通过将核心业务逻辑与外部依赖项分离,开发人员可以在其应用程序中实现更高的可测试性、可扩展性和可持续性。
在本文中,我们探讨了六边形架构的基础知识,演示了它在 Python 中的实现,并将其与其他流行的架构模式进行了比较。
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK