Index of every page on this site: llms.txt
All skills
github avatar

/mvvm-toolkit-di

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

Wire CommunityToolkit.Mvvm ViewModels into Microsoft.Extensions.DependencyInjection. Covers the .NET Generic Host composition root, constructor injection, service lifetimes (Singleton / Transient / Scoped), IMessenger registration, resolving ViewModels in Views, keyed services, testing seams, and the legacy Ioc.Default escape hatch. Use across WPF, WinUI 3, .NET MAUI, Uno, and Avalonia.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/mvvm-toolkit-di

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesdependency-injection.md

≈2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Dependency injection

The MVVM Toolkit deliberately ships no DI container of its own — it integrates with Microsoft.Extensions.DependencyInjection, the same container used by ASP.NET Core, Worker services, and the .NET Generic Host.

Default to constructor injection. Resolve services and child ViewModels through the constructor of the type that needs them. Avoid the service-locator pattern (Ioc.Default.GetService<T>()) in user code.


Recommended composition root (Generic Host)

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public partial class App : Application
{
    public IHost Host { get; }

    public App()
    {
        Host = Microsoft.Extensions.Hosting.Host
            .CreateDefaultBuilder()
            .ConfigureServices((_, services) =>
            {
                services.AddSingleton<IFilesService, FilesService>();
                services.AddSingleton<ISettingsService, SettingsService>();
                services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);

                services.AddSingleton<ShellViewModel>();
                services.AddTransient<ContactViewModel>();
                services.AddTransient<EditorViewModel>();
            })
            .Build();
    }

    public static T GetService<T>() where T : class =>
        ((App)Current).Host.Services.GetRequiredService<T>();
}

Generic Host benefits:

  • appsettings.json configuration binding via Microsoft.Extensions.Configuration
  • Built-in logging via Microsoft.Extensions.Logging
  • Hosted services (IHostedService) for background work
  • Scope validation in development builds

On WPF and Windows Forms, integrate the host lifetime with the application lifetime — see Use the .NET Generic Host in a WPF app.


Composition root (no Generic Host)

When you don't need configuration/logging/hosting, build the provider directly:

public partial class App : Application
{
    public IServiceProvider Services { get; }

    public App()
    {
        var services = new ServiceCollection();
        services.AddSingleton<IFilesService, FilesService>();
        services.AddTransient<ContactViewModel>();
        Services = services.BuildServiceProvider();
    }

    public static T GetService<T>() where T : class =>
        ((App)Current).Services.GetRequiredService<T>();
}

Constructor injection

Inject services and child ViewModels through the constructor:

public sealed partial class ContactViewModel(
    IFilesService files,
    IMessenger messenger,
    ILogger<ContactViewModel> logger)
    : ObservableRecipient(messenger)
{
    [ObservableProperty]
    private string? name;

    [RelayCommand]
    private async Task SaveAsync()
    {
        logger.LogInformation("Saving {Name}", Name);
        await files.SaveAsync(Name!);
    }
}

Why constructor injection beats a service locator:

  • Dependencies are explicit and visible at the call site.
  • Unit tests inject fakes/mocks without resorting to runtime tricks.
  • The DI container validates the dependency graph at startup (with BuildServiceProvider(validateScopes: true) in dev).
  • No hidden runtime failures — missing registrations throw immediately.

Lifetimes

Lifetime Method Typical use in XAML apps
Singleton AddSingleton<T> Shell/main-window VM, settings, file/HTTP services, the shared IMessenger, app-wide caches
Transient AddTransient<T> Per-page or per-document ViewModels (a fresh instance every resolve)
Scoped AddScoped<T> Rarely needed in client apps; useful when you create explicit IServiceScopes (per-window scopes, per-request scopes for embedded HTTP)
services.AddSingleton<ShellViewModel>();      // 1 instance for app lifetime
services.AddTransient<NoteViewModel>();        // new instance per resolve
services.AddScoped<DialogService>();           // 1 per scope (rare)

Resolving in a View

Resolve the root ViewModel for the page in code-behind, then let it pull its own dependencies:

public sealed partial class ContactPage : Page
{
    public ContactViewModel ViewModel { get; }

    public ContactPage()
    {
        ViewModel = App.GetService<ContactViewModel>();
        InitializeComponent();
    }
}

Bind in XAML with {x:Bind ViewModel.Xxx} (compiled bindings) or {Binding Xxx} against the DataContext.

For navigation frameworks (WinUI 3 Frame.Navigate, MAUI Shell, Prism, MVVMCross), let the framework resolve the page and the page resolves its ViewModel from DI. Avoid creating ViewModels manually.


IMessenger registration

The toolkit provides two implementations. Register the one you want once, and inject IMessenger everywhere:

services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);
// or
services.AddSingleton<IMessenger>(StrongReferenceMessenger.Default);

Then:

