$npx -y skills add Aaronontheweb/dotnet-skills --skill microsoft-extensions-dependency-injectionOrganize DI registrations using IServiceCollection extension methods. Group related services into composable Add* methods for clean Program.cs and reusable configuration in tests.
| 1 | # Dependency Injection Patterns |
| 2 | |
| 3 | ## When to Use This Skill |
| 4 | |
| 5 | Use this skill when: |
| 6 | - Organizing service registrations in ASP.NET Core applications |
| 7 | - Avoiding massive Program.cs/Startup.cs files with hundreds of registrations |
| 8 | - Making service configuration reusable between production and tests |
| 9 | - Designing libraries that integrate with Microsoft.Extensions.DependencyInjection |
| 10 | |
| 11 | ## Reference Files |
| 12 | |
| 13 | - [advanced-patterns.md](advanced-patterns.md): Testing with DI extensions, Akka.NET actor scope management, conditional/factory/keyed registration patterns |
| 14 | |
| 15 | --- |
| 16 | |
| 17 | ## The Problem |
| 18 | |
| 19 | Without organization, Program.cs becomes unmanageable: |
| 20 | |
| 21 | ```csharp |
| 22 | // BAD: 200+ lines of unorganized registrations |
| 23 | var builder = WebApplication.CreateBuilder(args); |
| 24 | |
| 25 | builder.Services.AddScoped<IUserRepository, UserRepository>(); |
| 26 | builder.Services.AddScoped<IOrderRepository, OrderRepository>(); |
| 27 | builder.Services.AddScoped<IProductRepository, ProductRepository>(); |
| 28 | builder.Services.AddScoped<IUserService, UserService>(); |
| 29 | // ... 150 more lines ... |
| 30 | ``` |
| 31 | |
| 32 | Problems: hard to find related registrations, no clear boundaries, can't reuse in tests, merge conflicts. |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## The Solution: Extension Method Composition |
| 37 | |
| 38 | Group related registrations into extension methods: |
| 39 | |
| 40 | ```csharp |
| 41 | // GOOD: Clean, composable Program.cs |
| 42 | var builder = WebApplication.CreateBuilder(args); |
| 43 | |
| 44 | builder.Services |
| 45 | .AddUserServices() |
| 46 | .AddOrderServices() |
| 47 | .AddEmailServices() |
| 48 | .AddPaymentServices() |
| 49 | .AddValidators(); |
| 50 | |
| 51 | var app = builder.Build(); |
| 52 | ``` |
| 53 | |
| 54 | --- |
| 55 | |
| 56 | ## Extension Method Pattern |
| 57 | |
| 58 | ### Basic Structure |
| 59 | |
| 60 | ```csharp |
| 61 | namespace MyApp.Users; |
| 62 | |
| 63 | public static class UserServiceCollectionExtensions |
| 64 | { |
| 65 | public static IServiceCollection AddUserServices(this IServiceCollection services) |
| 66 | { |
| 67 | services.AddScoped<IUserRepository, UserRepository>(); |
| 68 | services.AddScoped<IUserReadStore, UserReadStore>(); |
| 69 | services.AddScoped<IUserWriteStore, UserWriteStore>(); |
| 70 | services.AddScoped<IUserService, UserService>(); |
| 71 | services.AddScoped<IUserValidationService, UserValidationService>(); |
| 72 | |
| 73 | return services; |
| 74 | } |
| 75 | } |
| 76 | ``` |
| 77 | |
| 78 | ### With Configuration |
| 79 | |
| 80 | ```csharp |
| 81 | namespace MyApp.Email; |
| 82 | |
| 83 | public static class EmailServiceCollectionExtensions |
| 84 | { |
| 85 | public static IServiceCollection AddEmailServices( |
| 86 | this IServiceCollection services, |
| 87 | string configSectionName = "EmailSettings") |
| 88 | { |
| 89 | services.AddOptions<EmailOptions>() |
| 90 | .BindConfiguration(configSectionName) |
| 91 | .ValidateDataAnnotations() |
| 92 | .ValidateOnStart(); |
| 93 | |
| 94 | services.AddSingleton<IMjmlTemplateRenderer, MjmlTemplateRenderer>(); |
| 95 | services.AddSingleton<IEmailLinkGenerator, EmailLinkGenerator>(); |
| 96 | services.AddScoped<IUserEmailComposer, UserEmailComposer>(); |
| 97 | services.AddScoped<IEmailSender, SmtpEmailSender>(); |
| 98 | |
| 99 | return services; |
| 100 | } |
| 101 | } |
| 102 | ``` |
| 103 | |
| 104 | --- |
| 105 | |
| 106 | ## File Organization |
| 107 | |
| 108 | Place extension methods near the services they register: |
| 109 | |
| 110 | ``` |
| 111 | src/ |
| 112 | MyApp.Api/ |
| 113 | Program.cs # Composes all Add* methods |
| 114 | MyApp.Users/ |
| 115 | Services/ |
| 116 | UserService.cs |
| 117 | UserServiceCollectionExtensions.cs # AddUserServices() |
| 118 | MyApp.Orders/ |
| 119 | OrderServiceCollectionExtensions.cs # AddOrderServices() |
| 120 | MyApp.Email/ |
| 121 | EmailServiceCollectionExtensions.cs # AddEmailServices() |
| 122 | ``` |
| 123 | |
| 124 | **Convention**: `{Feature}ServiceCollectionExtensions.cs` next to the feature's services. |
| 125 | |
| 126 | --- |
| 127 | |
| 128 | ## Naming Conventions |
| 129 | |
| 130 | | Pattern | Use For | |
| 131 | |---------|---------| |
| 132 | | `Add{Feature}Services()` | General feature registration | |
| 133 | | `Add{Feature}()` | Short form when unambiguous | |
| 134 | | `Configure{Feature}()` | When primarily setting options | |
| 135 | | `Use{Feature}()` | Middleware (on IApplicationBuilder) | |
| 136 | |
| 137 | --- |
| 138 | |
| 139 | ## Testing Benefits |
| 140 | |
| 141 | The `Add*` pattern lets you **reuse production configuration in tests** and only override what's different. Works with WebApplicationFactory, Akka.Hosting.TestKit, and standalone ServiceCollection. |
| 142 | |
| 143 | See [advanced-patterns.md](advanced-patterns.md) for complete testing examples. |
| 144 | |
| 145 | --- |
| 146 | |
| 147 | ## Layered Extensions |
| 148 | |
| 149 | For larger applications, compose extensions hierarchically: |
| 150 | |
| 151 | ```csharp |
| 152 | public static class AppServiceCollectionExtensions |
| 153 | { |
| 154 | public static IServiceCollection AddAppServices(this IServiceCollection services) |
| 155 | { |
| 156 | return services |
| 157 | .AddDomainServices() |
| 158 | .AddInfrastructureServices() |
| 159 | .AddApiServices(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public static class DomainServiceCollectionExtensions |
| 164 | { |
| 165 | public static IServiceCollection AddDomainServices(this IServiceCollection services) |
| 166 | { |
| 167 | return services |
| 168 | .AddUserServices() |
| 169 | .AddOrderServices() |
| 170 | .AddProductServices(); |
| 171 | } |
| 172 | } |
| 173 | ``` |
| 174 | |
| 175 | --- |
| 176 | |
| 177 | ## Akka.Hosting Integration |
| 178 | |
| 179 | The same pattern works for Akka.NET actor confi |