Tooluck logoTooluck
SKILL.md

dependency-injection-patterns

Agent skill fromAaronontheweb/dotnet-skills

Organize DI registrations using IServiceCollection extension methods. Group related services into composable Add* methods for clean Program.cs and reusable configuration in tests.

How to install dependency-injection-patterns

install dependency-injection-patterns
# install the dotnet-skills pack with the skills CLI
npx skills add Aaronontheweb/dotnet-skills

# or add only this skill to Claude Code: copy the folder

skills/microsoft-extensions-dependency-injection/

# into

~/.claude/skills/microsoft-extensions-dependency-injection/ (all projects)

.claude/skills/microsoft-extensions-dependency-injection/ (one project)

skills/microsoft-extensions-dependency-injection/SKILL.md

MITFull file
1# Dependency Injection Patterns
2
3## When to Use This Skill
4
5Use 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
19Without organization, Program.cs becomes unmanageable:
20
21```csharp
22// BAD: 200+ lines of unorganized registrations
23var builder = WebApplication.CreateBuilder(args);
24
25builder.Services.AddScoped<IUserRepository, UserRepository>();
26builder.Services.AddScoped<IOrderRepository, OrderRepository>();
27builder.Services.AddScoped<IProductRepository, ProductRepository>();
28builder.Services.AddScoped<IUserService, UserService>();
29// ... 150 more lines ...
30```
31
32Problems: 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
38Group related registrations into extension methods:
39
40```csharp
41// GOOD: Clean, composable Program.cs
42var builder = WebApplication.CreateBuilder(args);
43
44builder.Services
45 .AddUserServices()
46 .AddOrderServices()
47 .AddEmailServices()
48 .AddPaymentServices()
49 .AddValidators();
50
51var app = builder.Build();
52…

Excerpt shown under the repository's MIT license.

More skills in dotnet-skills

Keep exploring

Similar skill repositories

Newsletter

Join the Community

Subscribe to our newsletter for the latest news and updates