博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Autofac与AOP功能例子
阅读量:6848 次
发布时间:2019-06-26

本文共 4656 字,大约阅读时间需要 15 分钟。

using Autofac.Extras.DynamicProxy;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace aopTest{    [Intercept(typeof(CallLogger))]    public interface IProduct    {        string Title { get; set; }        decimal Price { get; set; }        string Show();    }    [Intercept(typeof(CallLogger))]    public class Apple : IProduct    {        public Apple()        {            Title = "苹果";            Price = 5.0m;        }        public string Title { get ; set ; }        public decimal Price { get ; set; }        public virtual string Show()        {            return $"{Title} - {Price}";        }    }    [Intercept(typeof(CallLogger))]    public class Book : IProduct    {        public Book()        {            Title = "Asp.net开发";            Price = 35.0m;        }        public string Title { get; set; }        public decimal Price { get; set; }                [Auth("product.show")]        public virtual string Show()        {            Test();            return $"{Title} - {Price}";        }        protected virtual void Test()        {            Console.WriteLine("is a test code....");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace aopTest{    [AttributeUsage(AttributeTargets.Method)]    public class AuthAttribute : Attribute    {        public AuthAttribute(string code)        {            Code = code;        }        public string Code { get; set; }    }}using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace aopTest{    public class UserInfo    {        static LocalDataStoreSlot storeSlot = Thread.AllocateNamedDataSlot("user-info");        public static void SetUser(string name)        {            Thread.SetData(storeSlot, name);        }        public static string GetUser()        {            return Thread.GetData(storeSlot).ToString();        }    }}
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using Castle.DynamicProxy;namespace aopTest{    public class CallLogger : IInterceptor    {        TextWriter _output;        public CallLogger(TextWriter output)        {            _output = output;        }        public void Intercept(IInvocation invocation)        {            Stopwatch stopWatch = new Stopwatch();            stopWatch.Start();            string m = invocation.TargetType.ToString() + "." + invocation.Method.Name;            _output.WriteLine($"方法:{m}调用开始");            _output.Write("Calling method {0} with parameters {1}... ",            invocation.Method.Name,            string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));            var authAttribute = invocation.Method.GetCustomAttributes(typeof(AuthAttribute), false).FirstOrDefault();            if (authAttribute != null)            {                var authInfo = (AuthAttribute)authAttribute;                if (UserInfo.GetUser() != "zhangsan" && authInfo.Code == "product.show")                {                    _output.WriteLine($"当前用户{UserInfo.GetUser()}没有方法{m}的访问权限,需要权限{authInfo.Code}");                }            }            invocation.Proceed();            _output.WriteLine("Done: result was {0}.", invocation.ReturnValue);            stopWatch.Stop();            _output.WriteLine($"方法:{m}调用结束,总耗时{stopWatch.ElapsedMilliseconds}毫秒");        }    }}
using Autofac;using Autofac.Extras.DynamicProxy;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace aopTest{    class Program    {        static void Main(string[] args)        {            var builder = new ContainerBuilder();            /*builder.RegisterType
() .As
() .EnableInterfaceInterceptors(); builder.RegisterType
() .As
() .EnableInterfaceInterceptors();*/ builder.RegisterType
() .As
() .EnableClassInterceptors(); builder.RegisterType
() .As
() .EnableClassInterceptors(); builder.Register(c => new CallLogger(Console.Out)); var container = builder.Build(); UserInfo.SetUser("zhaoliu"); var products = container.Resolve
>(); products.ToList().ForEach(product => { product.Show(); }); Console.ReadKey(); } }}

 

转载于:https://www.cnblogs.com/huangzelin/p/10609493.html

你可能感兴趣的文章
最全的Markdown语法
查看>>
正则表达式
查看>>
Angular企业级开发(1)-AngularJS简介
查看>>
如何查看自己电脑系统的安装日期-Window上
查看>>
继承Runnable 实现Synchronized 同步锁
查看>>
好书推荐
查看>>
tomcat 连接数设置(转)
查看>>
linux下定时执行任务的方法
查看>>
Activity 之生命周期
查看>>
Windows压缩包安装MySQL
查看>>
datatable编辑一行数据的方法
查看>>
移动端web开发初探之Vuejs的简单实战
查看>>
Team Project Proposal for ASE Course---query suggestion by 3D tag cloud
查看>>
IDEA2016.3搭建Struts2+Hibernate+Spring项目环境
查看>>
多线程(一)线程创建的三种方式
查看>>
HDU-4310 Hero 贪心Or动态规划
查看>>
windows下memcache安装及配置
查看>>
第一次作业人工智能
查看>>
labeled LDA,Hierarchically Supervised LDA
查看>>
JavaScript 捕获按键
查看>>