development-workflow

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

.NET Development Workflow

.NET开发工作流

Workflow Overview

工作流概述

┌─────────────────────────────────────────────────────────────────┐
│                    DEVELOPMENT WORKFLOW                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌──────────────┐                                               │
│  │  Understand  │  Read requirements, explore codebase          │
│  │    Task      │                                               │
│  └──────────────┘                                               │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐                                               │
│  │  Implement   │  Write code following patterns                │
│  │   Changes    │                                               │
│  └──────────────┘                                               │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐     ┌──────────────┐                         │
│  │   Validate   │────▶│   Report     │                         │
│  │ Build/Test/  │     │   Results    │                         │
│  │   Analyze    │     │              │                         │
│  └──────────────┘     └──────────────┘                         │
│         │                    │                                  │
│         ▼                    ▼                                  │
│    ┌─────────┐         ┌──────────┐                            │
│    │  PASS?  │───NO───▶│   Fix    │                            │
│    └─────────┘         │  Issues  │                            │
│         │              └──────────┘                            │
│         │                    │                                  │
│        YES                   │                                  │
│         │                    │                                  │
│         ▼                    │                                  │
│  ┌──────────────┐           │                                  │
│  │   Ready to   │◀──────────┘                                  │
│  │    Commit    │         (re-validate)                        │
│  └──────────────┘                                              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│                    DEVELOPMENT WORKFLOW                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌──────────────┐                                               │
│  │  Understand  │  Read requirements, explore codebase          │
│  │    Task      │                                               │
│  └──────────────┘                                               │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐                                               │
│  │  Implement   │  Write code following patterns                │
│  │   Changes    │                                               │
│  └──────────────┘                                               │
│         │                                                       │
│         ▼                                                       │
│  ┌──────────────┐     ┌──────────────┐                         │
│  │   Validate   │────▶│   Report     │                         │
│  │ Build/Test/  │     │   Results    │                         │
│  │   Analyze    │     │              │                         │
│  └──────────────┘     └──────────────┘                         │
│         │                    │                                  │
│         ▼                    ▼                                  │
│    ┌─────────┐         ┌──────────┐                            │
│    │  PASS?  │───NO───▶│   Fix    │                            │
│    └─────────┘         │  Issues  │                            │
│         │              └──────────┘                            │
│         │                    │                                  │
│        YES                   │                                  │
│         │                    │                                  │
│         ▼                    │                                  │
│  ┌──────────────┐           │                                  │
│  │   Ready to   │◀──────────┘                                  │
│  │    Commit    │         (re-validate)                        │
│  └──────────────┘                                              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Phase 1: Understand the Task

阶段1:理解任务

Feature Implementation

功能实现

  1. Read the feature requirements/user story
  2. Identify affected components
  3. Check existing patterns in codebase
  4. Plan the implementation approach
  1. 阅读功能需求/用户故事
  2. 确定受影响的组件
  3. 查看代码库中的现有模式
  4. 规划实现方案

Bug Fix

Bug修复

  1. Reproduce the bug
  2. Identify root cause
  3. Find related code
  4. Plan the fix
  1. 复现Bug
  2. 确定根本原因
  3. 找到相关代码
  4. 规划修复方案

Refactoring

代码重构

  1. Understand current implementation
  2. Identify what needs to change
  3. Ensure test coverage exists
  4. Plan incremental changes
  1. 理解当前实现
  2. 确定需要修改的内容
  3. 确保存在测试覆盖
  4. 规划增量式修改

Phase 2: Implement Changes

阶段2:实施修改

Follow Existing Patterns

遵循现有模式

csharp
// Find existing patterns
// Look for similar implementations in the codebase
// Follow established conventions

// Example: If services follow this pattern
public class ExistingService : IExistingService
{
    private readonly IRepository _repository;
    private readonly ILogger<ExistingService> _logger;

    public ExistingService(IRepository repository, ILogger<ExistingService> logger)
    {
        _repository = repository;
        _logger = logger;
    }
}

// New service should follow same pattern
public class NewService : INewService
{
    private readonly IRepository _repository;
    private readonly ILogger<NewService> _logger;

    public NewService(IRepository repository, ILogger<NewService> logger)
    {
        _repository = repository;
        _logger = logger;
    }
}
csharp
// Find existing patterns
// Look for similar implementations in the codebase
// Follow established conventions

// Example: If services follow this pattern
public class ExistingService : IExistingService
{
    private readonly IRepository _repository;
    private readonly ILogger<ExistingService> _logger;

    public ExistingService(IRepository repository, ILogger<ExistingService> logger)
    {
        _repository = repository;
        _logger = logger;
    }
}

// New service should follow same pattern
public class NewService : INewService
{
    private readonly IRepository _repository;
    private readonly ILogger<NewService> _logger;

    public NewService(IRepository repository, ILogger<NewService> logger)
    {
        _repository = repository;
        _logger = logger;
    }
}

Make Small, Incremental Changes

进行小步增量式修改

  1. One logical change at a time
  2. Build after each change to catch errors early
  3. Run relevant tests frequently
  4. Keep commits focused
  1. 每次只做一个逻辑相关的修改
  2. 每次修改后构建,尽早发现错误
  3. 频繁运行相关测试
  4. 保持提交内容聚焦

