📜 ⬆️ ⬇️

Fody and its plugins

image
With the release of Roslyn, the talk that soon you can write code will be flashed less with a new force. Today I will tell you a little about a different approach, how to write less code and therefore make fewer mistakes - I will tell you about Fody. On Habré, the mention found only in passing within the framework of solving some problems. In order to interest the reader, who has not yet decided whether to spend his time on this text, I note that the popular NotifyPropertyWeaver moved to Fody and with the help of Fody you can make all kinds of cool AOPs.


So, Fody is a tool for post-processing .net assemblies in order to embed their IL code in them via Cecil.Emit. Fody is msbuild tasku which has a configuration file in each of the projects for which it will be launched. Here such line will be added to each ProjectName.project file

<Import Project="Fody.targets" /> 

All the work on the implementation of IL make Fody plugins listed in the configuration file.
 <?xml version="1.0" encoding="utf-8" ?> <Weavers> <SuperPluginName /> </Weavers> 

The maximum do-nothing plugin can be imagined as:
 public class ModuleWeaver { //    MSBuildLog public Action<string> LogInfo { get; set; } //  Mono.Cecil.ModuleDefinition    public ModuleDefinition ModuleDefinition { get; set; } public void Execute() { } } 

')
If the question arises why Fody is needed at all, I can only say that it is very convenient. It is installed via nuget and after writing your plugin you can simply specify it as a dependency. The main thing is to understand how Fody will search for this plugin and create the correct structure of files and folders in packages and that's it - you are already rewriting IL in your builds like a boss.

Plugins


By itself, fody does not make much sense, so I present an overview of the set of plug-ins from the official website github.com/Fody/Fody . The description of each plugin is very brief, but I hope enough to decide whether to spend time reading the documentation. I hope it will be useful. In my blog (alexeysuvorov.com) I have broken the plugins into categories useful / useless *, but here I don’t allow myself to do that, so everything is alphabetically listed:

Anotar
https://github.com/Fody/Anotar
Replaces calling one method for creating / rezolving a logger and calling it. It has several built-in integrations with major logging systems. The usefulness is doubtful, but here the taste and color.

Assert message
https://github.com/Fody/AssertMessage
 Assert.AreEqual(expectedCustomer.Money, actualCustomer.Money); // => Assert.AreEqual(expectedCustomer.Money, actualCustomer.Money, "Assert.AreEqual(expectedCustomer.Money, actualCustomer.Money);"); 

Why not. If you use Assert - and do not mind waiting a few extra seconds while building this plugin while adding lines for you.

Asyncerrorhandler
https://github.com/Fody/AsyncErrorHandler
Encloses async in a try-catch block in which it executes
AsyncErrorHandler.HandleException (exception);
We do not use, but it looks tempting, one of the candidates for tighter testing and implementation, if necessary.

Bix.Mixers
https://github.com/rileywhite/Bix.Mixers.Fody
Integration with BixMix. BixMix itself seems to have recently started up and there is very little information, so we wish the guys good luck and move on.

Caseless
https://github.com/Fody/Caseless
For strings, replaces == with string.Equals with the appropriate StringComparison. I really doubt that I want IL injector to do it for me.

Catel.Fody
https://github.com/Catel/Catel.Fody
Makes the DependencyProperty normal if the class is inherited from DataObjectBase or ViewModelBase. As I understand it, Catel is positioning itself as a framework for business over the .net framework. If you use Catel, it will probably be convenient.

Commander.Fody
https://github.com/DamianReeves/Commander.Fody
Implements ICommand for MVVM. In an incomprehensible state, the documentation is not added.

Costura
https://github.com/Fody/Costura
For fans of ILMerge. Splices dependent assemblies into one.

FodyDependencyInjection
https://github.com/jorgehmv/FodyDependencyInjection
Very strange plugin. Resolve dependencies from one of the 3 IoC containers Ninject, Autofac or Spring.Net to the properties of objects bypassing the constructor. I did not understand what was there and how inside, because the message was originally wrong. I believe that all dependencies must be declared as constructor parameters, otherwise the devil will break the leg to understand the code.

EmptyConstructor
https://github.com/Fody/EmptyConstructor
Adds an empty constructor to the class if it is not defined. Doubtfully helpful.

EmptyStringGuard
https://github.com/thirkcircus/EmptyStringGuard
Overwrites setters and public methods with strings, adds a check that the string is not empty. If the line can be empty, then it can be marked with the AllowEmpty attribute. Some might find it useful, but null bothers me more than empty lines.

EnableFaking.Fody
https://github.com/philippdolder/EnableFaking.Fody
Another anti-plugin (the first was FodyDependencyInjection), adds virtual classes for all classes of public classes (in fact, there are a number of conditions). It is supposed that it is convenient for testing and it should be turned off before production. If you need to use this plugin, then something is wrong with the design.

Equals
https://github.com/Fody/Equals
For marked objects, implements Equals methods based on the properties included in the object. If you have a bunch of DTO objects and the rules for comparing them are primitively simple, then maybe this is what you need.

ExtraConstraints
https://github.com/Fody/ExtraConstraints
Allows you to specify the delegate, enum, and struct constraints on the type of the parameter. C # does not support such restrictions, but in IL they are quite valid. May appear in the following versions of C # stackoverflow.com/questions/1331739/enum-type-constraints-in-c-sharp/1331811#1331811 .

Fielder
https://github.com/Fody/Fielder
Blood gut and dismemberment - perefarshiruet all properti in fildy. The craziest plugin in my opinion.

Freezable
https://github.com/Fody/Freezable
In all classes inherited from the IFreezable interface spec, a code is injected, which is thrown by exceptions on the setters after calling the Freeze method. My eaten f # with its immutability brain can not imagine scenarios when it can be used instead of objects where readonly properties are set in the constructor and have no properties, but it may be useful.

InfoOf
https://github.com/Fody/InfoOf
Provides methodof, propertyof and fieldof, but of course no magic and everything needs to be written in lines, which will then be overwritten into the appropriate IL commands (yes, methodof, propertyof and fieldof are in IL). On the fan.

Ionad
https://github.com/Fody/Ionad
Replaces all calls to methods of static classes with your implementations with the same signatures. Probably useful for testing, but Fake (formerly moles) is doing better.

Janitor
https://github.com/Fody/Janitor
Automatically implements IDisposable for all classes inherited from IDisposable. Uses a volatile int to determine that Dispose has already been invoked. If you share the look of the plugin's author on how IDisposable should be implemented, then this is a good plugin.

JetBrainsAnnotations
https://github.com/Fody/JetBrainsAnnotations
Jet Brains and here hurry up, well done what.

MethodCache
https://github.com/Dresel/MethodCache
Memoization of methods marked with attributes. The implementation of the cache is left to us, which of course pleases. I am pleased to take a closer look and test if a suitable task arises.

MethodDecorator
https://github.com/Fody/MethodDecorator
Decorates methods marked with attributes. Allows you to catch the moment of entry into the method, exit from it and eXepshen if occurred. We widely use in the project, though not the original one, but my fork , which I finished for the needs of the project and, at the same time, I added several wishes from the discussion of the original plug-in. Able to capture parameters passed to the method. I wrote about him. Do not hesitate to file in Issues on a githaba, I will try to promptly fix / expand if possible.

MethodTimer
https://github.com/Fody/MethodTimer
Lightweight version of MethodDecorator, only measures the time it took to execute the method. Crap in Debug or logger, which you give him. Perhaps useful, although MethodDecorator is more convenient in my opinion.

Mixins.Fody
https://bitbucket.org/skwasiborski/mixins.fody/wiki/Home
Multiple inheritance through impurities. Alternative to Castle's dynamic proxy mixins. I am not a champion of morality, so I fully admit that I will return to this plugin as soon as .net pushes me into the need to inherit from the Framework class and from my class. It really was once.

ModuleInit
https://github.com/Fody/ModuleInit
Interviewing candidates, I found out for myself that not all .net programmers are aware that the assembly contains modules. This plugin allows you to set a constructor for the module. For those who understand.

Mutable.Fody
https://github.com/ndamjan/Mutable.Fody
Antipode to Freeze. Makes an immutable mutable, though after the compiler has compiled everything. It makes sense if in F # you need to deserialize from XML, but Newton.Json has already added normal deserialization for immutable f # types, so I don’t see much point, if not legacy xml.

Nullguard
https://github.com/Fody/NullGuard
First in line for use. Generates checks for null unless explicitly specifying that null is possible. In my opinion, such things are simply necessary in languages ​​that admit null.

Obsolete
https://github.com/Fody/Obsolete
Allows you to use the Obsolete attribute effectively. You can specify the version from which when accessing Obsolete compilation errors will occur and the version of the assembly from which it does not compile with a class marked with such an attribute. Very comfortable I think.

PropertyChanged
https://github.com/Fody/PropertyChanged
Former NotifyPropertyWeaver. For those who are not familiar - this plugin adds an alert (call) to the PropertyChanged event for all setters of perperty objects inherited from INotifyPropertyChanged or tagged attributes. A completely irreplaceable thing if you ever tried to write under WPF and its subsets. Strongly recommend to use if you have a UI on WPF.

PropertyChanging
https://github.com/Fody/PropertyChanging
The same, but for the interface INotifyPropertyChanging.

Publicize
https://github.com/Fody/Publicize
Makes private fields public "hidden." The author did not specify the motivation, we leave it on his conscience.

RemoveReference.Fody
https://github.com/icnocop/RemoveReference.Fody
You can specify in the assembly level attribute which assemblies you want to remove from References after the build. As I understand it, the author suggests to treat this shamanism with this shamanism. Connect.microsoft.com/VisualStudio/feedback/details/779370/vs2012-incorrectly-resolves-mscorlib-version-when-referencing-pcl-assembly . It is still open.

Resourcer
https://github.com/Fody/Resourcer
Facilitates access to assembly resources by allowing you not to write AssemblyName. A great option for assemblies with tests. In line for use as soon as possible.

Scalpel
https://github.com/Fody/Scalpel
Cuts test methods and classes from assemblies with a code by convention or attribute. Apparently, Simon (the author of Fody and most plug-ins) practices testing "without departing from the box office." I find it hard to imagine the motivation, but of course this is not the craziest plugin, although it surely is in the top 5.

Stamp
https://github.com/Fody/Stamp
Adds a hashik of the last git commit to AssemblyVersionInformation. The taste and color.

Stiletto
https://github.com/benjamin-bader/stiletto
Another option is IoC container. It boasts an enviable omnivorous because it works where there is no Reflection.Emit. If you write on Xamarin under iOS, then look in this direction, perhaps it will not make you completely sad without IoC.

ToString
https://github.com/Fody/ToString
Like Equals, it generates code based on fields and, as you probably already guessed, the name generates the ToString method. If you really do not want to write ToString for debugging, then this is the right plugin, but the benefits are dubious, I can't remember when I redefined ToString for debugging purposes.

Usable
https://github.com/Fody/Usable
Wraps in using local variables the implementing IDisposable. Write better pens, more reliable and understandable.

Validar
https://github.com/Fody/Validar
Allows you to embed the implementation of IDataErrorInfo and INotifyDataErrorInfo into the class that implements INotifyPropertyCanged. In all classes marked with the InjectValidation attribute, the implementation of IDataErrorInfo and INotifyDataErrorInfo from the class with the name ValidationTemplate whose designer accepts INotifyPropertyChanged will be buried. If you write under WPF - be sure to look, maybe this is exactly what you need.

Visualize
https://github.com/Fody/Visualize
Arranges DebuggerDisplay attributes and if the class contains fields, it generates the “correct” code that allows you to view the value of the fields in the debugger mode. Interestingly, a proxy class is generated for sequences. A very interesting opportunity, but I don’t really like to sit in the debugger preferring unit tests to it, so I wouldn’t pay for building the project for the opportunity to see everything.

It's all. Hopefully, I saved you a few hours of wandering around the githab in search of FODI plugins. Thanks for attention.

* I evaluated all the plugins based on the depths of my ignorance, so please do not judge strictly, but oppose in the comments.

Source: https://habr.com/ru/post/220701/


All Articles