dependency-injection-patterns

Organize ASP.NET Core dependency injection registrations into reusable extension methods.

1.1k|101|Updated Nov 12, 2025
One-click install
npx skills add https://github.com/Aaronontheweb/dotnet-skills --skill dependency-injection-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dependency-injection-patterns
Source: https://github.com/Aaronontheweb/dotnet-skills/tree/main/skills/microsoft-extensions-dependency-injection
Command: npx skills add https://github.com/Aaronontheweb/dotnet-skills --skill dependency-injection-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps teams organize and reuse dependency injection registrations in ASP.NET Core applications by encapsulating related registrations inside small composable extension methods, keeping Program.cs clean and tests easily configurable.

Core Features & Use Cases

  • Encapsulated Add*Methods: Create extension methods like AddUserServices(), AddOrderServices(), and AddEmailServices() to group related registrations and improve readability.
  • Test-time configurability: Allow tests to override specific services while reusing production registrations, improving maintainability and resilience.
  • Lifetime and configuration cohesion: Centralize configuration and lifetimes in one place, reducing duplication and ensuring consistent behavior across environments.
  • Use Case: In a large web app, split registrations into modular areas (Users, Orders, Payments) and compose them in the top-level AppServiceCollectionExtensions.

Quick Start

Create extension methods on IServiceCollection (e.g., AddUserServices, AddOrderServices) that register related services and return IServiceCollection. Then, in your Program.cs, call services.AddUserServices().AddOrderServices() as part of app startup.

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 dependency injection registrations in ASP.NET Core to keep Program.cs clean?

Encapsulate related dependency injection registrations into composable Add* extension methods on IServiceCollection, such as AddUserServices() or AddOrderServices(), to keep Program.cs clean and modular.

What is the best way to override dependency injection services during testing in ASP.NET Core?

Build module-based Add* extension methods that accept configuration or allow test-time overrides, enabling tests to replace specific service registrations while reusing production dependencies.

Can I group ASP.NET Core service registrations by module for different application areas?

Yes, you can group ASP.NET Core service registrations by domain areas like Users, Orders, or Payments into separate Add* extension methods, then compose them together in a top-level AppServiceCollectionExtensions class.

How does using extension methods for service registration affect testability in dotnet?

Using extension methods for service registration improves testability in dotnet by centralizing configuration and lifetime management in one place, allowing tests to easily override specific services while maintaining consistent behavior across environments.

Does this approach to dependency injection support lifetime and configuration management?

Yes, this dependency injection approach supports lifetime and configuration management by centralizing these settings inside composable Add* extension methods, reducing duplication and ensuring consistent behavior across environments.