Blazing.Mvvm
🔥 Blazing.Mvvm - Full MVVM support for Blazor with CommunityToolkit.Mvvm integration. Supports all hosting models (Server, WASM, SSR, Auto, Hybrid, MAUI). Features strongly-typed navigation, automatic ViewModel registration, parameter resolution, validation support, and comprehensive lifecycle management. Includes samples and full documentation.
Install / Use
/learn @gragra33/Blazing.MvvmREADME
Blazor Extension for the MVVM CommunityToolkit
🔥 Blazing.Mvvm brings full MVVM support to Blazor applications through seamless integration with the CommunityToolkit.Mvvm. This library supports all Blazor hosting models, including Server, WebAssembly (WASM), Static Server-Side Rendering (SSR), Auto, Hybrid (WPF, WinForms, Avalonia), and MAUI. It features strongly-typed ViewModel-first navigation, automatic ViewModel registration and discovery, parameter resolution between Views and ViewModels, validation support with ObservableValidator, and comprehensive lifecycle management. The library includes extensive sample projects and complete documentation to help you get started quickly.
Table of Contents
- Blazor Extension for the MVVM CommunityToolkit
Quick Start
Installation
Add the Blazing.Mvvm NuGet package to your project.
Install the package via .NET CLI or the NuGet Package Manager.
.NET CLI
dotnet add package Blazing.Mvvm
NuGet Package Manager
Install-Package Blazing.Mvvm
Configuration
Configure the library in your Program.cs file. The AddMvvm method adds the required services for the library and automatically registers ViewModels that inherit from ViewModelBase, RecipientViewModelBase, or ValidatorViewModelBase in the calling assembly.
using Blazing.Mvvm;
builder.Services.AddMvvm(options =>
{
options.HostingModelType = BlazorHostingModelType.WebApp;
});
[!NOTE] Since v3.1.0, the
BasePathproperty is automatically detected from the application's base URI and is no longer required for subpath hosting or YARP scenarios. See the Subpath Hosting section for details.
If you are using a different hosting model, set the HostingModelType property to the appropriate value. The available options are:
BlazorHostingModelType.HybridBlazorHostingModelType.ServerBlazorHostingModelType.WebAppBlazorHostingModelType.WebAssemblyBlazorHostingModelType.HybridMaui
Registering ViewModels in a Different Assembly
If the ViewModels are in a different assembly, configure the library to scan that assembly for the ViewModels.
using Blazing.Mvvm;
builder.Services.AddMvvm(options =>
{
options.RegisterViewModelsFromAssemblyContaining<MyViewModel>();
});
// OR
var vmAssembly = typeof(MyViewModel).Assembly;
builder.Services.AddMvvm(options =>
{
options.RegisterViewModelsFromAssembly(vmAssembly);
});
Usage
Create a ViewModel inheriting the ViewModelBase class
[ViewModelDefinition(Lifetime = ServiceLifetime.Scoped)]
public sealed partial class FetchDataViewModel : ViewModelBase, IDisposable
{
private readonly IWeatherService _weatherService;
private readonly ILogger<FetchDataViewModel> _logger;
private readonly CancellationTokenSource _cancellationTokenSource = new();
[ObservableProperty]
private IEnumerable<WeatherForecast>? _weatherForecasts;
public string Title => "Weather forecast";
public FetchDataViewModel(IWeatherService weatherService, ILogger<FetchDataViewModel> logger)
{
_weatherService = weatherService;
_logger = logger;
}
public override async Task OnInitializedAsync()
{
WeatherForecasts = await _weatherService.GetForecastAsync() ?? [];
}
public void Dispose()
{
_logger.LogInformation("Disposing {VMName}.", nameof(FetchDataViewModel));
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
}
Create your Page inheriting the MvvmComponentBase<TViewModel> component
[!NOTE] If working with repositories, database services, etc., that require a scope, then use
MvvmOwningComponentBase<TViewModel>instead.
@page "/fetchdata"
@inherits MvvmOwningComponentBase<FetchDataViewModel>
<PageTitle>@ViewModel.Title</PageTitle>
<h1>@ViewModel.Title</h1>
@if (ViewModel.WeatherForecasts is null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in ViewModel.WeatherForecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
Give a ⭐
If you like this project or are using it to learn or start your own solution, please give it a star. Thanks!
Also, if you find this library useful, and you're feeling really generous, then please consider buying me a coffee ☕.
Documentation
The Library supports the following hosting models:
- Blazor Server App
- Blazor WebAssembly App (WASM)
- Blazor Web App (.NET 8.0+)
- Blazor Hybrid - Wpf, WinForms, MAUI, and Avalonia (Windows only)
The library package includes:
MvvmComponentBase,MvvmOwningComponentBase(Scoped service support), &MvvmLayoutComponentBasefor quick and easy wiring up ViewModels.ViewModelBase,RecipientViewModelBase, &ValidatorViewModelBasewrappers for the CommunityToolkit.Mvvm.MvvmNavigationManagerclass,MvvmNavLink, andMvvmKeyNavLinkcomponent for MVVM-style navigation, no more hard-coded paths.- Sample applications for getting started quickly with all hosting models.
View Model
The library offers several base classes that extend the CommunityToolkit.Mvvm base classes:
ViewModelBase: Inherits from theObservableObjectclass.RecipientViewModelBase: Inherits from the [ObservableRecipient](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/observablerecipi
