.Net Community Toolkit 8.1: Increased speed of MVVM source code generators

Microsoft has officially released version 8.1 of the .Net Community Toolkit. In addition to modifications to .NET 7 and C# 11 as well as a number of improvements and bug fixes, the new version also brings functionality long awaited by the developer community, such as user-defined themes for [ObservableProperty]. Additionally, according to the announcement, source code generators for the MVVM Toolkit (Model View ViewModel) should offer “significantly” higher performance.

One of the most requested features of the .Net Community Toolkit by developers is the custom property tags for .Net Community Toolkit [ObservableProperty]. Previously, they could only be set manually for the MVVM toolkit source code generator. To implement the feature in the new version, the Microsoft team decided to use the current version propertyUse C# syntax. This opens the ability to tag attributes that are carried over to the generated properties without having to enter additional attributes. Using C# syntax also eliminated any problems with declarative attributes, which can only target properties and not fields.

Custom attributes can now be set as follows after the toolkit has been updated:

[ObservableProperty]
[property: JsonPropertyName("responseName")]
[property: JsonRequired]
private string? _name;

The following property is then generated from this code:

[JsonPropertyName("responseName")]
[JsonRequired]
public string? Name
{
    get => _name;
    set
    {
        if (!EqualityComparer.Default.Equals(_name, value))
        {
            OnPropertyChanging("Name");
            OnNameChanging(value);

            _name = value;

            OnPropertyChanged("Name");
            OnNameChanged(value);
        }
    }
}

partial void OnNameChanging(string? value);
partial void OnNameChanged(string? value);

Version 8.1 of the .Net Community Toolkit provides developers with more targeted support to improve the use of the MVVM toolkit. Instead of just popping up hints about incorrectly used features that may therefore result in an error, custom analysis tools are now available. Their purpose is to provide targeted recommendations for code improvement and how common mistakes can be avoided.

In coordination with the Roslyn team, the toolkit developers have initiated a number of optimization measures that should help MVVM source code generators perform better. Among other things, matching attribute generators have been migrated to the new higher level Roslyn API. Generators that run via specific attributes – eg [ObservableProperty].

To reduce the overall memory allocation, all additional models and pipelines were reviewed. The team also outsourced almost all diagnostics to Diagnostic Analysis Tools. These processes run independently and independently of the source code generators, so that the input overhead is reduced.

Learn more about the improvements in .Net Community Toolkit 8.1, including full support for .Net 7 and C# 11 Lead blog post the end. Overview of all changes It can be found in the changelog on github.


(a map)

to the home page

Leave a Reply

Your email address will not be published. Required fields are marked *