Phase 3: Validate Changes

阶段3:验证修改

Validation Steps

验证步骤

bash
undefined
bash
undefined

1. Build (catch compilation errors)

1. Build (catch compilation errors)

dotnet build --no-incremental
dotnet build --no-incremental

2. Run tests (verify behavior)

2. Run tests (verify behavior)

dotnet test --no-build
dotnet test --no-build

3. Static analysis (code quality)

3. Static analysis (code quality)

dotnet build /p:TreatWarningsAsErrors=true dotnet format --verify-no-changes
undefined
dotnet build /p:TreatWarningsAsErrors=true dotnet format --verify-no-changes
undefined

Quality Gates

质量门禁

GateRequirementBlocking
Build0 errorsYes
Tests100% passYes
Critical Warnings0No
All Warnings< 10No
门禁要求是否阻塞
构建0个错误
测试100%通过
严重警告0个
所有警告<10个

Phase 4: Fix Issues

阶段4:修复问题

Build Errors

构建错误

  1. Read error message carefully
  2. Go to the file and line indicated
  3. Fix the issue
  4. Rebuild to verify
  1. 仔细阅读错误信息
  2. 定位到指定的文件和行
  3. 修复问题
  4. 重新构建验证

Test Failures

测试失败

  1. Read the assertion failure
  2. Check expected vs actual
  3. Determine if test or code is wrong
  4. Fix and re-run test
  1. 阅读断言失败信息
  2. 对比预期结果与实际结果
  3. 判断是测试还是代码存在问题
  4. 修复后重新运行测试

Analysis Warnings

分析警告

  1. Review each warning
  2. Apply fix or suppress with justification
  3. Use
    dotnet format
    for auto-fixable issues
  1. 逐一检查每个警告
  2. 修复或合理地抑制警告
  3. 使用
    dotnet format
    修复可自动修正的问题

Validation Before Commit

提交前的验证

Checklist

检查清单

  • dotnet build
    succeeds with no errors
  • dotnet test
    passes all tests
  • No new critical analyzer warnings
  • Code follows existing patterns
  • Changes are focused on the task
  • dotnet build
    无错误执行成功
  • dotnet test
    所有测试通过
  • 无新增的严重分析器警告
  • 代码遵循现有模式
  • 修改内容围绕任务聚焦

Commands

命令

bash
undefined
bash
undefined

Full validation

Full validation

dotnet build --no-incremental &&
dotnet test --no-build &&
dotnet format --verify-no-changes
undefined
dotnet build --no-incremental &&
dotnet test --no-build &&
dotnet format --verify-no-changes
undefined

Best Practices

最佳实践

Code Organization

代码组织

csharp
// Group related code
// 1. Fields
private readonly IService _service;

// 2. Constructors
public MyClass(IService service) => _service = service;

// 3. Public methods
public void Execute() { }

// 4. Private methods
private void Helper() { }
csharp
// Group related code
// 1. Fields
private readonly IService _service;

// 2. Constructors
public MyClass(IService service) => _service = service;

// 3. Public methods
public void Execute() { }

// 4. Private methods
private void Helper() { }

Error Handling

错误处理

csharp
// Be specific with exceptions
public User GetUser(int id)
{
    var user = _repository.Find(id);
    if (user == null)
        throw new EntityNotFoundException($"User {id} not found");
    return user;
}

// Use guard clauses
public void Process(Request request)
{
    ArgumentNullException.ThrowIfNull(request);
    ArgumentException.ThrowIfNullOrEmpty(request.Name);

    // Main logic
}
csharp
// Be specific with exceptions
public User GetUser(int id)
{
    var user = _repository.Find(id);
    if (user == null)
        throw new EntityNotFoundException($"User {id} not found");
    return user;
}

// Use guard clauses
public void Process(Request request)
{
    ArgumentNullException.ThrowIfNull(request);
    ArgumentException.ThrowIfNullOrEmpty(request.Name);

    // Main logic
}

Async/Await

Async/Await

csharp
// Always use async suffix
public async Task<User> GetUserAsync(int id)
{
    return await _repository.FindAsync(id);
}

// Don't block on async
// BAD
var user = GetUserAsync(id).Result;

// GOOD
var user = await GetUserAsync(id);
csharp
// Always use async suffix
public async Task<User> GetUserAsync(int id)
{
    return await _repository.FindAsync(id);
}

// Don't block on async
// BAD
var user = GetUserAsync(id).Result;

// GOOD
var user = await GetUserAsync(id);

Dependency Injection

依赖注入

csharp
// Register services
services.AddScoped<IUserService, UserService>();
services.AddSingleton<ICacheService, MemoryCacheService>();
services.AddTransient<IEmailSender, SmtpEmailSender>();

// Inject via constructor
public class UserController
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }
}
csharp
// Register services
services.AddScoped<IUserService, UserService>();
services.AddSingleton<ICacheService, MemoryCacheService>();
services.AddTransient<IEmailSender, SmtpEmailSender>();

// Inject via constructor
public class UserController
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }
}

Common Patterns

常见模式

See patterns.md for detailed implementation patterns.
详见patterns.md获取详细的实现模式。