C# 面向切面编程之AspectCore初探

写在前面

 AspectCore 是Lemon名下的一个国产Aop框架,提供了一个全新的轻量级和模块化的Aop解决方案。面向切面也可以叫做代码拦截,分为静态和动态两种模式,AspectCore 可以实现动态代理,支持程序运行时在内存中“临时”生成 AOP 动态代理类。

老规矩从 Nuget 安装 AspectCore.Extensions.DependencyInjection 包。

代码实现

using AspectCore.DynamicProxy;

public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Start...");
        ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder();
        using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build())
        {
            Person p = proxyGenerator.CreateClassProxy<Person>();
            Console.WriteLine(p.GetType().BaseType);
            p.Say($"{Environment.NewLine} Hello World!");
        }
        Console.WriteLine("End");
        Console.ReadLine();
    }
}

public class CustomInterceptor : AbstractInterceptorAttribute
{
    public async override Task Invoke(AspectContext context, AspectDelegate next)
    {
        try
        {
            Console.WriteLine("Before service call");
            await next(context);
        }
        catch (Exception)
        {
            Console.WriteLine("Service threw an exception!");
            throw;
        }
        finally
        {
            Console.WriteLine("After service call");
        }
    }
}

public class Person
{
    [CustomInterceptor]
    public virtual void Say(string msg)
    {
        Console.WriteLine("service calling..." + msg);
    }
}

调用示例

如图,代理类将Say方法包裹了起来。

如果修改一下CustomInterceptor 的Invoke方法,可以直接根据条件控制代码的分支跳转。

public class CustomInterceptor : AbstractInterceptorAttribute
{
    public async override Task Invoke(AspectContext context, AspectDelegate next)
    {
        try
        {
            Console.WriteLine("Before service call");
            if (false)
                await next(context);
            else
                await Task.Delay(1000);
        }
        catch (Exception)
        {
            Console.WriteLine("Service threw an exception!");
            throw;
        }
        finally
        {
            Console.WriteLine("After service call");
        }
    }
}

 运行代码 Person中的Say方法本体就被跳过了:

 

相关推荐

  1. springAOP(面向切面编程)详结

    2024-01-20 15:06:03       16 阅读
  2. AOP面向切面编程

    2024-01-20 15:06:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-20 15:06:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-20 15:06:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-20 15:06:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-20 15:06:03       20 阅读

热门阅读

  1. 判断质数(素数):

    2024-01-20 15:06:03       24 阅读
  2. 美易官方:Coinbase美股盘前涨超0.7%

    2024-01-20 15:06:03       34 阅读
  3. Spring Boot应用程序如何配置 HTTPS 访问方式

    2024-01-20 15:06:03       31 阅读
  4. 聊一聊工作中事故的复盘几点想法

    2024-01-20 15:06:03       31 阅读
  5. c#键盘事件的使用

    2024-01-20 15:06:03       30 阅读
  6. iptables TEE模块测试小记

    2024-01-20 15:06:03       35 阅读
  7. GoLang刷题之leetcode

    2024-01-20 15:06:03       33 阅读
  8. Vue学习1

    2024-01-20 15:06:03       30 阅读