All Projects → fs7744 → Norns

fs7744 / Norns

Licence: MIT license
dotnet core aop static weaving on roslyn

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to Norns

Cauldron
C# Toolkit
Stars: ✭ 68 (+195.65%)
Mutual labels:  aspect, netcore, aop
Aspectcore Framework
AspectCore is an AOP-based cross platform framework for .NET Standard.
Stars: ✭ 1,318 (+5630.43%)
Mutual labels:  netcore, aop
Blog.core
💖 ASP.NET Core 6.0 全家桶教程,前后端分离后端接口,vue教程姊妹篇,官方文档:
Stars: ✭ 3,542 (+15300%)
Mutual labels:  netcore, aop
NCop
Composite-aspect oriented framework for .NET
Stars: ✭ 30 (+30.43%)
Mutual labels:  aspect, aop
Decor.NET
A simple way to decorate a class with additional functionality using attributes.
Stars: ✭ 29 (+26.09%)
Mutual labels:  netcore, aop
Meiam.system
.NET 5 / .NET Core 3.1 WebAPI + Vue 2.0 + RBAC 企业级前后端分离权限框架
Stars: ✭ 340 (+1378.26%)
Mutual labels:  netcore, aop
Beike AspectD
Flutter AOP framework.(Flutter面向切面库, 最新适配Flutter v2.5.3, null-safety)
Stars: ✭ 39 (+69.57%)
Mutual labels:  aspect, aop
Pluginframework
Everything is a Plugin in .NET
Stars: ✭ 197 (+756.52%)
Mutual labels:  roslyn, netcore
Platform Compat
Roslyn analyzer that finds usages of APIs that will throw PlatformNotSupportedException on certain platforms.
Stars: ✭ 250 (+986.96%)
Mutual labels:  roslyn, netcore
AspecTS
An aspect-oriented programming library implemented in TypeScript
Stars: ✭ 21 (-8.7%)
Mutual labels:  aspect, aop
doteasy.rpc
Inspired by microservices, a lightweight framework that looks like a rabbit, based on NET Core 2.0 Standard 2 core library
Stars: ✭ 62 (+169.57%)
Mutual labels:  roslyn, netcore
Cometary
Roslyn extensions, with a touch of meta-programming.
Stars: ✭ 31 (+34.78%)
Mutual labels:  roslyn, netcore
AOP With Roslyn
Roslyn AOP
Stars: ✭ 66 (+186.96%)
Mutual labels:  roslyn, aop
Roslynator
A collection of 500+ analyzers, refactorings and fixes for C#, powered by Roslyn.
Stars: ✭ 2,221 (+9556.52%)
Mutual labels:  roslyn
Csharp Source Generators
A list of C# Source Generators (not necessarily awesome) and associated resources: articles, talks, demos.
Stars: ✭ 218 (+847.83%)
Mutual labels:  roslyn
Dotnetomdgenerator
A Roslyn-based cross-platform tool that generates an object model diagram from a set of C# source files or assemblies
Stars: ✭ 160 (+595.65%)
Mutual labels:  roslyn
Cs2cpp
C# to C++ transpiler (Cs2Cpp) (Powered by Roslyn)
Stars: ✭ 155 (+573.91%)
Mutual labels:  roslyn
SharpPlugs
.Net Core 鋒利扩展
Stars: ✭ 26 (+13.04%)
Mutual labels:  netcore
Retyped
Access 3600+ libraries from C# and let Bridge.NET compile your project into JavaScript.
Stars: ✭ 216 (+839.13%)
Mutual labels:  roslyn
Curl To Csharp
curl to C# converter
Stars: ✭ 153 (+565.22%)
Mutual labels:  roslyn

Norns

中文文档

Goal

This a project to do static weaving and dynamic weaving.

Desgin

AOP base on proxy

  1. Generate proxy class type
  2. Replace type to proxy type for di
  3. Do aop in proxy class

Static weaving generate code base on roslyn

There is two way that we will try to support :

