dotnet:di-patterns

Review .NET dependency injection patterns, lifetimes, and anti-patterns.

2|Updated Apr 19, 2026
One-click install
npx skills add https://github.com/dimsour/dotnet-ai-toolkit --skill dotnet-di-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet:di-patterns
Source: https://github.com/dimsour/dotnet-ai-toolkit/tree/main/plugins/dotnet/skills/di-patterns
Command: npx skills add https://github.com/dimsour/dotnet-ai-toolkit --skill dotnet-di-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps developers understand and implement best practices for dependency injection in .NET applications, ensuring better maintainability and performance.

Core Features & Use Cases

  • Dependency Injection Patterns: Learn about various patterns such as Singleton, Scoped, Transient, and their appropriate use cases.
  • Iron Laws: Follow established guidelines like using Scoped for DbContext and leveraging IHttpClientFactory for HttpClient.
  • Quick Reference: Access quick references for different lifetimes and understanding captive dependencies.
  • Anti-patterns: Identify and avoid common pitfalls such as Singleton capturing Scoped instances and using IServiceProvider as a service locator.

Quick Start

Use the dotnet:di-patterns skill to review the best practices for dependency injection in your application.

Frequently Asked Questions about dotnet:di-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What is the correct dependency injection lifetime for DbContext in .NET?

The correct dependency injection lifetime for DbContext in .NET is Scoped. Following this lifetime management practice ensures proper context handling within a request scope, preventing concurrency issues and maintaining code maintainability.

How do I use IHttpClientFactory to manage HttpClient dependencies?

You manage HttpClient dependencies by leveraging IHttpClientFactory to avoid socket exhaustion. This .NET dependency injection pattern ensures proper lifetime management and prevents DNS changes issues, improving application performance.

What is a captive dependency in .NET dependency injection?

A captive dependency in .NET dependency injection occurs when a Singleton service captures a Scoped instance. This anti-pattern forces the Scoped dependency to live as long as the Singleton, breaking lifetime management and causing concurrency bugs.

Why is using IServiceProvider as a service locator considered an anti-pattern?

Using IServiceProvider as a service locator is an anti-pattern because it hides class dependencies, making code maintainability harder and testing difficult. Explicit dependency injection patterns are preferred for clear dependency resolution.

When should I use Transient vs Singleton dependency lifetimes in .NET?

Use Transient dependency lifetimes for lightweight, stateless services and Singleton for shared state or expensive initialization in .NET. Reviewing lifetime management best practices ensures optimal performance and code maintainability.