.NET Tips and Tricks

Blog archive

Eliminate Code and Add Functionality with Fody Attributes

Fody is such a cool NuGet package that it's a shame it's only been mentioned on this site once and in passing. Fody handles the problem you have all the time: crosscutting concerns. A crosscutting concern is something that happens in many places in your application but not in every place.

The .NET Framework's attributes are probably the most common tool for handling crosscutting concerns. For example, security is a crosscutting concern: Many parts of your application should only be accessed by authorized users ... but not all parts (the login screen, for example, must be accessible to everyone). You can handle that crosscutting concern in ASP.NET by putting an Authorize attribute on those methods that you want to lock unauthorized users out of. Most attributes address issues important to users (security, for example). Most Fody attributes, on the other hand, handle those problems that annoy developers.

For example, the two Fody attributes I'm using the most right now (as part of building a Xamarin application) are NotifyFor (which eliminates the need to write code for the PropertyChanged event in a property) and AlsoNotifyFor (which fires a PropertyChanged event for a related property when a property changes value). All I have to do is put the attribute on my property and Fody takes care of the rest.

But there are dozens of useful Fody attributes, including ones to make your string comparisons caseless, allow you to specify the backing field for an auto-declared property, and check the syntax of your SQL queries during builds. There's also SexyProxy, which I've never needed but its name is so cute that I keep trying to find a use for it.

Posted by Peter Vogel on 07/23/2018


comments powered by Disqus

Featured

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube