dependency-injection-patterns

Organize ASP.NET Core DI registrations into composable IServiceCollection extension methods.

Updated Jan 29, 2024
One-click install
npx skills add https://github.com/riandeoliveira/aspnet-template --skill dependency-injection-patterns-riandeoliveira
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dependency-injection-patterns
Source: https://github.com/riandeoliveira/aspnet-template/tree/main/.claude/skills/microsoft-extensions-dependency-injection
Command: npx skills add https://github.com/riandeoliveira/aspnet-template --skill dependency-injection-patterns-riandeoliveira

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill prevents ASP.NET Core applications from accumulating unmanageable, hard-to-test dependency injection registrations by centralizing them into composable ServiceCollection extension methods.

Core Features & Use Cases

  • Organized Add registration methods*: Move related service registrations out of Program.cs/Startup.cs into feature-scoped Add{Feature}Services() extensions (e.g., AddUserServices(), AddEmailServices()).
  • Composable configuration for reuse: Build reusable production wiring that can be shared in integration tests and overridden only where necessary.
  • Correct lifetime and scope guidance: Apply best practices for singleton/scoped/transient lifetimes, and avoid common mistakes like injecting scoped services into singletons, especially in background services and actors.
  • Use case: When your application has 200+ registrations, group them by feature so Program.cs stays readable while tests can reuse the same composition root.

Quick Start

Use the dependency-injection-patterns skill to refactor your service registrations into feature-based IServiceCollection Add* extension methods and keep Program.cs focused on composing those methods.

Frequently Asked Questions about dependency-injection-patterns

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

FAQPage Schema
How do I organize ASP.NET Core dependency injection registrations to keep Program.cs readable?

Organize dependency injection registrations by moving related services into composable IServiceCollection extension methods like Add{Feature}Services(). This keeps Program.cs focused on composition and groups registrations by feature for better maintainability.

What is the best way to reuse service registration configurations across production and integration tests?

Reuse service registration configurations by building composable Add* extension methods that act as a shared composition root. Integration tests can call the same methods and override only the specific dependencies needed for testing.

How do I avoid injecting scoped services into singletons in ASP.NET Core background services?

Avoid injecting scoped services into singletons by following lifetime and scoping correctness rules within your feature-based service registrations. Apply best practices for singleton, scoped, and transient lifetimes to prevent common captive dependency mistakes.

Can I use IServiceCollection extension methods for feature-based library design?

IServiceCollection extension methods support feature-based library design by defining named service grouping methods that return IServiceCollection for chaining. This enables libraries to expose shareable and overridable dependency injection configurations.

What is the dependency injection pattern for grouping 200 or more service registrations in ASP.NET Core?

The pattern groups extensive service registrations into feature-scoped Add{Feature}Services() extension methods. This composition root pattern prevents unmaintainable Program.cs files and enables reuse across production and test environments.

Does refactoring dependency injection registrations into extension methods support configuration parameters?

Refactoring dependency injection registrations into extension methods supports configuration parameters. These methods can accept parameters, return IServiceCollection for chaining, and follow lifetime and scoping correctness rules for flexible composition.