zoukankan      html  css  js  c++  java
  • AspectCore操作aop

    AspectCore是一个很好的aop矿建,能够很好的支持async,我这边是使用autofac作为依赖注入容器,

    在nuget上直接安装AspectCore.Extensions.Autofac,就可以使用了。

    class Program
        {
    
            static async Task Main(string[] args)
            {
                ContainerBuilder builder=new ContainerBuilder();
                builder.RegisterType<MainModel>();
                builder.RegisterType<TestThree>().As<ITestThree>();
                builder.RegisterType<MainModelAsync>();
                builder.RegisterDynamicProxy();
                var container=builder.Build();
                container.Resolve<MainModel>().Test();
               await container.Resolve<MainModelAsync>().TestAsync();
            }
    
        }
    
        public class MainModel
        {
            [TestTwo]
            [TestOne]
            public virtual void Test()
            {
                Console.WriteLine("test");
            }
        }
    
        public class MainModelAsync
        {
            [TestTwo]
            [TestOne]
            public virtual async Task TestAsync()
            {
                await Task.Delay(1000);
                Console.WriteLine("test");
            }
        }
    
        public class TestOneAttribute : AbstractInterceptorAttribute
        {
            /// <summary>
            /// 
            /// </summary>
            [FromServiceContext]
            public ITestThree TestThree { get; set; }
            public override async Task Invoke(AspectContext context, AspectDelegate next)
            {
                TestThree.Test();
                Console.WriteLine("one start");
                await next(context);
                Console.WriteLine("one end");
            }
        }
        public class TestTwoAttribute : AbstractInterceptorAttribute
        {
            public override async Task Invoke(AspectContext context, AspectDelegate next)
            {
                var testThree = context.ServiceProvider.Resolve<ITestThree>();
                testThree.Test();
                Console.WriteLine("two start");
                await next(context);
                Console.WriteLine("two end");
            }
        }
    
        public interface ITestThree
        {
            void Test();
        }
        public class TestThree:ITestThree
        {
            public void Test()
            {
                Console.WriteLine("test three");
            }
        }
    View Code

    属性注入有两种方法:

    1.以特性注入的方式:

    /// <summary>
            /// 
            /// </summary>
            [FromServiceContext]
            public ITestThree TestThree { get; set; }
    View Code

    2.使用ioc获取

      public override async Task Invoke(AspectContext context, AspectDelegate next)
            {
                var testThree = context.ServiceProvider.Resolve<ITestThree>();
                testThree.Test();
                Console.WriteLine("two start");
                await next(context);
                Console.WriteLine("two end");
            }
    View Code
  • 相关阅读:
    CODE[VS] 3026 恶心的扑克
    CODE[VS] 2951 打字比赛
    CODE[VS] 2774 火烧赤壁
    CODE[VS] 1860 最大数 1998年NOIP全国联赛提高组
    matplotlib学习笔记(一)
    LUNA16数据集(二)肺结节可视化
    [转载]pytorch自定义数据集
    [转载]什么情况下应该设置 cudnn.benchmark = True?
    pytorch构建优化器
    pytorch批训练数据构造
  • 原文地址:https://www.cnblogs.com/sczmzx/p/12982199.html
Copyright © 2011-2022 走看看