Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News .NET Community Toolkit 8.2: MVVM Toolkit Attributes, Performance Enhancements, and More

.NET Community Toolkit 8.2: MVVM Toolkit Attributes, Performance Enhancements, and More

Microsoft has released the latest version of its .NET Community Toolkit, version 8.2, with several new enhancements and features. According to the release, this new version brings performance enhancements both at runtime and in the MVVM Toolkit source generators, new code fixers aimed at boosting productivity, and a number of user-requested features.

The first notable addition to the MVVM Toolkit is the support for custom attributes with RelayCommand. This was suggested by users on GitHub and builds on the work done in the previous release. The new version leverages the native field: and property: C# syntax to indicate targets of custom attributes, giving users full control over attributes for all generated members when using RelayCommand to generate an MVVM command. This feature is particularly useful when using a view model that needs to support JSON serialization and requires the ability to explicitly ignore generated properties.

The original blog post provides the following code examples regarding the custom attributes with RelayCommand.

[property: JsonIgnore]
private void DoWork()
    // other code.

As a result, the previous code sample will produce the following members in the runtime.

private RelayCommand? _doWorkCommand;

public IRelayCommand DoWorkCommand => _doWorkCommand ??= new RelayCommand(DoWork);

Furthermore, in the 8.2 release of the Toolkit, developers can now take advantage of two new property change hooks that have been added to the ObservableProperty fields. These new two hooks aim to address a common scenario in MVVM, where an observable property such as a "selected item" needs to be modified, requiring changes to both old and new instances.

Sergio Pedri, software engineer II, Microsoft Store client team and author of the original blog post, states the following:

Previously, this was a scenario where using [ObservableProperty] wasn’t ideal, as it didn’t have the necessary infrastructure to easily inject such logic to perform the necessary state changes on the old and new values being set. To fix this, starting from the 8.2 release of the MVVM Toolkit there are two new property change hooks being generated for all [ObservableProperty] fields.

The blog post also provides detailed code examples regarding the usage of the ObservableProperty attribute and it is worth checking out.

By the usage of the ObservableProperty attribute, developers can ensure that the selected view model will always be correctly reported as being selected. The attribute now includes built-in support for this functionality, eliminating the need for fallback methods. The MVVM Toolkit is also optimized to automatically detect usage of this attribute, optimizing code generation. Additionally, the Roslyn compiler will remove calls to any methods that are not implemented.

According to an original blog post, the MVVM Toolkit has introduced new diagnostic analyzers in its latest release, which can detect and warn users when they access a field marked with the ObservableProperty attribute incorrectly, or when they declare a type with similar attributes without using inheritance. Moreover, the latest release of the toolkit also includes built-in code fixers for these two analyzers. As per the report, users can now easily fix their code by selecting the code fix suggested by the IntelliSense light bulb whenever the analyzers produce a warning. Additionally, the code fixers support bulk fixes, enabling users to rectify all their errors with just one click.

Regarding MVVM Toolkit, this version also brings some performance improvements to its source generators. The primary focus was on optimizing the incremental pipelines to minimize memory usage and ensure that no unnecessary objects would be kept alive across concurrent executions. Several pull requests were made, including moving two additional diagnostics to a diagnostic analyzer, which can run concurrently and out of the process. The another one is removing some Roslyn symbols from the incremental pipeline, and resolving necessary analyzer symbols early during the initial callback setup to speed up callback executions in each compilation instance.

Lastly, this release includes enhancements and fixes, such as resolving build errors in VB.NET projects and fixing forwarded double attribute parameters. The release now supports partial methods with RelayCommand and open generic types in ToTypeString. In addition, MemberNotNull is now emitted in ObservableProperty setters, and complete XML docs are available for all generated types and members for better understanding.

The complete list of changes made in this release can be viewed on the GitHub release page.

About the Author

Rate this Article