Rougamo.Fody 4.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rougamo.Fody --version 4.0.0                
NuGet\Install-Package Rougamo.Fody -Version 4.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Rougamo.Fody" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rougamo.Fody --version 4.0.0                
#r "nuget: Rougamo.Fody, 4.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Rougamo.Fody as a Cake Addin
#addin nuget:?package=Rougamo.Fody&version=4.0.0

// Install Rougamo.Fody as a Cake Tool
#tool nuget:?package=Rougamo.Fody&version=4.0.0                

Rougamo - 肉夹馍

中文 | English

What is Rougamo

Rougamo is a static code weaving AOP component. Commonly used AOP components include Castle, Autofac, and AspectCore. Unlike these components, which typically implement AOP through dynamic proxy and IoC mechanisms at runtime, Rougamo achieves AOP by directly modifying the target method's IL code during compilation. Rougamo supports all types of methods, including synchronous/asynchronous methods, static/instance methods, constructors, and properties.

The origin of the name.

Rougamo, a type of traditional Chinese street food, is somewhat similar to a hamburger, as both involve meat being sandwiched between bread. Every time I mention AOP, I think of Rougamo, because AOP is just like Rougamo, do aspects around the mehod.

Rougamo's Advantages and Disadvantages

  • Advantages:

    1. Shorter Application Startup Time: Static weaving occurs at compile time, whereas dynamic proxies are set up at application startup.
    2. Support for All Methods: Rougamo supports all methods, including constructors, properties, and static methods. Dynamic proxies often only handle instance methods due to their reliance on IoC.
    3. Independent from Other Components: Rougamo does not require initialization and is easy and quick to use. Dynamic AOP relies on IoC components, which often need initialization at application startup and vary between different IoC components. Rougamo does not require initialization; you just define the aspect type and apply it.
  • Disadvantages:

    1. Larger Assembly Size: Since Rougamo is a compile-time AOP, it weaves additional code into the assembly at compile time, which increases the assembly size. However, this overhead is generally minimal and can be evaluated in your project through configuration options.
    2. Less Convenient IoC Interaction: Dynamic AOP can easily access the IoC container and inject types into aspect types due to its reliance on IoC. Rougamo, being a compile-time AOP, does not depend on IoC components and thus cannot directly interact with IoC through methods like constructor injection. However, it can still be used with IoC; see using IoC in Rougamo for details.

Basic Functionality Introduction

As an AOP component, Rougamo's primary function is to execute additional operations at key lifecycle points of a method. Rougamo supports four lifecycle points (or Join Points):

  1. Before method execution;
  2. After method execution successfully;
  3. After method throws an exception;
  4. When the method exits (regardless of whether it was successful or threw an exception, similar to try..finally).

Here's a simple example demonstrating how lifecycle points are expressed in code:

// Define a type inheriting from MoAttribute
public class TestAttribute : MoAttribute
{
    public override void OnEntry(MethodContext context)
    {
        // OnEntry corresponds to before method execution
    }

    public override void OnException(MethodContext context)
    {
        // OnException corresponds to after method throws an exception
    }

    public override void OnSuccess(MethodContext context)
    {
        // OnSuccess corresponds to after method execution successfully
    }

    public override void OnExit(MethodContext context)
    {
        // OnExit corresponds to when method exits
    }
}

At each lifecycle point, in addition to performing operations like logging, measuring method execution time, and APM instrumentation that don't affect method execution, you can also:

  1. Modify method parameters
  2. Intercept method execution
  3. Modify method return values
  4. Handle method exceptions
  5. Retry method execution

Applying Rougamo to Methods

The simplest and most direct way is to apply the defined Attribute directly to methods. This can include synchronous and asynchronous methods, instance methods and static methods, properties and property getters/setters, as well as instance and static constructors:

class Abc
{
    [Test]
    static Abc() { }

    [Test]
    public Abc() { }

