{{
FodyFody
code weaving made funcode weaving made fun
About meAbout me
 Tomasz PluskiewiczTomasz Pluskiewicz
 WrocławWrocław
 MakoLab S.A.MakoLab S.A.
 @tpluscode@tpluscode
 About code weavingAbout code weaving
 About FodyAbout Fody
 ExamplesExamples
 QuestionsQuestions
AgendaAgenda
„„Generate” additional code to:Generate” additional code to:
 Add boilerplate code automaticallyAdd boilerplate code automatically
 Implement Aspect-Oriented ProgrammingImplement Aspect-Oriented Programming
 ObfuscationObfuscation
 Build killer APIsBuild killer APIs
What is code weaving?What is code weaving?
 Source code weavingSource code weaving
 Compile-time weavingCompile-time weaving
 Runtime weavingRuntime weaving
How can I weave?How can I weave?
 Emitting MSILEmitting MSIL
 Refelction.EmitRefelction.Emit
 Mono.CecilMono.Cecil
 Dynamic proxiesDynamic proxies
 Castle, LinFu, Unity, Impromptu InterfaceCastle, LinFu, Unity, Impromptu Interface
 AOPAOP
 PostSharp, AfterthoughtPostSharp, Afterthought
 RoslynRoslyn
The toolsThe tools
 Simple but less flexibileSimple but less flexibile
 Requires runtime reference to PostSharpRequires runtime reference to PostSharp
 Requires Visual Studio extensionRequires Visual Studio extension
 Aspects in embedded resourcesAspects in embedded resources
 Full functionality is not freeFull functionality is not free
PostSharpPostSharp
IL weaving (with Cecil)IL weaving (with Cecil)
 ILIL isis difficultdifficult
 Need to update PDBsNeed to update PDBs
 MSBuild integrationMSBuild integration
Fodies are small passerine
birds belonging to the genus
Foudia in the weaver family
Ploceidae
- wikipedia/Fody
WeaverWeaver
CodeCode
 Many pre-built weaversMany pre-built weavers
 Uses Mono.CecilUses Mono.Cecil
 Integrates into build processIntegrates into build process
 Works on build serversWorks on build servers
 Leaves no referencesLeaves no references
 Well… depending on the pluginWell… depending on the plugin
 Versatile:Versatile:
 .NET 3.5-4.5.NET 3.5-4.5
 Silverlight 4 & 5Silverlight 4 & 5
 Mono, MonoTouch, MonoDroidMono, MonoTouch, MonoDroid
 Windows Phone 7 & 8 (also Metro)Windows Phone 7 & 8 (also Metro)
 Portable Class LibraryPortable Class Library
FodyFody
 Almost 70 packages on NuGetAlmost 70 packages on NuGet
 NullGuardNullGuard
 PropertyChangedPropertyChanged
 CosturaCostura
 AnotarAnotar
 ModuleInitModuleInit
 FreezableFreezable
 MethodDecoratorMethodDecorator
 InfoOfInfoOf
 ToStringToString
Community weaversCommunity weavers
DemoDemo
 Emitting IL is still required (duh!)Emitting IL is still required (duh!)
 However FodyHowever Fody
 Injects weavers to build pipelineInjects weavers to build pipeline
 Loads assembliesLoads assemblies
 Rewrites PDBsRewrites PDBs
 Reapplies strong nameReapplies strong name
 Writes to MSBuild build logWrites to MSBuild build log
Custom weavingCustom weaving
 Assembly with .Fody suffixAssembly with .Fody suffix
 ILMerge referencesILMerge references
 Pulic ModuleWeaver classPulic ModuleWeaver class
 Mutltiple deployment optionsMutltiple deployment options
 In-solutionIn-solution
 $(SolutionDir)/Tools
 NuGet packageNuGet package
 Learn fromLearn from
 existing weavers (example follows)existing weavers (example follows)
 Fody wikiFody wiki
Writing a weaverWriting a weaver
DemoDemo
 Don’t use Visual Studio pluginDon’t use Visual Studio plugin
 Weaver order sometimes does matterWeaver order sometimes does matter
 Debugging unavailable codeDebugging unavailable code

 LogInfo requires detailed MSBuild outputLogInfo requires detailed MSBuild output
 Tools -> Projects and Solutions -> Build andTools -> Projects and Solutions -> Build and
RunRun
 Remember to update Fody packageRemember to update Fody package
GotchasGotchas
 https://github.com/Fody/https://github.com/Fody/
 https://github.com/Fody/Fody/wiki/HowToWriteAnAddinhttps://github.com/Fody/Fody/wiki/HowToWriteAnAddin
 https://github.com/Fody/Fody/wiki/ModuleWeaverhttps://github.com/Fody/Fody/wiki/ModuleWeaver
 https://github.com/Fody/Fody/wiki/DeployingAddinsAsNugetshttps://github.com/Fody/Fody/wiki/DeployingAddinsAsNugets
 http://www.planetgeek.ch/2013/08/31/dont-wear-woven-sweatshirts-instead-weave-your-http://www.planetgeek.ch/2013/08/31/dont-wear-woven-sweatshirts-instead-weave-your-
assemblies/assemblies/
 http://haacked.com/archive/2013/01/04/mitigate-the-billion-dollar-mistake-with-aspects.aspx/http://haacked.com/archive/2013/01/04/mitigate-the-billion-dollar-mistake-with-aspects.aspx/
LinksLinks
Questions?Questions?
The end – wake up nowThe end – wake up now 

Fody - code weaving made fun

  • 1.
    {{ FodyFody code weaving madefuncode weaving made fun
  • 2.
    About meAbout me Tomasz PluskiewiczTomasz Pluskiewicz  WrocławWrocław  MakoLab S.A.MakoLab S.A.  @tpluscode@tpluscode
  • 3.
     About codeweavingAbout code weaving  About FodyAbout Fody  ExamplesExamples  QuestionsQuestions AgendaAgenda
  • 4.
    „„Generate” additional codeto:Generate” additional code to:  Add boilerplate code automaticallyAdd boilerplate code automatically  Implement Aspect-Oriented ProgrammingImplement Aspect-Oriented Programming  ObfuscationObfuscation  Build killer APIsBuild killer APIs What is code weaving?What is code weaving?
  • 5.
     Source codeweavingSource code weaving  Compile-time weavingCompile-time weaving  Runtime weavingRuntime weaving How can I weave?How can I weave?
  • 6.
     Emitting MSILEmittingMSIL  Refelction.EmitRefelction.Emit  Mono.CecilMono.Cecil  Dynamic proxiesDynamic proxies  Castle, LinFu, Unity, Impromptu InterfaceCastle, LinFu, Unity, Impromptu Interface  AOPAOP  PostSharp, AfterthoughtPostSharp, Afterthought  RoslynRoslyn The toolsThe tools
  • 7.
     Simple butless flexibileSimple but less flexibile  Requires runtime reference to PostSharpRequires runtime reference to PostSharp  Requires Visual Studio extensionRequires Visual Studio extension  Aspects in embedded resourcesAspects in embedded resources  Full functionality is not freeFull functionality is not free PostSharpPostSharp
  • 8.
    IL weaving (withCecil)IL weaving (with Cecil)  ILIL isis difficultdifficult  Need to update PDBsNeed to update PDBs  MSBuild integrationMSBuild integration
  • 9.
    Fodies are smallpasserine birds belonging to the genus Foudia in the weaver family Ploceidae - wikipedia/Fody
  • 10.
  • 11.
     Many pre-builtweaversMany pre-built weavers  Uses Mono.CecilUses Mono.Cecil  Integrates into build processIntegrates into build process  Works on build serversWorks on build servers  Leaves no referencesLeaves no references  Well… depending on the pluginWell… depending on the plugin  Versatile:Versatile:  .NET 3.5-4.5.NET 3.5-4.5  Silverlight 4 & 5Silverlight 4 & 5  Mono, MonoTouch, MonoDroidMono, MonoTouch, MonoDroid  Windows Phone 7 & 8 (also Metro)Windows Phone 7 & 8 (also Metro)  Portable Class LibraryPortable Class Library FodyFody
  • 12.
     Almost 70packages on NuGetAlmost 70 packages on NuGet  NullGuardNullGuard  PropertyChangedPropertyChanged  CosturaCostura  AnotarAnotar  ModuleInitModuleInit  FreezableFreezable  MethodDecoratorMethodDecorator  InfoOfInfoOf  ToStringToString Community weaversCommunity weavers
  • 13.
  • 14.
     Emitting ILis still required (duh!)Emitting IL is still required (duh!)  However FodyHowever Fody  Injects weavers to build pipelineInjects weavers to build pipeline  Loads assembliesLoads assemblies  Rewrites PDBsRewrites PDBs  Reapplies strong nameReapplies strong name  Writes to MSBuild build logWrites to MSBuild build log Custom weavingCustom weaving
  • 15.
     Assembly with.Fody suffixAssembly with .Fody suffix  ILMerge referencesILMerge references  Pulic ModuleWeaver classPulic ModuleWeaver class  Mutltiple deployment optionsMutltiple deployment options  In-solutionIn-solution  $(SolutionDir)/Tools  NuGet packageNuGet package  Learn fromLearn from  existing weavers (example follows)existing weavers (example follows)  Fody wikiFody wiki Writing a weaverWriting a weaver
  • 16.
  • 17.
     Don’t useVisual Studio pluginDon’t use Visual Studio plugin  Weaver order sometimes does matterWeaver order sometimes does matter  Debugging unavailable codeDebugging unavailable code   LogInfo requires detailed MSBuild outputLogInfo requires detailed MSBuild output  Tools -> Projects and Solutions -> Build andTools -> Projects and Solutions -> Build and RunRun  Remember to update Fody packageRemember to update Fody package GotchasGotchas
  • 18.
     https://github.com/Fody/https://github.com/Fody/  https://github.com/Fody/Fody/wiki/HowToWriteAnAddinhttps://github.com/Fody/Fody/wiki/HowToWriteAnAddin https://github.com/Fody/Fody/wiki/ModuleWeaverhttps://github.com/Fody/Fody/wiki/ModuleWeaver  https://github.com/Fody/Fody/wiki/DeployingAddinsAsNugetshttps://github.com/Fody/Fody/wiki/DeployingAddinsAsNugets  http://www.planetgeek.ch/2013/08/31/dont-wear-woven-sweatshirts-instead-weave-your-http://www.planetgeek.ch/2013/08/31/dont-wear-woven-sweatshirts-instead-weave-your- assemblies/assemblies/  http://haacked.com/archive/2013/01/04/mitigate-the-billion-dollar-mistake-with-aspects.aspx/http://haacked.com/archive/2013/01/04/mitigate-the-billion-dollar-mistake-with-aspects.aspx/ LinksLinks
  • 19.
  • 21.
    The end –wake up nowThe end – wake up now 