Home

Awesome

RoslynWeave

An AOP code generator

Initiative

This AOP framework sets up a goal to inject C# code during compile time, by weaving with original C# code instead of IL it provides a much higher reliability and ease of use

The code generating approach

The AOP Approach

It needs .NET5.0 compiler to work !!

Example

Next Step

Usage

The basic use of the application is to generate and weave code to the code being compiled. according to WrapperTemplate.cs, it's free to take the code and write up your own template to fit your work, below only describles how to use the built in WrapperTemplate

 <ItemGroup>
    <ProjectReference Include="..\RoslynWeave\RoslynWeave.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <ProjectReference Include="..\RoslynWeave\RoslynWeave.csproj" />
  </ItemGroup>

    public class MyContext: DefaultAopContext
    {
        public MyContext()
        {
            //A very basic example to describle how to let the AopContextLoctor resolve the Context.
            //You can add some more logics such as letting your IOC container to resolve.
            //AopContextLocator resolves once per aync context so that the the AopContext is scoped.
            //Will possibly implement more complex logic for resolving to handle more scenarios
            AopContextLocator.Initialize(() => this);
        }

        protected override Task EnteringMethodAsync(MethodMetadata method)
        {
            return base.EnteringMethodAsync(method);
        }

        protected override void EnteringMethod(MethodMetadata method)
        {
            base.EnteringMethod(method);
        }

        protected override void ExitingMethod(MethodMetadata method, double timeSpent)
        {
            base.ExitingMethod(method, timeSpent);
        }

        protected override Task ExitingMethodAsync(MethodMetadata method, double timeSpent)
        {
            return base.ExitingMethodAsync(method, timeSpent);
        }

        public override bool TryHandleException(Exception data)
        {
            return base.TryHandleException(data);
        }

        public override Task<bool> TryHandleExceptionAsync(Exception data)
        {
            return base.TryHandleExceptionAsync(data);
        }
    }

How it works

Therefore in order to use it, use the _AopWrapped namespace in the program

How to customize it

Limitations

It's built as an intermediary solution while I'm waiting for .Net to release an intercept feature of the compiler. so it's expected to have limitations, I can't find a perfect solution except IL weaving, but, IL is too difficult to write and debug for many people. is impossible to me.

Something good about this trick