    [Test]
    public int X { get; set; }

    public static Y
    {
        [Test]
        get;
        [Test]
        set;
    }

    [Test]
    public void M() { }

    [Test]
    public static async ValueTask MAsync() => await Task.Yield();
}

Applying attributes directly to methods is straightforward, but for common AOP types, adding the attribute to every method can be cumbersome. Rougamo also provides several bulk application methods:

  1. Class or assembly-level attributes
  2. Low-intrusive implementation of the empty interface IRougamo
  3. Specify an attribute as a proxy attribute and apply it to methods with the proxy attribute

When applying attributes in bulk, such as applying TestAttribute to a class, you typically don’t want every method in the class to receive TestAttribute. Instead, you may want to select methods that meet specific criteria. Rougamo offers two methods for method selection:

  1. Coarse-grained method feature matching, which allows specifying static, instance, public, private, property, constructor methods, etc.
  2. AspectJ-style class expressions, which provide AspectJ-like expressions for finer-grained matching, such as method names, return types, parameter types, etc.

Asynchronous Aspects

In modern development, asynchronous programming has become quite common. In the previous example, the method lifecycle nodes correspond to synchronous methods. If you need to perform asynchronous operations, you would have to manually block asynchronous operations to wait for the results, which can lead to resource wastage and performance loss. Rougamo provides asynchronous aspects in addition to synchronous aspects to better support asynchronous operations:

// Define a type that inherits from AsyncMoAttribute
public class TestAttribute : AsyncMoAttribute
{
    public override async ValueTask OnEntryAsync(MethodContext context) { }

    public override async ValueTask OnExceptionAsync(MethodContext context) { }

    public override async ValueTask OnSuccessAsync(MethodContext context) { }

    public override async ValueTask OnExitAsync(MethodContext context) { }
}

However, if asynchronous operations are not needed, it's still recommended to use synchronous aspects. For more information on asynchronous aspects, you can refer to Asynchronous Aspects.

Performance Optimization

Rougamo is a method-level AOP component. When applying Rougamo to methods, it instantiates related objects each time the method is called, which adds a burden to garbage collection (GC). Although this overhead is usually minimal and often negligible, Rougamo addresses performance impact by providing various optimization strategies:

  1. Partial Weaving: If you only want to record a call log before method execution and do not need other lifecycle nodes or exception handling, you can use partial weaving to include only the required functionalities. This reduces the actual IL code woven and minimizes the number of instructions executed at runtime.

  2. Structs: One difference between classes and structs is that classes are allocated on the heap, while structs are allocated on the stack. Using structs can allocate some Rougamo types on the stack, reducing GC pressure.

  3. Slimming MethodContext: MethodContext holds contextual information about the current method. This information requires additional objects and involves boxing and unboxing operations, such as for method parameters and return values. If you do not need this information, slimming down MethodContext can achieve certain optimization effects.

  4. Forced Synchronization: As discussed in Asynchronous Aspects, asynchronous aspects use ValueTask to optimize synchronous execution but still incur additional overhead. If asynchronous operations are not required, forcing synchronous aspects can avoid the extra costs of asynchronous aspects.

Learn More

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (40)

Showing the top 5 NuGet packages that depend on Rougamo.Fody:

Package Downloads
HandeSoft.Core

Package Description

HandeSoft.Web.Core

Package Description

Ray.Infrastructure

This client library is a infrastructure that including extensions and helpers etc.

HZY.Framework.Core

HZY Framework 核心 1、ScheduledAttribute 定时任务特性标记 2、IServerMetricMonitoringService 服务器指标监控 CPU、内存、硬盘、运行时长 3、HZY.Framework.DynamicApiController 动态Api控制器 4、HZY.Framework.DependencyInjection 依赖注入

BlazorWpf.SharedBlazor

BlazorWpf

GitHub repositories (5)

Showing the top 5 popular GitHub repositories that depend on Rougamo.Fody:

