dependency-injection

Dependency injection patterns for .NET 10. Covers service lifetimes, keyed services, the decorator pattern, factory pattern, and common DI pitfalls. Load this skill when registering services, resolving lifetime issues, designing service composition, or when the user mentions "DI", "dependency inject

By codewithmukesh · 1,189 installs

npx skills add codewithmukesh/dotnet-claude-kit --skill dependency-injection

Source repository · Upstream listing

Dependency Injection Core Principles 1. Constructor injection is the default — Inject dependencies through the constructor (primary constructors make this clean). No service locator, no property injection. 2. Match lifetimes carefully — A singleton must never depend on a scoped or transient service. This is the most common DI bug. 3. Register interfaces, resolve interfaces — Register services.AddScoped<IOrderService, OrderService () , not the concrete type. 4. Keyed services for strategy pattern — .NET 8+ keyed services replace manual factory patterns for selecting between implementations. Patterns Keyed Services (.NET 8+) Use keyed services to register and resolve multiple implementations of the same interface. Decorator Pattern Registration by Convention (Scrutor) Factory Pattern When you need runtime logic to select an implementation. Options Registration Anti patterns Don't Capture Scoped Services in Singletons Don't Register Everything as Singleton Decision Guide Scenario Recommendation Stateless service Scoped (default) or Transient Configuration / cache Singleton DbContext Scoped (registered by AddDbContext ) Multiple implementations Keyed services (strategy pattern) Cross cutting behavior Decorator pattern Convention based registration Scrutor Runtime implementation selection Factory delegate Audit existing registrations get di registrations MCP tool — lifetimes, duplicates, captive dependency risks in one call Strongly typed config AddOptions<T ().BindConfiguration()