DDD 驱动的 WPF 开发协议

作者:萤灬虫 发布时间: 2026-04-18 阅读量:11 评论数:0

作为一名 C# 开发者,我们经常被“代码腐化”所困扰。为了在 WPF 项目中贯彻 DDD(领域驱动设计)整洁架构(Clean Architecture),我最近探索出了一种新的工作流——将架构规范转化为 AI 协议(AI Protocol)

今天,我将分享我是如何通过 .cursorrules 文件,让 Cursor 或 Windsurf 成为我项目中严格的“架构守门员”。


1. 痛点:为什么架构总是“一写就乱”?

在多人协作或快速迭代中,业务逻辑很容易因为缺乏约束而“越位”。例如:

  • ViewModel 里直接写了 SQL。
  • 硬件控制 SDK 被混杂在业务代码中。
  • 依赖关系混乱,导致单元测试寸步难行。

2. 什么是 AI 架构协议 (.cursorrules)?

如果你在使用 Cursor 或 Windsurf,你应该听说过 .cursorrules。它不仅仅是一个提示词,它是你项目的**“工程宪法”**。通过这份协议,我们告诉 AI:在编写代码前,必须先评估是否符合架构规范。

3. 我的 .cursorrules 配置 (中英双语版)

为了确保 AI 既能准确理解专业约束(英文),又能保持良好的逻辑理解(中文),我设计了一套“英文指令 + 中文解释”的协议,直接放在项目根目录即可生效。

4. 下载

直接复制下面内容粘贴在.cursorrules即可。

# WPF Architecture & Development Rules

## 1\. Architecture Constraints (DDD & Clean Architecture)

-   **Layer Segregation**:
    
    -   `Domain`: No dependencies. Contains Entities, Value Objects, and Domain Interfaces (e.g., `IRepository`).
    -   `Application`: Depends only on `Domain`. Orchestrates business logic, DTOs, and Service interfaces.
    -   `Infrastructure`: Depends on `Domain`. Implements persistence, external SDKs (like Spectrometer APIs), and logging.
    -   `UI (WPF)`: Depends on `Application` and `Domain`. Only ViewModels and Views.
    -   **中文解释**: 严格分层。领域层(Domain)保持纯净,应用层(Application)负责协调,基础设施层(Infrastructure)负责具体实现,UI层只处理界面逻辑。
-   **Dependency Inversion**: High-level modules must NEVER depend on low-level modules. Use interfaces.
    
    -   **中文解释**: 依赖倒置。高层模块绝对禁止直接引用底层实现,必须通过接口交互。
-   **IoC Enforcement**: All services must be registered via `Microsoft.Extensions.DependencyInjection`. No `new` keywords for services in ViewModels/Services.
    
    -   **中文解释**: 强制使用 IoC 容器,禁止在 ViewModel 或 Service 中直接 `new` 对象。

## 2\. WPF Specifics

-   **MVVM Pattern**: Code-behind should be empty. Logic must reside in ViewModels. Use `CommunityToolkit.Mvvm`.
    
    -   **中文解释**: 严格 MVVM。Code-behind 保持干净,业务逻辑必须在 ViewModel 中处理。
-   **Data Binding & Async**: Avoid hardcoded logic in XAML. Use `async/await` for all I/O operations (DB, Hardware SDK, File system).
    
    -   **中文解释**: 绑定与异步。XAML 避免逻辑硬编码,I/O 操作必须异步化以保持界面响应。

## 3\. Coding Standards

-   **Naming**: Interfaces start with 'I', ViewModels end with 'ViewModel', Services end with 'Service'.
    
    -   **中文解释**: 命名规范。接口以 'I' 开头,ViewModel 以 'ViewModel' 结尾,服务以 'Service' 结尾。
-   **Error Handling**: Use custom Domain Exceptions. Do not leak Infrastructure-level exceptions (e.g., `SqlException`) into the Application layer.
    
    -   **中文解释**: 异常处理。使用自定义业务异常,严禁将数据库或底层的异常直接透传给应用层。

## 4\. Module Encapsulation

-   **Local IoC**: Complex modules (e.g., Identity, DeviceControl) should expose an `AddModuleServices(this IServiceCollection services)` extension method.
    -   **中文解释**: 模块化封装。复杂功能模块应提供扩展方法来注册自身服务,保持启动项整洁。

## 5\. Instructions for Cursor AI

-   Always ask: "Does this violate the layer separation?" before generating code.
-   Prioritize creating an Interface before implementing a Service.
-   Ensure all business logic validation is in the Domain layer.
-   Default to Constructor Injection for all dependencies.
    -   **中文解释**: 给 AI 的执行指令。生成代码前需自检是否破坏分层,优先定义接口,业务验证归属领域层,默认使用构造函数注入。

评论