Skip to content

DevExpress/DevExpress.Mvvm.CodeGenerators

Repository files navigation

View Model Code Generator

The DevExpress MVVM Framework includes a source generator that produces boilerplate code for your View Models at compile time. You need to define a stub View Model class that specifies the required logic. Our MVVM Framework analyzes your implementation and applied attributes to generate the final View Model class with all required boilerplate code.

Generator.mp4
  • Base View Model

    Create a partial class. Add attributes to the class and its fields/methods:

    using DevExpress.Mvvm.CodeGenerators;
    
    [GenerateViewModel]
    partial class ViewModel {
        [GenerateProperty]
        string username;
        [GenerateProperty]
        string status;
    
        [GenerateCommand]
        void Login() => Status = "User: " + Username;
        bool CanLogin() => !string.IsNullOrEmpty(Username);
    }
  • Generated View Model

    The generator inspects the base View Model and produces a partial class that complements your implementation with the following boilerplate code:

    • Properties
    • Property change notifications
    • Command declarations
    • INotifyPropertyChanged, INotifyPropertyChanging, IDataErrorInfo, ISupportServices implementation

    You can view and debug the generated View Model:

    partial class ViewModel : INotifyPropertyChanged {
        public event PropertyChangedEventHandler? PropertyChanged;
    
        protected void RaisePropertyChanged(PropertyChangedEventArgs e) => PropertyChanged?.Invoke(this, e);
    
        public string? Username {
            get => username;
            set {
                if(EqualityComparer<string?>.Default.Equals(username, value)) return;
                username = value;
                RaisePropertyChanged(UsernameChangedEventArgs);
            }
        }
    
        public string? Status {
            get => status;
            set {
                if(EqualityComparer<string?>.Default.Equals(status, value)) return;
                status = value;
                RaisePropertyChanged(StatusChangedEventArgs);
            }
        }
    
        DelegateCommand? loginCommand;
        public DelegateCommand LoginCommand {
            get => loginCommand ??= new DelegateCommand(Login, CanLogin, true);
        }
    
        static PropertyChangedEventArgs UsernameChangedEventArgs = new PropertyChangedEventArgs(nameof(Username));
        static PropertyChangedEventArgs StatusChangedEventArgs = new PropertyChangedEventArgs(nameof(Status));
    }

Prerequisites

Your project should meet the following requirements:

  • C# 9+ (VB is not supported)
  • .NET Framework v4.6.1+ or .NET Core v3.0+
  • Visual Studio v16.9.0+

Prepare Your Project

Prepare your project as outlined below to enable support for View Models generated at compile time:

  1. Add a reference to the DevExpress.Mvvm.v21.1+ or install the DevExpress.Mvvm NuGet package.

  2. Install the DevExpress.Mvvm.CodeGenerators NuGet package in your project.

  3. Set the language version to 9 in the .csproject file:

    <PropertyGroup>
        <LangVersion>9</LangVersion>
    </PropertyGroup>

    For .NET Core projects, set the IncludePackageReferencesDuringMarkupCompilation property to true additionally:

    <PropertyGroup>
        <IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
    </PropertyGroup>

Documentation

Example

Notes on Version Updates and Compatibility

Updates for the DevExpress.Mvvm.CodeGenerators NuGet package are released independently of the DevExpress component suite release cycle. We do not set specific release dates; we publish a new version when we fix reported issues.

The version number for the DevExpress.Mvvm.CodeGenerators package does not need to match that of DevExpress components. The latest available versions are compatible.

Release Notes

22.1.0

  • The View Model Code Generator now supports the updated WinUI MVVM Framework that is available in the DevExpress.WinUI.Mvvm.v22.1 assembly. If your WinUI project uses DevExpress.Mvvm.v21.2, replace it with DevExpress.WinUI.Mvvm.v22.1.

21.2.2

21.2.1

Our View Model Code Generator now supports the Prism Library.

21.1.3

We fixed the following issue: A sealed class generates protected methods.

If you use a sealed class, the following members of the generated View Model class will have the private access modifier:

  • RaisePropertyChanged methods
  • RaisePropertyChanging methods
  • ServiceContainers
  • GetService methods
  • GetRequiredService methods

21.1.2

  • You can now use the View Model Code Generator in WinUI projects.
  • You can add XML comments to a field and a method. The generated property or command now copies these comments.
  • We minimized memory allocations and improved the performance.

21.1.1

  • You can now use Generic and Nested classes when you generate View Models.
  • Use the ImplementISupportParentViewModel property to implement ISupportParentViewModel.
  • Nullable annotation is supported.
  • You can create attributes with custom arguments and apply these attributes to a field. A generated View Model now copies them.