
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:
Anotarhttps://github.com/Fody/AnotarReplaces 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 messagehttps://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.
Asyncerrorhandlerhttps://github.com/Fody/AsyncErrorHandlerEncloses 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.Mixershttps://github.com/rileywhite/Bix.Mixers.FodyIntegration 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.
Caselesshttps://github.com/Fody/CaselessFor strings, replaces == with string.Equals with the appropriate StringComparison. I really doubt that I want IL injector to do it for me.
Catel.Fodyhttps://github.com/Catel/Catel.FodyMakes 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.Fodyhttps://github.com/DamianReeves/Commander.FodyImplements ICommand for MVVM. In an incomprehensible state, the documentation is not added.
Costurahttps://github.com/Fody/CosturaFor fans of ILMerge. Splices dependent assemblies into one.
FodyDependencyInjectionhttps://github.com/jorgehmv/FodyDependencyInjectionVery 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.
EmptyConstructorhttps://github.com/Fody/EmptyConstructorAdds an empty constructor to the class if it is not defined. Doubtfully helpful.
EmptyStringGuardhttps://github.com/thirkcircus/EmptyStringGuardOverwrites 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.Fodyhttps://github.com/philippdolder/EnableFaking.FodyAnother 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.
Equalshttps://github.com/Fody/EqualsFor 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.
ExtraConstraintshttps://github.com/Fody/ExtraConstraintsAllows 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 .
Fielderhttps://github.com/Fody/FielderBlood gut and dismemberment - perefarshiruet all properti in fildy. The craziest plugin in my opinion.
Freezablehttps://github.com/Fody/FreezableIn 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.
InfoOfhttps://github.com/Fody/InfoOfProvides 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.
Ionadhttps://github.com/Fody/IonadReplaces 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.
Janitorhttps://github.com/Fody/JanitorAutomatically 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.
JetBrainsAnnotationshttps://github.com/Fody/JetBrainsAnnotationsJet Brains and here hurry up, well done what.
MethodCachehttps://github.com/Dresel/MethodCacheMemoization 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.
MethodDecoratorhttps://github.com/Fody/MethodDecoratorDecorates 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.
MethodTimerhttps://github.com/Fody/MethodTimerLightweight 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.Fodyhttps://bitbucket.org/skwasiborski/mixins.fody/wiki/HomeMultiple 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.
ModuleInithttps://github.com/Fody/ModuleInitInterviewing 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.Fodyhttps://github.com/ndamjan/Mutable.FodyAntipode 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.
Nullguardhttps://github.com/Fody/NullGuardFirst 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.
Obsoletehttps://github.com/Fody/ObsoleteAllows 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.
PropertyChangedhttps://github.com/Fody/PropertyChangedFormer 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.
PropertyChanginghttps://github.com/Fody/PropertyChangingThe same, but for the interface INotifyPropertyChanging.
Publicizehttps://github.com/Fody/PublicizeMakes private fields public "hidden." The author did not specify the motivation, we leave it on his conscience.
RemoveReference.Fodyhttps://github.com/icnocop/RemoveReference.FodyYou 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.
Resourcerhttps://github.com/Fody/ResourcerFacilitates 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.
Scalpelhttps://github.com/Fody/ScalpelCuts 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.
Stamphttps://github.com/Fody/StampAdds a hashik of the last git commit to AssemblyVersionInformation. The taste and color.
Stilettohttps://github.com/benjamin-bader/stilettoAnother 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.
ToStringhttps://github.com/Fody/ToStringLike 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.
Usablehttps://github.com/Fody/UsableWraps in using local variables the implementing IDisposable. Write better pens, more reliable and understandable.
Validarhttps://github.com/Fody/ValidarAllows 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.
Visualizehttps://github.com/Fody/VisualizeArranges 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.