Skip to main content

mvvmgen by Thomas Claudius Huber

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: mvvmgen

MvvmGen is a next generation MVVM library for XAML applications built with C# source generators. MvvmGen helps you to efficently build your WPF, WinUI, and MAUI apps with the Model-View-ViewModel pattern, as it generates all the boilerplate for you.

Author: Thomas Claudius Huber

NuGet: https://www.nuget.org/packages/mvvmgen/

You can find more details at https://github.com/thomasclaudiushuber/mvvmgen

Source: https://github.com/thomasclaudiushuber/mvvmgen

Author

note

Thomas Claudius Huber Alt text

Original Readme

note

⚡ MvvmGen

Build MvvmGen NuGet MvvmGen NuGet MvvmGen

Your Friend Who Writes the Boilerplate for You

Hey there, welcome to the MvvmGen repository. MvvmGen is a lightweight and modern MVVM library (.NET Standard 2.0) built with C# Source Generators that helps you to apply the popular Model-View-ViewModel-pattern (MVVM) in your XAML applications that you build with WPF, WinUI, Uno Platform, Avalonia, Xamarin Forms, or .NET MAUI.

MvvmGen is licensed under the MIT license.

Get Started

Quick intro

In this quick intro, you'll learn that creating a ViewModel is a lot of fun with MvvmGen! 🔥

Installing the MvvmGen NuGet Package

Reference the NuGet package MvvmGen in your .NET application, and then you're ready to go:

Install-Package MvvmGen

MvvmGen will register itself as a C# source generator in your project, and it will be your friend who writes the boilerplate for you.

Generating a ViewModel class

To generate a ViewModel class, you create a new class, you mark it as partial, and you put MvvmGen's ViewModel attribute on the class:

using MvvmGen;

namespace MyWpfApp.ViewModel
{
[ViewModel]
public partial class EmployeeViewModel
{
}
}

The ViewModel attribute tells MvvmGen to generate another partial EmployeeViewModel class. Right now, it will be a class that looks like this:

using MvvmGen.Commands;
using MvvmGen.Events;
using MvvmGen.ViewModels;

namespace MyWpfApp.ViewModel
{
partial class EmployeeViewModel : ViewModelBase
{
public EmployeeViewModel()
{
this.OnInitialize();
}

partial void OnInitialize();
}
}

You can see that generated class in Visual Studio under Dependencies->Analyzers: Generated class

Beside the ViewModel attribute, you find many other attributes in the MvvmGen namespace that you can use to decorate your ViewModel class. These attributes allow you to build a full ViewModel like this:

using MvvmGen;
using MvvmGen.Events;

namespace MyWpfApp.ViewModel
{
public record EmployeeSavedEvent(string FirstName, string LastName);

[Inject(typeof(IEventAggregator))]
[ViewModel]
public partial class EmployeeViewModel
{
[Property] private string _firstName;
[Property] private string _lastName;

[Command(CanExecuteMethod = nameof(CanSave))]
private void Save()
{
EventAggregator.Publish(new EmployeeSavedEvent(FirstName, LastName));
}

[CommandInvalidate(nameof(FirstName))]
private bool CanSave()
{
return !string.IsNullOrEmpty(FirstName);
}
}
}

For this ViewModel, MvvmGen will generate the following partial class definition for you

using MvvmGen.Commands;
using MvvmGen.Events;
using MvvmGen.ViewModels;

namespace MyWpfApp.ViewModel
{
partial class EmployeeViewModel : ViewModelBase
{
private IDelegateCommand? _saveCommand;

public EmployeeViewModel(MvvmGen.Events.IEventAggregator eventAggregator)
{
this.EventAggregator = eventAggregator;
this.OnInitialize();
}

partial void OnInitialize();

public IDelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand(_ => Save(), _ => CanSave());

public string FirstName
{
get => _firstName;
set
{
if (_firstName != value)
{
_firstName = value;
OnPropertyChanged("FirstName");
}
}
}

public string LastName
{
get => _lastName;
set
{
if (_lastName != value)
{
_lastName = value;
OnPropertyChanged("LastName");
}
}
}

protected MvvmGen.Events.IEventAggregator EventAggregator \{ get; private set; }

protected override void InvalidateCommands(string? propertyName)
{
base.InvalidateCommands(propertyName);
if(propertyName == "FirstName")
{
SaveCommand.RaiseCanExecuteChanged();
}
}
}
}

To learn all the details, go to the documentation in this repo.

About

note

Generate MVVM boilerplate code

How to use

Example (source csproj, source files)

This is the CSharp Project that references mvvmgen

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MvvmGen.PureCodeGeneration" Version="1.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// ***********************************************************************
// ⚡ MvvmGen => https://github.com/thomasclaudiushuber/mvvmgen
// Copyright © by Thomas Claudius Huber
// Licensed under the MIT license => See LICENSE file in repository root
// ***********************************************************************

#nullable enable

using System;

namespace MvvmGen
{
/// <summary>
/// Specifies that a DelegateCommand property in the ViewModel should be generated for a method. Set this attribute on methods of a class that has the <see cref="ViewModelAttribute"/> set.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CommandAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="CommandAttribute"/> class.
/// </summary>
public CommandAttribute() \{ }

/// <summary>
/// Initializes a new instance of the <see cref="CommandAttribute"/> class.
/// </summary>
/// <param name="canExecuteMethod">The name of the method with the can-execute logic</param>
public CommandAttribute(string canExecuteMethod)
{
CanExecuteMethod = canExecuteMethod;
}

/// <summary>
/// Gets or sets the name of the method with the can-execute logic.
/// </summary>
public string? CanExecuteMethod \{ get; set; }

/// <summary>
/// Gets or sets the name of the command property.
/// </summary>
public string? PropertyName \{ get; set; }
}
}

Useful

Download Example (.NET C#)

Share mvvmgen

https://ignatandrei.github.io/RSCG_Examples/v2/docs/mvvmgen

Category "Serializer" has the following generators:

1 GenPack

2 jsonConverterSourceGenerator

3 JsonPolymorphicGenerator

4 mvvmgen

5 Nino

6 ProtobufSourceGenerator

7 Schema

8 StackXML

9 System.Text.Json

See category

Serializer