首頁 > web前端 > js教程 > 主體

什麼是尖叫建築?

DDD
發布: 2024-09-23 22:31:34
原創
978 人瀏覽過

What is Screaming Architecture?

尖叫架構是由著名軟體開發人員和思想領袖 Robert C. Martin(通常被稱為「叔叔鮑伯」)提出的概念。這個術語可能聽起來非常規,但它代表了軟體設計中一個強大的原則,專注於使系統架構反映應用程式的主要關注點和用例。簡而言之,您的軟體架構應該「尖叫」其意圖和目的。

在這份綜合指南中,我們將探討 Screaming Architecture 的基礎知識、它與傳統軟體架構的對比、它在領域驅動設計中的重要性,以及如何在專案中實現此架構。我們還將介紹 Screaming Architecture 可以提高程式碼可讀性、可維護性和長期可擴展性的實際範例和場景。

為什麼要「尖叫」建築?

尖叫架構背後的想法是程式碼庫的主要結構應該立即傳達其業務目的。這與傳統架構形成鮮明對比,傳統架構可能強調技術框架、工具或其他次要問題。在尖叫架構中,領域著重優先於實現細節。

叔叔鮑伯用一個類比來說明這一點:想像走到一棟建築物前,看看它的建築。無需標誌,您通常就能辨別出這是圖書館、學校還是辦公室。這同樣適用於軟體架構。當您查看應用程式的資料夾結構和設計時,您應該立即了解它的用途。如果你正在建立一個會計系統,架構應該大喊“會計”,而不是“Django”、“Spring Boot”或“React”。

以框架為中心的架構的問題

在許多專案中,對技術框架的關注掩蓋了業務或領域邏輯。您會發現以下文件結構:

controllers/

services/

repositories/

models/
登入後複製

雖然這些目錄很有用,但它們描述了技術角色,而不是反映軟體解決的核心問題。例如,此結構告訴您系統使用 MVC(模型-視圖-控制器),但沒有深入了解系統是否處理財務資料、使用者管理或內容創建。

框架陷阱

過度強調框架會導致程式碼庫中的業務邏輯被技術樣板所掩蓋。圍繞框架約定所建構的系統與這些框架緊密耦合。如果你想改變框架或技術棧,重構就成為一項主要工作。 Screaming Architecture 主張保持領域邏輯乾淨且獨立,因此框架的選擇成為實作細節,而不是程式碼庫的核心結構。

領域驅動設計 (DDD) 中令人驚嘆的架構

領域驅動設計(DDD)和尖叫架構經常齊頭並進。 DDD 是一種強調技術和領域專家之間協作的軟體開發方法,它專注於以與實際操作緊密結合的方式對核心業務邏輯進行建模。

在 Screaming Architecture 中,領域模型和業務邏輯位於應用程式的中心,而其他所有內容(框架、資料庫、UI 和服務)都變成了外圍。關鍵思想是程式碼結構應該反映領域模型而不是技術實作細節。

以下是如何使用領域驅動原則以「尖叫」其意圖的方式建構專案:

/src
    /accounting
        Ledger.cs
        Transaction.cs
        Account.cs
        TaxService.cs
    /sales
        Order.cs
        Invoice.cs
        Customer.cs
        DiscountPolicy.cs
登入後複製

在此範例中,資料夾名稱直接反映了業務問題:會計和銷售。每個特定於域的類,如 Ledger、Transaction 和 Order,都放置在其相關的域上下文中。這種結構可以立即清楚系統的含義以及每個組件的適用位置。

程式碼範例 1:簡單的以領域為中心的結構

考慮一個處理訂單和庫存的電子商務應用程式。使用 Screaming Architecture,資料夾結構應該反映業務邏輯而不是技術角色:

/src
    /orders
        Order.cs
        OrderService.cs
        OrderRepository.cs
    /inventory
        InventoryItem.cs
        InventoryService.cs
        InventoryRepository.cs
登入後複製

這是訂單上下文中的基本程式碼範例:

public class Order
{
    public Guid Id { get; set; }
    public DateTime OrderDate { get; set; }
    public List<OrderItem> Items { get; set; }
    public decimal TotalAmount { get; set; }

    public Order(List<OrderItem> items)
    {
        Id = Guid.NewGuid();
        OrderDate = DateTime.Now;
        Items = items;
        TotalAmount = CalculateTotal(items);
    }

    private decimal CalculateTotal(List<OrderItem> items)
    {
        return items.Sum(item => item.Price * item.Quantity);
    }
}

public class OrderItem
{
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}
登入後複製

在此程式碼中,域概念(Order)位於前端和中心,OrderService 和 OrderRepository 等支援邏輯保存在單獨的檔案中。業務邏輯 (CalculateTotal) 是 Order 實體的一部分,而不是隱藏在服務或控制器中。

避免技術幹擾

Frameworks and libraries are crucial for software development, but they shouldn't dictate how your business logic is structured. Screaming Architecture advocates for pushing technical details like HTTP controllers, persistence layers, and database frameworks to the periphery.