Repository Stars
RayWangQvQ/BiliBiliToolPro
B 站(bilibili)自动任务工具,支持docker、青龙、k8s等多种部署方式。敏感肌也能用。
dotnetcore/FreeSql
🦄 .NET aot orm, C# orm, VB.NET orm, Mysql orm, Postgresql orm, SqlServer orm, Oracle orm, Sqlite orm, Firebird orm, 达梦 orm, 人大金仓 orm, 神通 orm, 翰高 orm, 南大通用 orm, 虚谷 orm, 国产 orm, Clickhouse orm, QuestDB orm, MsAccess orm.
SciSharp/BotSharp
The AI Agent Framework in .NET
2881099/FreeSql.AdminLTE
这是一个 .NETCore MVC 中间件,基于 AdminLTE 前端框架动态产生 FreeSql 实体的增删查改界面。
kimdiego2098/ThingsGateway
ThingsGateway is a cross platform high-performance edge acquisition gateway based on Net8, providing underlying PLC communication libraries, communication debugging software, and more.
Version Downloads Last updated
4.0.3-preview-1726120802 35 9/12/2024
4.0.3-preview-1725957423 56 9/10/2024
4.0.2 205 9/9/2024
4.0.2-preview-1725956948 47 9/10/2024
4.0.2-preview-1725875652 51 9/9/2024
4.0.2-preview-1725466232 66 9/4/2024
4.0.1 1,734 9/2/2024
4.0.1-preview-1725141430 66 8/31/2024
4.0.0 3,561 8/10/2024
4.0.0-priview-1723306347 90 8/10/2024
4.0.0-priview-1722831925 62 8/5/2024
3.1.0 1,108 7/16/2024
3.0.2 341 7/8/2024
3.0.2-priview-1720363148 80 7/7/2024
3.0.2-priview-1720251661 80 7/6/2024
3.0.1 164 7/4/2024
3.0.1-priview-1720089186 85 7/4/2024
3.0.1-priview-1720085112 68 7/4/2024
3.0.0 2,474 5/4/2024
3.0.0-priview-1714754497 62 5/3/2024
3.0.0-priview-1714407561 113 4/29/2024
2.3.1 5,025 4/23/2024
2.3.1-priview-1713854631 91 4/23/2024
2.3.1-priview-1713791514 81 4/22/2024
2.3.0 3,059 3/10/2024
2.3.0-priview-1709894403 96 3/8/2024
2.2.0 2,391 1/20/2024
2.2.0-priview-1705656978 75 1/19/2024
2.2.0-priview-1705571301 81 1/18/2024
2.2.0-priview-1705566213 78 1/18/2024
2.2.0-priview-1702899195 163 12/18/2023
2.1.1 3,961 12/14/2023
2.1.1-priview-1702545048 113 12/14/2023
2.1.1-priview-1702542781 122 12/14/2023
2.0.1 1,099 11/16/2023
2.0.0 2,456 10/8/2023
2.0.0-priview-1696783135 117 10/8/2023
2.0.0-priview-1696592398 109 10/6/2023
2.0.0-priview-1695658688 133 9/25/2023
2.0.0-priview-1695465141 125 9/23/2023
2.0.0-priview-1680984436 201 4/8/2023
2.0.0-priview-1680981587 162 4/8/2023
1.4.1 9,512 3/12/2023
1.4.1-priview-1678603084 170 3/12/2023
1.4.1-priview-1678557697 168 3/11/2023
1.4.1-priview-1678557463 170 3/11/2023
1.4.0 2,583 3/1/2023
1.4.0-beta 291 2/27/2023
1.4.0-alpha 199 2/25/2023
1.3.4 50,974 2/17/2023
1.3.3 955 1/17/2023
1.3.2 15,976 12/20/2022
1.3.1 319 12/20/2022
1.3.1-beta 157 12/14/2022
1.3.0 1,217 12/8/2022 1.3.0 is deprecated because it has critical bugs.
1.2.3 329 1/17/2023
1.2.2 303 12/20/2022
1.2.2-beta 150 12/14/2022
1.2.1 672 11/29/2022
1.2.1-beta 144 11/29/2022
1.2.0 2,017 9/14/2022 1.2.0 is deprecated.
1.2.0-beta 165 9/12/2022
1.2.0-alpha2 153 9/12/2022
1.2.0-alpha1 160 8/31/2022
1.2.0-alpha 150 8/30/2022
1.1.4 353 11/29/2022
1.1.4-alpha 169 12/25/2022
1.1.3 505 9/11/2022 1.1.3 is deprecated because it has critical bugs.
1.1.2 1,568 8/22/2022
1.1.2-beta 159 8/22/2022
1.1.1 2,637 8/8/2022
1.1.1-beta 169 8/1/2022
1.1.0 612 7/28/2022
1.1.0-beta 189 7/15/2022
1.1.0-alpha4 169 6/24/2022
1.1.0-alpha3 155 6/24/2022
1.1.0-alpha2 155 6/23/2022
1.1.0-alpha1 156 6/22/2022
1.1.0-alpha 168 5/22/2022
1.0.3 684 5/6/2022
1.0.3-beta 172 4/26/2022
1.0.2 664 12/23/2021
1.0.1 6,500 11/23/2021
1.0.1-beta 4,894 11/23/2021

