maui-dependency-injection
Installation
SKILL.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
AddScopedin MAUI — there is no built-in scope per page. Using it without manually creatingIServiceScopegives you singleton behaviour silently, which is confusing and error-prone.
Singleton traps
// ❌ ViewModel registered as Singleton — stale data across navigations
builder.Services.AddSingleton<DetailViewModel>();
Related skills