Here’s an example that contrasts the traditional and screaming architectures:

Traditional Architecture:

/src
    /controllers
        OrderController.cs
    /services
        OrderService.cs
    /repositories
        OrderRepository.cs
    /models
        Order.cs
        OrderItem.cs
登入後複製

While this is technically correct, it doesn’t tell you what the system is for. The folder structure reveals nothing about the domain. Is it an e-commerce system? A financial application? It’s impossible to know without diving deep into the code.

Screaming Architecture:

/src
    /orders
        OrderController.cs
        OrderService.cs
        OrderRepository.cs
        Order.cs
        OrderItem.cs
    /inventory
        InventoryController.cs
        InventoryService.cs
        InventoryRepository.cs
        InventoryItem.cs
登入後複製

This structure immediately clarifies that the system handles orders and inventory. If you add more domains in the future (e.g., customers, payments), they’ll have a dedicated place in the architecture.

The Role of Clean Architecture

Screaming Architecture often aligns with Uncle Bob’s broader Clean Architecture principles. Clean Architecture promotes a separation of concerns, focusing on ensuring that business rules are independent of frameworks, UI, and databases. Screaming Architecture takes this a step further by suggesting that the project’s structure should reveal the core business logic.

Here’s a quick recap of Clean Architecture:

Entities: Core business objects and logic.

Use Cases: Application-specific business rules.

Interfaces: Gateways for frameworks and external systems.

Frameworks & Drivers: UI, databases, and other external components.

In a Clean Architecture project, domain concepts like Order, Customer, and Invoice are part of the central layer. Frameworks like ASP.NET Core, Django, or Rails are relegated to the outer layers, serving as mechanisms to deliver the core functionality.

Code Example 2: Applying Clean Architecture in Screaming Architecture

In a Screaming Architecture, you’d structure the use cases and entities in a way that reflects the business domain. Let’s extend our e-commerce example:

/src
    /orders
        CreateOrderUseCase.cs
        OrderRepository.cs
        Order.cs
        OrderItem.cs
    /inventory
        AddInventoryItemUseCase.cs
        InventoryRepository.cs
        InventoryItem.cs
登入後複製

Here’s an example use case for creating an order:

public class CreateOrderUseCase
{
    private readonly IOrderRepository _orderRepository;
    private readonly IInventoryService _inventoryService;

    public CreateOrderUseCase(IOrderRepository orderRepository, IInventoryService inventoryService)
    {
        _orderRepository = orderRepository;
        _inventoryService = inventoryService;
    }

    public Order Execute(List<OrderItem> items)
    {
        // Ensure all items are available in inventory
        foreach (var item in items)
        {
            _inventoryService.CheckInventory(item.ProductName, item.Quantity);
        }

        var order = new Order(items);
        _orderRepository.Save(order);
        return order;
    }
}
登入後複製

In this example, the CreateOrderUseCase is part of the domain logic and interacts with the OrderRepository and InventoryService to fulfill the business need of creating an order.

Benefits of Screaming Architecture

Improved Readability: Anyone who opens your codebase will immediately understand what the system does.

Separation of Concerns: Business logic remains isolated from technical details, making it easier to change frameworks or technologies later.

Scalability: As the system grows, the domain structure remains consistent, allowing for easy addition of new features and modules.

Maintainability: Domain logic is easier to maintain when it’s cleanly separated from external dependencies and frameworks.

Framework Agnostic: Screaming Architecture allows the business logic to remain portable across different technical stacks, avoiding tight coupling with any particular framework.

Criticisms of Screaming Architecture

While Screaming Architecture has many benefits, it’s not without its criticisms:

Perceived Complexity: Developers unfamiliar with domain-driven design may find the separation of domain logic from technical details unnecessary or overly complex for small applications.
2

. Overhead: In small projects or simple CRUD applications, implementing Screaming Architecture may seem like overkill.

Learning Curve: For teams used to framework-first approaches, adopting Screaming Architecture requires a shift in thinking that may take time to internalize.

When to Use Screaming Architecture

Screaming Architecture is particularly useful in the following scenarios:

Domain-Driven Systems: Applications with complex business rules and domain logic.

Long-Term Projects: Systems expected to evolve over time, where scalability and maintainability are critical.

Cross-Platform Development: Systems that may switch frameworks or platforms, making a clean separation of business logic essential.

Conclusion

Screaming Architecture is more than just a catchy name; it’s a philosophy that advocates for making the core business logic the most prominent part of your codebase. By focusing on domain concepts rather than technical frameworks, developers can build systems that are more readable, maintainable, and scalable in the long run. Whether you’re working on a simple web application or a complex enterprise system, adopting Screaming Architecture can lead to cleaner, more focused code that clearly expresses its purpose.

To learn more about Screaming Architecture, you can check out some references and additional readings:

Clean Architecture by Robert C. Martin

Domain-Driven Design by Eric Evans

Uncle Bob's Blog on Clean Code

By embracing the principles of Screaming Architecture, you can create codebases that not only work but also "scream" their intent.

以上是什麼是尖叫建築?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!