New .NET Community Toolkit Analyzers Help Noobs Improve Code

An update to Microsoft's .NET Community Toolkit provides new dedicated analyzers that not only flags potential errors but also help beginners understand and improve their code.

The kit is a collection of UI-agnostic helpers and APIs that work for all .NET developers, because all the APIs have no runtime or framework dependencies and its libraries can target everything from .NET Standard 2.0 to .NET 7. It's one of a number of similar toolkits, which typically are collections of helper functions, custom controls and app services designed to simplify and demonstrate common developer tasks for projects of varying types. The .NET Foundation project, maintained by Microsoft, just advanced to version 8.1 following the August 2022 debut of v8.0, which introduced a new sample app shown in action here:
MVVM Toolkit Sample App in Animated Action
[Click on image for larger view.] MVVM Toolkit Sample App in Animated Action (source: Microsoft).

The new analyzers come with the MVVM component of the kit, which helps developers use the Model-ViewModel-View software design pattern (separating UI code from core business or back-end logic). This component was formerly called the Windows Community Toolkit before the project's breadth advanced.

"This release of the MVVM Toolkit is also the first one to introduce dedicated analyzers to help developers use the MVVM Toolkit in the best way possible," the dev team said in a Jan. 18 blog post. "That is, the MVVM Toolkit will no longer just emit diagnostics for features used incorrectly (as in, in a way that would result in an error), but it will now also show recommendations to improve the code and avoid common errors! 🎯"

One example of the new functionality is an analyzer that can flag assignments to fields backing an observable property, along with showing diagnostics to suggest referencing a generated property instead. "No more mysterious missing property notifications!" the dev team said.

The second new analyzer can help reduce binary size in applications using the MVVM Toolkit. For example, in one example scenario, it can suggest that instead of using the [ObservableObject] attribute to implement an interface, a developer should use inheritance instead. So the analyzer understands that the attribute is meant for use only when a class is already inheriting from another type and thus can't gain functionality inherited from another class. If the containing class isn't already inheritable, the developer should use inheritance to gain the functionality that's provided by the attribute, shrinking the amount of code.

"This will particularly help beginners, who might not understand the nuances of the two different approaches and might not know how to choose," Microsoft said. "In those cases, the analyzer will now be there to help."

Also here to help are new source generator optimizations for the MVVM Toolkit that improve things in several ways, with the team listing five. "As we mentioned, this new release also includes major performance optimizations to the MVVM Toolkit, to improve the developer UX even more especially when working on very large solutions," the team said. "We spent a lot of time improving the architecture of all our generators and talking with engineers from the Roslyn team to make sure that we were doing everything possible to squeeze as much performance out of them as we could."

The new update also supports .NET 7 and C# 11, and marks the first support of custom attributes for [ObservableProperty], one of the most requested features.

Along with the MVVM component, the toolkit has others called Common, Diagnostics and HighPerformance.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus


Subscribe on YouTube