跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
开发与测试

dotnet-architecture

Use this skill when structuring .NET applications — clean architecture, vertical slices, CQRS, MediatR, dependency injection, and project organization. This skill enforces: solution structure conventions, dependency flow direction, DI registration patterns, and architecture testing. Requires .NET SDK (dotnet new). Do NOT use for: Go, Node.js, Java, or non-.NET stacks.

j4flmao · 已有静态检查记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add j4flmao/agent-skills --skill dotnet-architecture

技能说明


name: dotnet-architecture description: > Use this skill when structuring .NET applications — clean architecture, vertical slices, CQRS, MediatR, dependency injection, and project organization. This skill enforces: solution structure conventions, dependency flow direction, DI registration patterns, and architecture testing. Requires .NET SDK (dotnet new). Do NOT use for: Go, Node.js, Java, or non-.NET stacks. version: "1.0.0" author: "j4flmao" license: "MIT" compatibility: claude-code: true cursor: true codex: true windsurf: true tags: [backend, dotnet, phase-2]

.NET Architecture

Purpose

Structure .NET applications with clean architecture, vertical slices, or CQRS — dependency inversion, MediatR pipelines, DI registration, and solution organization.

Agent Protocol

Trigger

User request includes: dotnet clean architecture, dotnet vertical slice, dotnet CQRS, dotnet solution structure, .NET project layout, dotnet DI, dotnet MediatR.

Input Context

  • .NET version (6+, 8+, 9+)
  • Architecture (Clean Architecture, Vertical Slices, N-tier)
  • Database (EF Core, Dapper, ADO.NET)
  • Patterns (CQRS, Event Sourcing, Repository)

Output Artifact

Solution structure, project references, DI registration, MediatR pipeline, EF Core setup.

Response Format

Produce artifact directly. No preamble, no postamble, no explanations. No filler, no hedging, no transitions.

Completion Criteria

  • Solution split by concern: Domain, Application, Infrastructure, Presentation
  • Domain has zero external dependencies
  • Application depends only on Domain
  • Infrastructure implements Application/Domain interfaces
  • DI registered in Composition Root (Presentation layer)
  • Architecture tests enforce dependency rules

Max Response Length

4096 tokens

Architecture Decision Trees

Clean Architecture vs Vertical Slices vs N-tier

CriterionClean ArchitectureVertical SlicesN-tier
Team size5+ (many devs on same codebase)2-5 (feature teams)1-3 (simple apps)
Domain complexityHigh (many business rules)Medium (CRUD-heavy)Low (simple CRUD)
Change frequencyCore domain changes rarelyFeatures change independentlyEverything changes together
Testing strategyUnit test domain, integration infraFeature-level integration testsEnd-to-end, horizontal
Learning curveSteep (many abstractions)Moderate (vertical boundaries)Low (traditional layers)

Decision: Complex domain with many rules → Clean Architecture. CRUD-heavy medium app → Vertical Slices. Simple API with few rules → N-tier.

CQRS: Full vs Simple

AspectFull CQRS (separate models)Simple CQRS (same model, separate methods)
Read/write modelsSeparate DbContext, tablesSame DbContext, different queries
ComplexityHigh (eventual consistency)Low (immediate consistency)
PerformanceOptimized for each workloadCompromise
When to useHigh read/write asymmetrySimple CRUD with queries

Decision: Reports/analytics workload separate from transactional → Full CQRS. Simple list/detail views → Simple CQRS.

Workflow

Step 1: Solution Structure (Clean Architecture)

src/
  Domain/
    Entities/
    ValueObjects/
    Aggregates/
    DomainEvents/
    Exceptions/
    Interfaces/
  Application/
    Common/
      Interfaces/
      Behaviors/        // MediatR pipelines
      Mappings/
    Features/
      Users/
        Commands/
        Queries/
        DTOs/
        Validators/
    DependencyInjection.cs
  Infrastructure/
    Persistence/
      Context/
      Configurations/
      Repositories/
    Services/
    DependencyInjection.cs
  Presentation/
    Controllers/
    Middleware/
    Program.cs
tests/
  Domain.Tests/
  Application.Tests/
  Integration.Tests/
  Architecture.Tests/

Step 2: Domain Layer (Zero Dependencies)

// Domain/Entities/Order.cs
public class Order : IAggregateRoot
{
    public Guid Id { get; private set; }
    public string OrderNumber { get; private set; }
    public OrderStatus Status { get; private set; }
    private readonly List<OrderItem> _items = new();
    public IReadOnlyCollection<OrderItem> Items => _items.AsReadOnly();

    private Order() { } // EF Core

    public Order(string orderNumber)
    {
        Id = Guid.NewGuid();
        OrderNumber = orderNumber;
        Status = OrderStatus.Pending;
        AddDomainEvent(new OrderCreatedDomainEvent(Id));
    }

    public void AddItem(string product, decimal price, int quantity)
    {
        if (Status != OrderStatus.Pending)
            throw new DomainException("Cannot modify confirmed order");
        _items.Add(new OrderItem(product, price, quantity));
    }

    public void Confirm()
    {
        if (_items.Count == 0)
            throw new DomainException("Cannot confirm empty order");
        Status = OrderStatus.Confirmed;
        AddDomainEvent(new OrderConfirmedDomainEvent(Id));
    }
}

// Domain/ValueObjects/Money.cs
public record Money
{
    public decimal Amount { get; init; }
    public string Currency { get; init; }

    public Money(decimal amount, string currency)
    {
        if (amount < 0) throw new DomainException("Amount cannot be negative");
        if (string.IsNullOrWhiteSpace(currency)) throw new DomainException("Currency required");
        Amount = amount;
        Currency = currency.ToUpperInvariant();
    }
}

Step 3: Application Layer (MediatR + CQRS)

// Application/Features/Orders/Commands/CreateOrder/CreateOrderCommand.cs
public record CreateOrderCommand(string OrderNumber, List<OrderItemDto> Items) : IRequest<OrderDto>;

// Application/Features/Orders/Commands/CreateOrder/CreateOrderCommandHandler.cs
public class CreateOrderCommandHandler : IRequestHandler<CreateOrderCommand, OrderDto>
{
    private readonl
阅读完整来源 ↗