- 新增异步切面功能
- `IMo`新增系列方法`OnEntryAsync`, `OnExceptionAsync`, `OnSuccessAsync`, `OnExitAsync`,当将肉夹馍应用到异步方法上时将调用对应的`OnXxxAsync`异步切面方法
- 新增`AsyncMoAttribute`,原有的`MoAttribute`提供了默认的`OnXxxAsync`实现并且不支持重写,而`AsyncMoAttribute`相反的提供了`OnXxx`的默认实现,同时可以重写`OnXxxAsync`系列方法
- 新增`RawMoAttribute`,同时支持重写同步切面的`OnXxx`和异步切面的`OnXxxAsync`,但需要自己注意同步切面方法与异步切面方法的调用关系,不可产生递归调用
- 新增`RawMo`, `Mo`, `AsyncMo`,对应`RawMoAttribute`, `MoAttribute`, `AsyncMoAttribute`,区别在于前者仅实现`IMo`接口,没有继承`Attribute`
- `IMo`新增`ForceSync`属性,可用于指定在异步方法上强制执行同步切面方法,用于性能优化
- [[#68](https://github.com/inversionhourglass/Rougamo/issues/68)] 在async void上应用Rougamo时,编译时在输出告警信息到MSBuild,可以通过在项目文件的`PropertyGroup`节点下增加`<FodyTreatWarningsAsErrors>true</FodyTreatWarningsAsErrors>`配置,将告警信息变为错误信息,强制编译失败来避免`async void`的使用
- 异步方法在编织时将忽略`moarray-threshold`配置项,无论存在多少个`IMo`对象,都不会使用数组进行存储,因为异步切面的代码并不会因为使用数组而得到优化,反而会产生更多的操作指令。该配置对于同步方法依旧生效
- 优化ref struct参数/返回值的报错提示,一次编译会检查所有方法,并产生多个错误信息到错误列表(Error List)
- 肉夹馍产生的错误列表(Error List)中的错误信息都可以直接双击定位到对应产生该错误的方法,方便排查和反馈问题
- 删除`MethodContext`中的`IsAsync`, `IsIterator`, `MosNonEntryFIFO`, `Data`属性,将`RealReturnType`标记为过时并隐藏,同时新增`TaskReturnType`属性,该属性与`RealReturnType`具有类似功能
- 增加xcf文件,为`FodyWeavers.xml`中的`Rougamo`节点增加智能提示功能

---