Automatic .NET Code Weaving With Fody

Write fewer lines of repetitive boilerplate code and have more time to add more business value.
Course info
Rating
(52)
Level
Intermediate
Updated
Jun 17, 2014
Duration
2h 52m
Table of contents
Introduction to Fody
Write Less Code, get More Done
Simplify Logging, Debugging, and Testing Code
Other Useful Fody Add-ins
Introduction to Writing Your Own Fody Add-ins
Description
Course info
Rating
(52)
Level
Intermediate
Updated
Jun 17, 2014
Duration
2h 52m
Description

We often spend more time than we'd like to writing repetitive, low-value, boilerplate code. If we want to spend more of our time actually writing more interesting code that adds business value, Fody can help. Fody is a modular, extensible, open source tool that comes with a whole host of pre-built add-ins to perform tasks such as automatically adding Equals, GetHashCode and Equality operators or implementing IDisposable for us. In this course, we'll learn how to use these pre-built add-ins and also create our own custom Fody add-ins.

About the author
About the author

With over 15 years experience, Jason Roberts is a Microsoft .NET MVP, freelance developer, and author.

More from the author
Working with Files and Streams in C#
Intermediate
3h 13m
12 Oct 2018
Error Handling in C# with Exceptions
Intermediate
1h 37m
6 Aug 2018
More courses by Jason Roberts
Section Introduction Transcripts
Section Introduction Transcripts

Introduction to Fody
Hi. I'm Jason Roberts from Pluralsight. Welcome to this course on Automatic. NET Code Weaving with Fody. In this module we're going to examine why we'd want to use Fody, and we're going to see how we can write less code and get more done, how we can use Fody to simplify logging, debugging, and test code, and also how we can use Fody to ensure code consistency. We'll get an introduction to the Common Intermediate Language that Fody uses behind the scenes, and then we'll get an overview of how Fody fits into our compilation process. Finally, we'll see how we can install Fody in Visual Studio using NuGet.

Write Less Code, get More Done
Hi. Welcome back to Module 2. In this module we'll look at some of the ways that Fody can help us write less code to free up more time for us to actually write code that adds business value. So, in this module we're going to see how we can use Fody to automatically implement a ToString() method on our classes that derive its string from the public properties of our class. We'll learn how we can use Fody to automatically add empty string parameter checks to our methods and also to our property setters. We'll learn how to automatically check for null values in properties and methods and also how we can additionally make sure that methods don't return nulls. We'll see how we can quickly define public properties by simply defining public fields and letting Fody convert these to properties. We'll see how we can do away with manually writing our INotifyPropertyChanged implementation code and instead use Fody to implement this for us. We'll see how rather than having to write our own Equals, GetHashCode, and equality operators for our class we can simply use Fody to do this for us. We'll learn how to remove the need to manually implement IDisposable and have Fody fill in the implementation for us including the ability to write custom code to dispose of managed and unmanaged resources. And finally, we'll see how we can use Fody to ensure that Dispose() is called on objects that implement IDisposable without having to manually write using statements ourselves. So, let's get started by looking at ToString().

Simplify Logging, Debugging, and Testing Code
Hi. Welcome back. In this module we're going to see some ways in which Fody can help us simplify our logging, our debugging, and our testing code. So, first off in this module we're going to see how we can use Fody to replace the calls to our static logging code with a more unified and simple syntax that we can use regardless of the underlying logging technology, and we'll also see how we can automatically log errors if methods throw exceptions. Next we'll see how we can use Fody to automatically improve how a type is viewed in the Visual Studio debugger. We'll then take a look at how we can use Fody to test tricky static methods if we're unable to introduce abstraction concepts such as an interface for the static methods. Next we'll see how we can get simple method execution timing code and how we can customize this to output to a custom location. We'll then go on to see how we can get Fody to add additional messages to our test assert code, and if we have test code intermingled with the production code how we can use Fody to completely remove all of our test code and also all of the references to potential testing libraries such as NUnit and Moq. So, let's get started and take a look at Anotar.

Other Useful Fody Add-ins
Hi. Welcome back. In this module we'll take a look at some of the other useful Fody Add-Ins that are available. We'll start off by seeing how we can make all of the string comparisons that are happening in an assembly case-insensitive even if the strings that we're comparing in our code are actually different in case. Next we'll see how we can better manage use of the Obsolete attribute in our code base and how we can even automatically break our build if we forget to remove an Obsolete member by a certain version number. We'll see how we can automatically make the members of our types virtual even if we haven't specified the virtual keyword in our actual source code. We'll learn how we can actually use Delegate and Enum constraints on generic types even though the C# language doesn't support this, and we'll see how Fody enables us to do this. We'll see how Fody can help us implement the Freezable pattern. So, this means we can freeze an object so that we can no longer make any changes to it, and if we try and make changes to it we'll automatically get an exception thrown. Finally, we'll look at how we can decorate an existing method with additional code and how we can write code that executes when the method starts, when the method ends, and if the method throws an exception.

Introduction to Writing Your Own Fody Add-ins
Hi. Welcome to this final module. In this module we're going to get an introduction to how we can write our own Fody add-ins. Up until this point we've been using the pre-built Fody add-ins that have been published to NuGet, but we can write our own. So, first off in this module we'll start off by getting a brief overview of Mono. Cecil, and this is the underlying technology that Fody uses to manipulate the contents of the assembly's Common Intermediate Language. We'll see how we can get a starter project from GitHub that will allow us to more quickly write our add-in. Next we'll see how this sample fits together and how the different projects in the solution relate to each other. We'll examine the sample Weaver and see how the output assembly contains the new Common Intermediate Language instructions. We'll then go and create a new test, and we'll modify the Weaver to create an interface and add it to some existing types. Finally we'll see how we can generate the NuGet installer for our new Fody add-in and how we can reference it from our local file system and use it in a new project.