dotnet-dev
0
总安装量
1
周安装量
安装命令
npx skills add https://github.com/mounchons/agentmarketplace --skill dotnet-dev
Agent 安装分布
amp
1
cline
1
opencode
1
cursor
1
kimi-cli
1
codex
1
Skill 文档
.NET Core Development Expert Skill
à¸à¸¸à¸à¹à¸à¹à¸ .NET Core Development Expert à¸à¸µà¹à¹à¸à¸µà¹à¸¢à¸§à¸à¸²à¸à¹à¸à¸à¸²à¸£à¸à¸±à¸à¸à¸²à¸£à¸°à¸à¸ Enterprise-grade à¸à¹à¸§à¸¢ Microsoft Stack
ð¯ Core Principles (à¸à¸²à¸ User Preferences)
1. Domain-First Approach
- à¹à¸£à¸´à¹à¸¡à¸à¸²à¸ Domain Model à¹à¸ªà¸¡à¸ – à¸à¸´à¸à¸à¸²à¸ Business Requirements à¸à¹à¸à¸
- à¸à¸à¸à¹à¸à¸ Entities à¹à¸¥à¸° Relationships à¸à¹à¸à¸à¹à¸à¸µà¸¢à¸ Code
- à¹à¸à¹ Rich Domain Models à¹à¸à¸ Anemic Models
2. Architecture Preferences
- Clean Architecture à¹à¸à¹à¸à¸«à¸¥à¸±à¸
- Repository Pattern + Unit of Work สำหรัภData Access
- CQRS with MediatR สำหรัภComplex Applications
- Dependency Injection à¸à¸¸à¸à¸à¸µà¹
3. Technology Stack
- .NET 8+ (Latest LTS)
- Entity Framework Core (Code First)
- PostgreSQL หรืภSQL Server à¹à¸à¹à¸ Primary Database
- Redis สำหรัภCaching
- ASP.NET Core MVC / Web API / Minimal APIs
ðï¸ Database Provider Selection
à¹à¸¡à¸·à¹à¸à¹à¸«à¸£à¹à¹à¸à¹ PostgreSQL
- Open source, à¹à¸¡à¹à¸¡à¸µà¸à¹à¸² license
- à¸à¹à¸à¸à¸à¸²à¸£ JSONB columns
- Full-text search ภาษาà¹à¸à¸¢
- Array data types
- Linux/Container deployment
à¹à¸¡à¸·à¹à¸à¹à¸«à¸£à¹à¹à¸à¹ SQL Server
- Enterprise environment à¸à¸µà¹à¸¡à¸µ license à¸à¸¢à¸¹à¹à¹à¸¥à¹à¸§
- à¸à¹à¸à¸à¸à¸²à¸£ Temporal Tables (System-Versioned)
- Row-Level Security (RLS)
- Always Encrypted
- Integration à¸à¸±à¸ Azure services
- Legacy systems à¸à¸µà¹à¹à¸à¹ SQL Server à¸à¸¢à¸¹à¹
ð à¹à¸¡à¸·à¹à¸à¸à¹à¸à¸à¸à¸²à¸£à¸à¹à¸à¸¡à¸¹à¸¥à¸¥à¹à¸²à¸ªà¸¸à¸à¸à¸²à¸ Microsoft Learn
à¹à¸à¹ MCP Server microsoft-learn à¹à¸à¸·à¹à¸à¸à¹à¸à¸«à¸² documentation ลà¹à¸²à¸ªà¸¸à¸:
# à¸à¹à¸à¸«à¸² documentation
npx mcporter call --stdio "streamable-http https://learn.microsoft.com/api/mcp" \
search query:"Entity Framework Core SQL Server"
# หรืà¸à¹à¸à¹à¸à¹à¸²à¸ mcp tool à¹à¸à¸¢à¸à¸£à¸à¸à¹à¸² configure à¹à¸§à¹à¹à¸¥à¹à¸§
# mcp__microsoft-learn__search query:"ASP.NET Core authentication"
à¹à¸¡à¸·à¹à¸à¹à¸«à¸£à¹à¸à¸§à¸£à¹à¸à¹ Microsoft Learn MCP:
- à¸à¹à¸à¸à¸à¸²à¸£ syntax หรืภAPI ลà¹à¸²à¸ªà¸¸à¸
- à¹à¸¡à¹à¹à¸à¹à¹à¸à¹à¸à¸µà¹à¸¢à¸§à¸à¸±à¸ breaking changes à¹à¸ version à¹à¸«à¸¡à¹
- à¸à¹à¸à¸à¸à¸²à¸£ best practices à¸à¸²à¸ Microsoft
- à¸à¹à¸à¸«à¸² configuration options à¸à¸µà¹à¸à¸¹à¸à¸à¹à¸à¸
ðï¸ Project Structure (Clean Architecture)
Solution/
âââ src/
â âââ Domain/ # Core business logic
â â âââ Entities/
â â âââ ValueObjects/
â â âââ Enums/
â â âââ Events/
â â âââ Exceptions/
â â
â âââ Application/ # Use cases & business rules
â â âââ Common/
â â â âââ Interfaces/
â â â âââ Behaviors/
â â â âââ Mappings/
â â âââ Features/
â â â âââ [Feature]/
â â â âââ Commands/
â â â âââ Queries/
â â âââ DTOs/
â â
â âââ Infrastructure/ # External concerns
â â âââ Data/
â â â âââ Configurations/
â â â âââ Repositories/
â â â âââ Migrations/
â â â âââ ApplicationDbContext.cs
â â âââ Services/
â â âââ DependencyInjection.cs
â â
â âââ WebApi/ # Presentation layer
â âââ Controllers/
â âââ Middleware/
â âââ Filters/
â âââ Program.cs
â
âââ tests/
â âââ Domain.Tests/
â âââ Application.Tests/
â âââ Integration.Tests/
â
âââ [AppName].AppHost/ # .NET Aspire (optional)
âââ Program.cs
ð Code Patterns & Templates
1. Base Entity
public abstract class BaseEntity
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public string? CreatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
public bool IsDeleted { get; set; } = false;
}
public abstract class BaseEntity<TKey> : BaseEntity
{
public new TKey Id { get; set; } = default!;
}
2. Repository Interface
public interface IRepository<T> where T : BaseEntity
{
Task<T?> GetByIdAsync(long id, CancellationToken ct = default);
Task<IEnumerable<T>> GetAllAsync(CancellationToken ct = default);
Task<T> AddAsync(T entity, CancellationToken ct = default);
void Update(T entity);
void Delete(T entity);
Task<bool> ExistsAsync(long id, CancellationToken ct = default);
IQueryable<T> Query();
}
3. Unit of Work
public interface IUnitOfWork : IDisposable
{
// Repositories
IRepository<Customer> Customers { get; }
IRepository<Order> Orders { get; }
// Transaction management
Task<int> SaveChangesAsync(CancellationToken ct = default);
Task BeginTransactionAsync(CancellationToken ct = default);
Task CommitAsync(CancellationToken ct = default);
Task RollbackAsync(CancellationToken ct = default);
}
4. Generic Repository Implementation
public class Repository<T> : IRepository<T> where T : BaseEntity
{
protected readonly ApplicationDbContext _context;
protected readonly DbSet<T> _dbSet;
public Repository(ApplicationDbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public virtual async Task<T?> GetByIdAsync(long id, CancellationToken ct = default)
=> await _dbSet.FirstOrDefaultAsync(e => e.Id == id && !e.IsDeleted, ct);
public virtual async Task<IEnumerable<T>> GetAllAsync(CancellationToken ct = default)
=> await _dbSet.Where(e => !e.IsDeleted).ToListAsync(ct);
public virtual async Task<T> AddAsync(T entity, CancellationToken ct = default)
{
await _dbSet.AddAsync(entity, ct);
return entity;
}
public virtual void Update(T entity)
{
entity.UpdatedAt = DateTime.UtcNow;
_dbSet.Update(entity);
}
public virtual void Delete(T entity)
{
entity.IsDeleted = true;
entity.UpdatedAt = DateTime.UtcNow;
Update(entity);
}
public virtual async Task<bool> ExistsAsync(long id, CancellationToken ct = default)
=> await _dbSet.AnyAsync(e => e.Id == id && !e.IsDeleted, ct);
public virtual IQueryable<T> Query()
=> _dbSet.Where(e => !e.IsDeleted).AsQueryable();
}
5. DbContext – Multi-Database Support
public class ApplicationDbContext : DbContext
{
private readonly ICurrentUserService _currentUser;
public ApplicationDbContext(
DbContextOptions<ApplicationDbContext> options,
ICurrentUserService currentUser) : base(options)
{
_currentUser = currentUser;
}
// DbSets
public DbSet<Customer> Customers => Set<Customer>();
public DbSet<Order> Orders => Set<Order>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Apply all configurations from assembly
modelBuilder.ApplyConfigurationsFromAssembly(
typeof(ApplicationDbContext).Assembly);
// Global query filter for soft delete
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
{
modelBuilder.Entity(entityType.ClrType)
.HasQueryFilter(
GenerateSoftDeleteFilter(entityType.ClrType));
}
}
}
public override async Task<int> SaveChangesAsync(CancellationToken ct = default)
{
foreach (var entry in ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedAt = DateTime.UtcNow;
entry.Entity.CreatedBy = _currentUser.UserId;
break;
case EntityState.Modified:
entry.Entity.UpdatedAt = DateTime.UtcNow;
entry.Entity.UpdatedBy = _currentUser.UserId;
break;
}
}
return await base.SaveChangesAsync(ct);
}
private static LambdaExpression GenerateSoftDeleteFilter(Type type)
{
var parameter = Expression.Parameter(type, "e");
var property = Expression.Property(parameter, nameof(BaseEntity.IsDeleted));
var condition = Expression.Equal(property, Expression.Constant(false));
return Expression.Lambda(condition, parameter);
}
}
6. Dependency Injection – Database Provider
// Infrastructure/DependencyInjection.cs
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(
this IServiceCollection services,
IConfiguration configuration)
{
var dbProvider = configuration.GetValue<string>("DatabaseProvider") ?? "PostgreSQL";
services.AddDbContext<ApplicationDbContext>(options =>
{
switch (dbProvider)
{
case "SqlServer":
options.UseSqlServer(
configuration.GetConnectionString("DefaultConnection"),
sqlOptions =>
{
sqlOptions.MigrationsAssembly(
typeof(ApplicationDbContext).Assembly.FullName);
sqlOptions.EnableRetryOnFailure(
maxRetryCount: 3,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null);
});
break;
case "PostgreSQL":
default:
options.UseNpgsql(
configuration.GetConnectionString("DefaultConnection"),
npgsqlOptions =>
{
npgsqlOptions.MigrationsAssembly(
typeof(ApplicationDbContext).Assembly.FullName);
npgsqlOptions.EnableRetryOnFailure(
maxRetryCount: 3,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorCodesToAdd: null);
});
break;
}
});
// Repositories
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<IUnitOfWork, UnitOfWork>();
// Caching
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = configuration.GetConnectionString("Redis");
options.InstanceName = "App_";
});
return services;
}
}
7. appsettings.json – Database Configuration
{
"DatabaseProvider": "SqlServer",
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=MyApp;User Id=sa;Password=YourPassword;TrustServerCertificate=True;",
"Redis": "localhost:6379"
}
}
{
"DatabaseProvider": "PostgreSQL",
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=myapp;Username=postgres;Password=YourPassword;",
"Redis": "localhost:6379"
}
}
8. .NET Aspire AppHost – Multi-Database
// AppHost/Program.cs
var builder = DistributedApplication.CreateBuilder(args);
// Choose database provider
var usePostgres = builder.Configuration.GetValue<bool>("UsePostgres", true);
IResourceBuilder<IResourceWithConnectionString> database;
if (usePostgres)
{
var postgres = builder.AddPostgres("postgres")
.WithPgAdmin()
.AddDatabase("appdb");
database = postgres;
}
else
{
var sqlserver = builder.AddSqlServer("sqlserver")
.AddDatabase("appdb");
database = sqlserver;
}
var redis = builder.AddRedis("redis")
.WithRedisCommander();
// API Project
var api = builder.AddProject<Projects.WebApi>("api")
.WithReference(database)
.WithReference(redis)
.WithExternalHttpEndpoints();
builder.Build().Run();
ð§ Common Tasks
Migration Commands
# Add migration
dotnet ef migrations add InitialCreate -p Infrastructure -s WebApi
# Update database
dotnet ef database update -p Infrastructure -s WebApi
# Generate SQL script
dotnet ef migrations script -p Infrastructure -s WebApi -o ./migrations.sql
# Remove last migration
dotnet ef migrations remove -p Infrastructure -s WebApi
NuGet Packages
Common Packages
<!-- Domain/Application -->
<PackageReference Include="MediatR" Version="12.*" />
<PackageReference Include="FluentValidation" Version="11.*" />
<PackageReference Include="AutoMapper" Version="13.*" />
<!-- Infrastructure - Core -->
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.*" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.*" />
<!-- WebApi -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.*" />
<PackageReference Include="Serilog.AspNetCore" Version="8.*" />
PostgreSQL Packages
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.*" />
<!-- Aspire -->
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.*" />
SQL Server Packages
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.*" />
<!-- Aspire -->
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="8.*" />
ð Reference Files
à¸à¸¹à¹à¸à¸¥à¹à¹à¸à¸´à¹à¸¡à¹à¸à¸´à¸¡à¹à¸:
references/ef-core-patterns.md– EF Core advanced patterns (PostgreSQL + SQL Server)references/aspire-setup.md– .NET Aspire configurationreferences/testing-patterns.md– Testing strategiesreferences/microsoft-learn-mcp.md– MCP usage guide
â ï¸ Best Practices
- Always use async/await – à¹à¸¡à¹ block threads
- Use CancellationToken – à¸à¸¸à¸ async method
- Validate inputs – FluentValidation à¸à¹à¸à¸ process
- Log appropriately – Structured logging with Serilog
- Handle exceptions – Global exception handler + Result pattern
- Write tests – Unit tests for business logic, Integration tests for APIs
- Use DTOs – à¹à¸¡à¹ expose Entities à¸à¸£à¸à¹
- Soft delete – à¹à¸à¹ IsDeleted flag à¹à¸à¸ hard delete
- Audit trail – CreatedAt, UpdatedAt, CreatedBy, UpdatedBy
- Use transactions – สำหรัภoperations à¸à¸µà¹à¸à¹à¸à¸ atomic
- Enable retry on failure – สำหรัภdatabase connections
- Use connection resiliency – à¸à¸±à¹à¸ PostgreSQL à¹à¸¥à¸° SQL Server