davidortinau/maui-skills/maui-dependency-injection
Claude Code and agent skill by davidortinau/maui-skills
- community
Checking that the SKILL.md file is still in the repository.
Guidance for dependency injection in .NET MAUI apps — service registration, lifetime selection (Singleton/Transient/Scoped), constructor injection, automatic resolution via Shell navigation, explicit resolution patterns, platform-specific registrations, and testability best practices. USE FOR: "dependency injection", "DI registration", "AddSingleton", "AddTransient", "AddScoped", "service registration", "constructor injection", "IServiceProvider", "MauiProgram DI", "register services". DO NOT USE FOR: data binding (use maui-data-binding), Shell route setup (use maui-shell-navigation), or unit test mocking patterns (use maui-unit-testing).
Install
npx vskill@latest install davidortinau/maui-skills/maui-dependency-injectionSKILL.md
Dependency Injection in .NET MAUI
Lifetime Decision Framework
| Question | Answer → Lifetime | |
|---|---|---|
| Does it hold shared state or is expensive to create? | AddSingleton | |
| Is it stateless, lightweight, or per-request? | AddTransient | |
Do you manage IServiceScope yourself? | AddScoped |
⚠️ Avoid `AddScoped` in MAUI — there is no built-in scope per page.Using it without manually creatingIServiceScopegives you singletonbehaviour silently, which is confusing and error-prone.
Singleton traps
Register Pages and ViewModels as Transient. Register **services that holdshared state as Singleton** (e.g.,IDataService,HttpClientfactory).
Gotcha: XAML Resource Parsing vs. DI Timing
XAML resources (App.xaml styles, converters) are parsed during `InitializeComponent()` — before the DI container is fully available. If a resource or converter needs a service, resolve it in CreateWindow(), not in the constructor.
Gotcha: Unregistered Page Silently Skips DI
If a Page is used in Shell XAML (<ShellContent ContentTemplate="...">) but not registered in builder.Services, MAUI instantiates it with the parameterless constructor. Dependencies are silently null — no exception.
Anti-Pattern: Service Locator Overuse
Use explicit resolution (Handler.MauiContext.Services) only when constructorinjection is genuinely unavailable (e.g., inside a custom handler orplatform callback).
Platform-Specific Registration Pitfall
When using #if directives for platform services, ensure the interface is always registered — otherwise consumers on untargeted platforms get a runtime null.
Checklist
- [ ] Every Page and ViewModel that needs injection is registered in
MauiProgram.cs - [ ] Pages/ViewModels are
AddTransient; shared services areAddSingleton - [ ] Constructor injection used everywhere possible; service locator only as last resort
- [ ] Interfaces defined for any service you need to mock in tests
- [ ] Platform-specific
#ifregistrations cover all target platforms (or provide fallback) - [ ] Late-bound services resolved in
CreateWindow(), not during XAML parse - [ ]
AddScopedonly used when you manually manageIServiceScope
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingplugins/maui-skills/skills/maui-dependency-injection/SKILL.md
Popularity
- Repository stars
- 102
- Repository forks
- 15
- 7-day trend score
- 23
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 27 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/davidortinau/maui-skills/maui-dependency-injection)