public sealed partial class MyViewModel(IMessenger messenger)
    : ObservableRecipient(messenger) { }

Multiple messengers (e.g., one per window) are also valid — register them with keyed services or as scoped instances.


Keyed services (.NET 8+)

Useful when you have multiple implementations of the same interface and want to choose one by key:

services.AddKeyedSingleton<IExporter, CsvExporter>("csv");
services.AddKeyedSingleton<IExporter, JsonExporter>("json");

public sealed partial class ExportViewModel(
    [FromKeyedServices("csv")] IExporter csvExporter,
    [FromKeyedServices("json")] IExporter jsonExporter)
    : ObservableObject
{ /* ... */ }

Testing seams

Constructor-injected dependencies are trivial to swap in tests. With Moq (or NSubstitute / FakeItEasy):

[Fact]
public async Task Save_calls_files_service()
{
    var files = new Mock<IFilesService>();
    var messenger = new WeakReferenceMessenger();
    var logger = NullLogger<ContactViewModel>.Instance;

    var vm = new ContactViewModel(files.Object, messenger, logger)
    {
        Name = "Ada"
    };

    await vm.SaveCommand.ExecuteAsync(null);

    files.Verify(f => f.SaveAsync("Ada"), Times.Once);
}

If you find yourself needing to mock Ioc.Default or static state, the ViewModel is using a service locator — refactor to constructor injection instead.


Legacy: Ioc.Default

The toolkit ships CommunityToolkit.Mvvm.DependencyInjection.Ioc for cases where constructor injection is impossible (e.g., a XAML-instantiated ViewModel for design-time data, a ValueConverter, a control template).

Setup:

Ioc.Default.ConfigureServices(
    new ServiceCollection()
        .AddSingleton<IFilesService, FilesService>()
        .AddTransient<ContactViewModel>()
        .BuildServiceProvider());

Resolve:

var files = Ioc.Default.GetRequiredService<IFilesService>();

Treat this as an escape hatch only. Inside ViewModels, services, and any class you can pass through DI, prefer constructor injection.


Common mistakes

  1. Resolving children from inside a ViewModel constructor via Ioc. Hides the dependency. Inject the child VM (or a factory) through the constructor instead.
  2. Registering everything as singleton. A "per-document" ViewModel registered as singleton becomes shared state across all documents — a subtle data-corruption bug. Use AddTransient for per-instance VMs.
  3. Building multiple ServiceProvider instances. Each BuildServiceProvider() is a fresh container — singletons aren't shared. Build once at startup, then reuse.
  4. Capturing the IServiceProvider itself in long-lived objects. Indicates a service-locator pattern. Inject the specific dependencies you need.
  5. Forgetting to wire scope validation in development. Use Host.CreateDefaultBuilder() (which sets ValidateScopes and ValidateOnBuild in development) so registration mistakes fail at startup, not at first use.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    No security issues detected. This skill provides educational documentation and C# code snippets for implementing Dependency Injection with the CommunityToolkit.Mvvm library.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 7 months ago
  • mvvm
  • dependency-injection
  • microsoft-extensions
  • csharp
  • wpf
  • winui
  • maui
  • viewmodel
  • ioc
  • dotnet

README badge

README badge for github/awesome-copilot/mvvm-toolkit-di

Wires CommunityToolkit.Mvvm ViewModels into Microsoft.Extensions.DependencyInjection, the standard .NET container. Covers composition root setup with the Generic Host, constructor injection, service lifetimes (Singleton/Transient/Scoped), IMessenger registration, and testing patterns across WPF, WinUI 3, MAUI, Uno, and Avalonia.

Generated from the current SKILL.md.

Does this work with WPF, WinUI 3, MAUI, and Avalonia?
Yes. The skill covers composition root setup and constructor injection patterns across WPF, WinUI 3, .NET MAUI, Uno, and Avalonia. WPF and Windows Forms require extra integration steps to sync host lifetime with app lifetime.
Should I use Ioc.Default in my ViewModels?
No. Avoid Ioc.Default in user code — it hides dependencies, breaks unit tests, and prevents startup validation. Use constructor injection instead. Ioc.Default is a last resort for XAML-instantiated VMs and design-time data only.
What lifetime should I use for per-page ViewModels?
Use Transient. Singleton makes per-document VMs share state across all documents, causing data corruption. Transient gives you a fresh instance each time a page resolves its ViewModel.
Do I need the .NET Generic Host or can I just use ServiceCollection?
ServiceCollection alone works if you only need a container. Generic Host is recommended because it adds configuration binding, logging, hosted services, and scope validation in development — all at minimal cost.
How do I register IMessenger for injection?
Register WeakReferenceMessenger.Default or StrongReferenceMessenger.Default as a singleton IMessenger, then inject it into ObservableRecipient ViewModels. For per-window messengers, use keyed services or scoped instances.

Generated from the current SKILL.md. These answers refresh after source changes.