dependency-injection

Guide .NET dependency injection configuration for lifetimes, registrations, and keyed services.

Updated Apr 27, 2026
One-click install
npx skills add https://github.com/shahdanish/vibepos --skill dependency-injection-shahdanish
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dependency-injection
Source: https://github.com/shahdanish/vibepos/tree/main/skills/dependency-injection
Command: npx skills add https://github.com/shahdanish/vibepos --skill dependency-injection-shahdanish

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Dependency injection (DI) helps you structure .NET apps cleanly, but it’s easy to introduce subtle lifetime bugs (like singletons capturing scoped services) and to overcomplicate service registration and resolution.

Core Features & Use Cases

  • Correct lifetime and scope management: Prevent singleton/scoped mismatches and resolve lifetime-related failures reliably.
  • Clean registration and resolution: Register interfaces and resolve interfaces to keep implementations swappable and testable.
  • Advanced DI patterns: Use keyed services for strategy selection, decorator patterns for cross-cutting concerns, Scrutor convention-based registration, and factory delegates for runtime provider selection.

Quick Start

Load this skill when you are registering services and resolving lifetime issues in a .NET app, then apply constructor injection, select appropriate lifetimes, and use keyed services or decorators to structure implementation selection and cross-cutting behavior.

Frequently Asked Questions about dependency-injection

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

FAQPage Schema
Why does my .NET dependency injection fail when a singleton captures a scoped service?

Dependency injection fails because singletons live longer than scoped services, creating a captive dependency bug. You must prevent singleton/scoped mismatches by selecting appropriate service lifetimes and ensuring safe singleton behavior with scoped dependencies.

What is the difference between AddScoped, AddTransient, and AddSingleton in .NET dependency injection?

In dependency injection, AddScoped creates one instance per request, AddTransient creates a new instance every time, and AddSingleton creates a single shared instance. Accurate lifetime selection prevents common service registration errors and improves maintainability.

How do I use keyed services for strategy selection in .NET dependency injection?

Keyed services in dependency injection allow strategy-style resolution by registering services with distinct keys. You resolve the desired implementation at runtime by specifying the key, enabling clean composition and flexible service wiring without complex factory delegates.

How do I apply the decorator pattern for cross-cutting concerns using .NET dependency injection?

You apply the decorator pattern in dependency injection by wrapping an existing service registration to handle cross-cutting concerns. This keeps implementations swappable and testable while structuring cross-cutting behavior cleanly through composition.

Can I use Scrutor for convention-based service registration in .NET?

Yes, Scrutor enables convention-based dependency injection wiring in .NET applications. It automatically registers interfaces and implementations by convention, keeping service registration clean and reducing manual configuration errors.

When should I use factory delegates instead of direct constructor injection in .NET dependency injection?

Use factory delegates in dependency injection when you need runtime provider selection or dynamic service resolution. While constructor injection is the default, factories provide flexibility for complex wiring scenarios where the implementation is determined at runtime.