dependency-injection

Explain Dependency Injection patterns and best practices for .NET applications.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/AlexanderRadevich/SportowyHub_clientApp --skill dependency-injection-alexanderradevich
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dependency-injection
Source: https://github.com/AlexanderRadevich/SportowyHub_clientApp/tree/main/.claude/skills/dependency-injection
Command: npx skills add https://github.com/AlexanderRadevich/SportowyHub_clientApp --skill dependency-injection-alexanderradevich

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers understand and implement effective Dependency Injection (DI) patterns in .NET, addressing common pitfalls and promoting robust, maintainable code.

Core Features & Use Cases

  • Service Lifetimes: Explains Singleton, Scoped, and Transient lifetimes with clear examples.
  • Keyed Services: Demonstrates .NET 8+ keyed services for strategy patterns.
  • Decorator Pattern: Shows how to add cross-cutting concerns like logging.
  • Convention Registration: Utilizes Scrutor for efficient assembly scanning.
  • Anti-patterns: Highlights and provides solutions for common DI mistakes like service locator usage and capturing scoped services in singletons.
  • Use Case: Resolve issues with DbContext lifetimes in a web API, implement a strategy pattern for different payment gateways, or refactor code to use constructor injection instead of service locator.

Quick Start

Use the dependency-injection skill to understand how to register a scoped service with the name 'IOrderService' and its implementation 'OrderService'.

Frequently Asked Questions about dependency-injection

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

FAQPage Schema
What is the difference between Singleton, Scoped, and Transient service lifetimes in .NET dependency injection?

Singleton persists for the application lifetime, Scoped per request, and Transient creates a new instance each time. This Skill explains each lifetime with clear examples.

How do I use Scrutor for convention-based assembly scanning in .NET?

You can use Scrutor for convention-based registration to automatically scan assemblies and register services by interface naming conventions. This Skill demonstrates how to utilize Scrutor to simplify your .NET dependency injection setup.

How do I implement the strategy pattern using keyed services in .NET 8?

Keyed services in .NET 8 allow you to resolve dependencies by a specific key, enabling straightforward strategy pattern implementations. This Skill demonstrates registering and resolving keyed services for scenarios like different payment gateways.

Why does capturing a scoped service in a singleton cause issues in .NET?

Capturing a scoped service in a singleton causes captive dependency issues, where the shorter-lived service is forced to live longer than intended. This Skill highlights this anti-pattern and provides solutions to avoid it.

Do I need to know C# to use the .NET dependency injection container effectively?

Yes, understanding C# and the .NET Core DI container is required. This Skill addresses advanced patterns like decorators and options rather than basic language syntax.

How do I add cross-cutting concerns like logging using the decorator pattern in .NET?

You can add cross-cutting concerns like logging by wrapping services with the decorator pattern in .NET. This Skill shows how to implement decorators to extend service behavior without modifying the original implementation.