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
| Criterion | Clean Architecture | Vertical Slices | N-tier |
|---|---|---|---|
| Team size | 5+ (many devs on same codebase) | 2-5 (feature teams) | 1-3 (simple apps) |
| Domain complexity | High (many business rules) | Medium (CRUD-heavy) | Low (simple CRUD) |
| Change frequency | Core domain changes rarely | Features change independently | Everything changes together |
| Testing strategy | Unit test domain, integration infra | Feature-level integration tests | End-to-end, horizontal |
| Learning curve | Steep (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
| Aspect | Full CQRS (separate models) | Simple CQRS (same model, separate methods) |
|---|---|---|
| Read/write models | Separate DbContext, tables | Same DbContext, different queries |
| Complexity | High (eventual consistency) | Low (immediate consistency) |
| Performance | Optimized for each workload | Compromise |
| When to use | High read/write asymmetry | Simple 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
阅读完整来源 ↗