microsoft-extensions-dependency-injection

Dependency injection patterns and best practices using Microsoft.Extensions.DependencyInjection for .NET applications. Use when configuring DI containers in .NET, choosing between service lifetimes (Singleton, Scoped, Transient), or implementing decorator patterns and service interception.

By wshaddix · 540 installs

npx skills add wshaddix/dotnet-skills --skill microsoft-extensions-dependency-injection

Source repository · Upstream listing

Dependency Injection Patterns When to Use This Skill Use this skill when: Organizing service registrations in ASP.NET Core applications Avoiding massive Program.cs/Startup.cs files with hundreds of registrations Making service configuration reusable between production and tests Designing libraries that integrate with Microsoft.Extensions.DependencyInjection The Problem Without organization, Program.cs becomes unmanageable: Problems: Hard to find related registrations No clear boundaries between subsystems Can't reuse configuration in tests Merge conflicts in team settings No encapsulation of internal dependencies The Solution: Extension Method Composition Group related registrations into extension methods: Each Add method encapsulates a cohesive set of registrations. Extension Method Pattern Basic Structure With Configuration With Dependencies on Other Extensions File Organization Place extension methods near the services they register: Convention : {Feature}ServiceCollectionExtensions.cs next to the feature's services. Naming Conventions Pattern Use For Add{Feature}Services() General feature registration Add{Feature}() Short form when unambiguous Configure{Feature}() When primarily setting options Use{Feature}() Middleware (on IApplicationBuilder) Testing Benefits The main advantage: reuse production configuration in tests . WebApplicationFactory Akka.Hosting.TestKit Standalone Unit Tests Layered Extensions For larger applications, compose extensions hierarchically: Akka.Hosting Integration The same pattern works for Akka.NET actor configuration: See akka/hosting actor patterns skill for complete Akka.Hosting patterns. Common Patterns Conditional Registration Factory Based Registration Keyed Services (.NET 8+) Anti Patterns Don't: Register Everything in Program.cs Don't: Create Overly Generic Extensions Don't: Hide Important Configuration Best Practices Summary Practice Benefit Group related services into Add methods Clean Program.cs, clear boundaries Place extensions near the services they register Easy to find and maintain Return IServiceCollection for chaining Fluent API Accept configuration parameters Flexibility Use consistent naming ( Add{Feature}Services ) Discoverability Test by reusing production extensions Confidence, less duplication Lifetime Management Choose the right lifetime based on state: Lifetime Use When Examples Singleton Stateless, thread safe, expensive to create Configuration, HttpClient factories, caches Scoped Stateful per request, database contexts DbContext, repositories, user context Transient Lightweight, stateful, cheap to create Validators, short lived helpers Rules of Thumb Scope Requirements Scoped services require a scope to exist. In ASP.NET Core, each HTTP request creates a scope automatically. But in other contexts (background services, actors), you must create scopes manually. Akka.NET Actor Scope Management Actors don't have automatic DI scopes. If you need scoped services inside an actor, inject IServiceProvider and create scopes manually. Pattern: Scope Per Message Why This Pattern Works 1. Each message gets fresh DbContext No stale entity tracking 2. Proper disposal Connections released after each message 3. Isolation One message's errors don't affect others 4. Testable Can inject mock IServiceProvider Singleton Services in Actors For stateless services, inject directly (no scope needed): Akka.DependencyInjection Reference Akka.NET's DI integration is documented at: Akka.DependencyInjection : https://getakka.net/articles/actors/dependency injection.html Akka.Hosting : https://github.com/akkadotnet/Akka.Hosting Common Mistakes Injecting Scoped into Singleton No Scope in Background Work Resources Microsoft.Extensions.DependencyInjection : https://learn.microsoft.com/en us/dotnet/core/extensions/dependency injection Akka.Hosting : https://github.com/akkadotnet/Akka.Hosting Akka.DependencyInjection : https://getakka.net/articles/actors/dependency injection.html Options Pattern : See microsoft extensions/configuration skill