di-patterns

Guide .NET Dependency Injection patterns including HttpClientFactory, Options, and keyed services.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

This Skill addresses common issues in .NET's Dependency Injection (DI) by providing guidance and best practices for implementing DI patterns.

Core Features & Use Cases

  • Dependency Injection Patterns: Offers explanations and examples of various DI patterns such as Singleton, Scoped, and Transient lifetimes.
  • HttpClientFactory: Guidance on using HttpClientFactory for HTTP requests.
  • Options Pattern: Detailed explanation of the Options pattern for configuration management.
  • Keyed Services: Information on using keyed services in .NET 8 and above.
  • Factories: Examples of implementing factories in .NET for creating services based on different criteria.
  • Multi-Registration: Techniques for registering multiple services with the same interface.
  • Use Case: Helps developers implement DI patterns correctly in their .NET applications, improving maintainability and scalability.

Quick Start

Review the recommended DI patterns for your .NET application in the 'di-patterns' skill.

Frequently Asked Questions about di-patterns

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

FAQPage Schema
What is the best way to manage .NET dependency injection lifetimes for Singleton, Scoped, and Transient services?

The Options pattern in .NET is a configuration management approach that binds strongly-typed class properties to configuration sources. It provides safe, validated access to app settings without manually parsing configuration files.

How do I use HttpClientFactory to avoid socket exhaustion in .NET dependency injection?

You use HttpClientFactory by registering it through .NET dependency injection to create and manage HttpClient instances. This centralizes HTTP client configuration and prevents socket exhaustion from improper client disposal.

How do I resolve multiple service implementations registered with the same interface in .NET dependency injection?

Yes, keyed services are supported in .NET 8 and above to enhance dependency injection. They allow you to register and resolve multiple implementations of the same interface by associating each service with a unique key.

When should I use factories instead of direct dependency injection in .NET?

You should use factories in .NET when service creation depends on runtime criteria or variable parameters. Factories defer instantiation until specific conditions are met, providing flexibility beyond standard .NET dependency injection patterns.