AOT (Norns.Skuld)

experimental feature

(ps: because source-generators not allow loading referenced assemblies now, so can't share package to other now. I will try to find way to fix this.)

JIT (Norns.Verthandi)

  • use Reflection to generator proxy class code
  • use roslyn sdk to convert code to type
Not use this in production.

Roslyn is so great, but if we just use it once before di, it seem wasting a lot of memory and cpu.

Actually we can do jit after generate dll to generate proxy dll, make the jit to aot after build.

But now there is source-generators.

Dynamic weaving Emit (Norns.Urd)

  • Emit to generate proxy type

(ps: this will begin after static weaving done)

How to use

JIT (Norns.Verthandi)

  1. reference Norns.Adapters.DependencyInjection

  2. write InterceptorGenerator base on AbstractInterceptorGenerator

using Norns.Destiny.Structure;
using Norns.Destiny.AOP;
using Norns.Destiny.AOP.Notations;
using Norns.Destiny.Notations;
using System.Collections.Generic;
using System.Linq;

namespace Norns.Benchmark
{
    public class ConsoleCallMethodGenerator : AbstractInterceptorGenerator
    {
        public override IEnumerable<INotation> BeforeMethod(ProxyGeneratorContext context, IMethodSymbolInfo method)
        {
            typeof(System.Console).Name.ToString(); // just make sure load System.Console dll before jit generate code
            if (!method.Parameters.IsEmpty)
            {
                yield return $"System.Console.WriteLine($\"Call Method {method.Name} at {{System.DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss.fff\")}} {method.Parameters.First().Type.FullName} {method.Parameters.First().Name} = {{{method.Parameters.First().Name}}}".ToNotation();
                foreach (var item in method.Parameters.Skip(1))
                {
                    yield return $", {item.FullName} {item.Name} = {{{item.Name}}}".ToNotation();
                }
                yield return "\");".ToNotation();
            }
        }

        public override IEnumerable<INotation> AfterMethod(ProxyGeneratorContext context, IMethodSymbolInfo method)
        {
            if (method.HasReturnValue)
            {
                yield return $"System.Console.WriteLine($\"return {{{context.GetReturnValueParameterName()}}} at {{System.DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss.fff\")}}\");".ToNotation();
            }
        }
    }
}
  1. write interface
using System;

namespace Norns.Benchmark
{
    public interface IC
    {
        int AddOne(int v);

        public int AddOne2(int v)
        {
            return v + 1;
        }
    }
}
  1. write class
public class C : IC
{
    public int AddOne(int v)
    {
        return v;
    }
}
  1. set to DI

if use asp.net core, just use UseVerthandiAop like :

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseVerthandiAop(new IInterceptorGenerator[] { new ConsoleCallMethodGenerator() })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

if not, you can try this :

internal class Program
{
    private static void Main(string[] args)
    {
        var p = new ServiceCollection()
            .AddTransient<IC, C>()
            .BuildVerthandiAopServiceProvider(new IInterceptorGenerator[] { new ConsoleCallMethodGenerator() })
            .GetRequiredService<IC>();

        var result = p.AddOne(99);
        Console.WriteLine($"p.AddOne(99) 's result is {result}.");
        Console.WriteLine();
        result = p.AddOne2(1);
        Console.WriteLine($"p.AddOne2(1) 's result is {result}.");

        Console.ReadKey();
    }
}

result :

Call Method AddOne at 2020-07-05 15:42:21.999 int v = 99
return 99 at 2020-07-05 15:42:22.002
p.AddOne(99) 's result is 99.

Call Method AddOne2 at 2020-07-05 15:42:22.003 int v = 1
return 2 at 2020-07-05 15:42:22.003
p.AddOne2(1) 's result is 2.

AOT (Norns.Skuld)

There is Noting until source-generators can do this.

Because source-generators not allow loading referenced assemblies now, so can't share package to other, and i don't want to write the demo.

Emit (Norns.Urd)

waiting to start

Plan

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].