microsoft-extensions-dependency-injection_skill

This skill helps organize dependency injection registrations via extension methods, enabling cohesive grouping, reuse in production and tests, and cleaner
  • Shell

643

GitHub Stars

2

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill aaronontheweb/dotnet-skills --skill microsoft-extensions-dependency-injection

  • advanced-patterns.md7.2 KB
  • SKILL.md9.5 KB

Overview

This skill shows how to organize ASP.NET Core dependency injection registrations using IServiceCollection extension methods. It teaches grouping related services into composable Add* methods to keep Program.cs small, enforce clear boundaries, and make production configuration reusable in tests. The patterns cover configuration binding, conditional or factory registrations, layered composition, and integration points like Akka.Hosting.

How this skill works

It defines feature-focused IServiceCollection extension methods (e.g., AddUserServices, AddEmailServices) that encapsulate cohesive registrations and return IServiceCollection for chaining. Extensions can bind options, register factories, choose implementations by environment, depend on other Add* methods, and be composed into higher-level AddAppServices. Tests reuse these same extensions, then override external dependencies.

When to use it

  • When Program.cs or Startup.cs is growing with many service registrations
  • When you need reusable production configuration for tests or test hosts
  • When building libraries that must integrate with Microsoft.Extensions.DependencyInjection
  • When you want clear boundaries and discoverable service groups for teams
  • When layering domain, infrastructure, and API registrations for large apps

Best practices

  • Group related registrations into Add{Feature}Services() and keep each extension focused
  • Place extension files next to the feature they register for easy discoverability
  • Return IServiceCollection to allow fluent chaining and composition
  • Expose configuration parameters (or bind options) instead of hardcoding settings
  • Prefer idempotent extensions so callers can call AddEmailServices() safely from multiple places
  • Override external dependencies in tests by removing and replacing service registrations

Example use cases

  • Compose Program.cs with builder.Services.AddUserServices().AddOrderServices().AddEmailServices() for a clean startup
  • Create AddEmailServices(configSection) that binds EmailOptions and registers template renderer, composers, and IEmailSender
  • Use layered composition: AddAppServices().AddDomainServices().AddInfrastructureServices() to assemble the app
  • In WebApplicationFactory tests, reuse Add* methods and Replace IEmailSender or IPaymentProcessor with test doubles
  • Register keyed notification senders with AddNotificationServices and dispatch via a resolver

FAQ

Prefer idempotent calls: either document dependency order or call the dependent Add* inside if it is safe and idempotent. Clear documentation avoids surprises.

How do I keep configuration visible and testable?

Accept configuration parameters or bind options with BindConfiguration and ValidateOnStart instead of hardcoding values. This makes overrides in tests straightforward.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
microsoft-extensions-dependency-injection skill by aaronontheweb/dotnet-skills | VeilStrat