Skip to content
Back to skills

Infrastructure Layer Rules

ASecurity

Infrastructure layer rules — repository implementations, EF Core configuration and DbContext, mandatory migrations with zero-downtime patterns, DI registration, and external service clients. Apply whenever writing or editing code in an Infrastructure project.

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 11, 2026
developmentsql

Works with

  • cli

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add makigjuro/cloud-app-blueprint --skill infrastructure-layer-rules --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Infrastructure Layer Rules?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Infrastructure Layer Rules
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/makigjuro-infrastructure-layer-rules/badge)](https://www.skillsdirectory.com/skills/makigjuro-infrastructure-layer-rules)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: infrastructure-layer-rules
description: Infrastructure layer rules — repository implementations, EF Core configuration and DbContext, mandatory migrations with zero-downtime patterns, DI registration, and external service clients. Apply whenever writing or editing code in an Infrastructure project.
user-invocable: false
paths:
  - "src/**/Infrastructure/**/*.cs"
---

# Infrastructure Layer Rules

## Repository Implementation
- Implement both command and query repository interfaces from Domain
- A single class can implement both: `OrderRepository : IOrderRepository, IOrderQueryRepository`
- Use EF Core for writes, Dapper for read-optimized queries where needed
- Pagination returns `(IReadOnlyCollection<T> Items, int TotalCount)` tuples

## EF Core Configuration
- Entity configurations in `Persistence/Configurations/` folder using `IEntityTypeConfiguration<T>`
- Auto-apply via `modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly())`
- Each service has its own schema: `modelBuilder.HasDefaultSchema("order_service")`
- Ignore domain events: `modelBuilder.Ignore<DomainEvent>()`
- Value object conversions defined in entity configurations

## DbContext
- One DbContext per microservice, inherits from `DbContext`
- DbSets for aggregate roots only
- Protected parameterless constructor not needed (DI handles instantiation)

## EF Core Migrations
- Create a migration whenever you add or modify entities, configurations, or DbSets
- Run `dotnet ef migrations add {Name} --project {Infrastructure} --startup-project {Host}` immediately after model changes
- Never commit entity/configuration changes without the corresponding migration
- Review generated migrations before committing -- verify only expected changes are included
- Skipping migrations causes `PendingModelChangesWarning` runtime errors
- Migrations are applied by a one-shot job (`{Service}.Host --migrate`), never on normal web startup. That mode registers only the DbContext (`AddPersistence`), not the full `AddInfrastructure` -- otherwise DI validation in Development fails on services it cannot satisfy (e.g. Wolverine's `IMessageBus`)
- Wrap host startup (both modes) in try/catch that logs and returns exit code 1 (rethrow `HostAbortedException` for `dotnet ef`): as PID 1 in a container an unhandled startup exception hangs the process instead of exiting

## Zero-Downtime Migration Patterns
When modifying existing schemas in production, use the **expand/contract** pattern to avoid downtime:

**Adding a column:** Safe -- add as nullable or with a default value. Backfill data in a separate migration if needed.

**Renaming a column:** Unsafe as a single step. Instead:
1. **Expand:** Add new column, deploy code that writes to both old and new
2. **Migrate:** Backfill new column from old column
3. **Contract:** Remove reads from old column, then drop it in a later migration

**Dropping a column:** Unsafe -- remove all code references first, deploy, then drop the column in a subsequent migration.

**Adding a non-nullable column to existing table:** Add as nullable first with a default, backfill, then alter to non-nullable.

**Index changes:** Adding indexes is safe (may be slow on large tables -- consider `CREATE INDEX CONCURRENTLY` via raw SQL migration). Dropping unused indexes is safe.

For simple additive changes (new tables, new nullable columns, new indexes), a single migration is fine. Use expand/contract only when modifying or removing existing schema that production code depends on.

## DI Registration Pattern
- Expose a single `AddInfrastructure(this IServiceCollection services, IConfiguration configuration)` extension method; it calls a separate `AddPersistence(...)` that registers only the DbContext (reused by `--migrate`)
- Register DbContext with Npgsql: `services.AddDbContext<TContext>(options => options.UseNpgsql(...))`
- Register repositories as scoped
- Register external service clients (message broker, blob storage, cache)

## External Service Clients
- Message broker: wrapper implementing `IMessagePublisher`, JSON serialization with camelCase
- Blob storage: wrapper around blob client implementing `IBlobStorageClient`
- Cache: `IConnectionMultiplexer` or equivalent cache client
- All external calls have explicit timeouts

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.