Table of Contents

Namespace Dmnk.Blazor.Mvvm

NuGet Version

This library provides some base types to inherit from in a Blazor application, to implement the MVVM pattern. It makes no assumptions about the implementation of the ViewModel, only that it must implement INotifyPropertyChanged and that commands implement ICommand.

ViewModels are registered with their Views via DI using ViewModelRegistration. The ViewModelRegistry resolves both closed and open-generic registrations. Use the [ViewModelFor] attribute together with the source generator (see Dmnk.Blazor.Mvvm.SourceGen) to have registrations emitted automatically.

Example

// You don't have to use the community toolkit, but we will use it in this example here.
// If you are using the community toolkit, you probably also want the 
// Dmnk.Blazor.Mvvm.CommunityToolkit package, which allows binding to async relay commands.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

public class MyViewModel : ObservableObject
{
    [ObservableProperty]
    private string _text;
    
    [RelayCommand]
    private void DoSomething()
    {
        Text = "Hello World!";
    }
}
@inherits MvvmComponentBase<MyViewModel>

<p>@Vm.Text</p>

<button @onclick="@Vm.DoSomethingCommand.Bind(this)">Click me</button>

Registering ViewModels

// Program.cs — register each View/ViewModel pair as a singleton:
services.AddSingleton(ViewModelRegistration.Create<MyViewModel, MyView>());
// For open-generic types:
services.AddSingleton(ViewModelRegistration.CreateOpenGeneric(
    typeof(MyViewModel<>), typeof(MyView<>)));
services.AddSingleton<IViewModelRegistry, ViewModelRegistry>();

Or use the [ViewModelFor] attribute and the source generator to emit these calls automatically — see Dmnk.Blazor.Mvvm.SourceGen.

Classes

AbstractMvvmComponentBase<T>

Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.

CommandExtensions

See Bind<T>(ICommand, AbstractMvvmComponentBase<T>)

MvvmComponentBase<T>

Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.

The ViewModel is set as a required parameter(Vm).

OptionalMvvmComponentBase<T>

Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.

The ViewModel is set as an optional parameter(Vm).

OwningMvvmComponentBase<T>

Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.

The ViewModel is set as a property that isn't a parameter(Vm).

RegisteredViewFor<TViewModel>

Blazor component that serves as a marker for associating a ViewModel type with a View type in the ViewModelRegistry.

ViewModelForAttribute

Marks a ViewModel to be automatically registered with its corresponding view component. The source generator will create registration code for this pairing. See https://dominiksta.github.io/Dmnk.Toolkit/api/Dmnk.Blazor.Mvvm.SourceGen.html.

ViewModelRegistration

Represents a mapping of a ViewModel type to a View type. Used by ViewModelRegistry to store the associations between ViewModels and Views.

You probably want to inject a number of these into DI, such that a ViewModelRegistry that is also in DI can consume them to build its mapping.

ViewModelRegistry

Interfaces

IViewModelRegistry

Registry for associating ViewModel types with their corresponding View types.