当前位置: 首页 > news >正文

网站商城注意事项网站开发标准

网站商城注意事项,网站开发标准,新乡手机网站建设哪家专业,网页设计html代码大全动物转自#xff1a;https://www.cnblogs.com/neverc/p/5241466.html AOP介绍 面向切面编程#xff08;Aspect Oriented Programming,英文缩写为AOP#xff09;#xff0c;通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。 AOP是OOP的延续#xff0c;是软件…转自https://www.cnblogs.com/neverc/p/5241466.html AOP介绍 面向切面编程Aspect Oriented Programming,英文缩写为AOP通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。 AOP是OOP的延续是软件开发中的一个热点. 常用于 Authentication  Caching Lazy loading Transactions   AOP基本原理 普通类 1 2 3 4 5 6 7 8 9 class Person : MarshalByRefObject {     public string Say()     {         const string str  Persons say is called;         Console.WriteLine(str);         return str;     } } 代理类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class ProxyT : RealProxy where T : new() {     private object _obj;     public Proxy(object obj)         : base(typeof(T))     {         _obj obj;     }     public override IMessage Invoke(IMessage msg)     {         Console.WriteLine({0}:Invoke前, DateTime.Now);         var ret ((IMethodCallMessage)msg).MethodBase.Invoke(_obj, null);         Console.WriteLine({0}:Invoke后, DateTime.Now);         return new ReturnMessage(ret, null, 0, null, null);     } } 执行 1 2 3 4 5 6 7 8 9 10 static void Main(string[] args) {     var per  new ProxyPerson(new Person()).GetTransparentProxy() as Person;     if (per ! null)     {         var str per.Say();         Console.WriteLine(返回值:  str);     }     Console.ReadKey(); }   AOP框架 AOP有动态代理和静态IL织入. 本节主要介绍动态代理方式,静态可参考PostSharp.   Castle Core 原理:本质是创建继承原来类的代理类.重写虚方法实现AOP功能.   只需引用: Install-Package Castle.Core (在Castle的2.5以上版本已经将 Castle.DynamicProxy2.dll 里有内容集成到 Castle.Core.dll 中。)   Simple Class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public abstract class Person {     public virtual void SayHello()     {         Console.WriteLine(我是{0}方法, SayHello);     }     public virtual void SayName(string name)     {         Console.WriteLine(我是{0}方法,参数值:{1}, SayName, name);     }     public abstract void AbstactSayOther();     public void SayOther()     {         Console.WriteLine(我是{0}方法, SayOther);     } }   interceptor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class SimpleInterceptor : StandardInterceptor {     protected override void PreProceed(IInvocation invocation)     {         Console.WriteLine(拦截器调用方法前方法名是{0}。, invocation.Method.Name);     }     protected override void PerformProceed(IInvocation invocation)     {         Console.WriteLine(拦截器开始调用方法方法名是{0}。, invocation.Method.Name);         var attrs invocation.MethodInvocationTarget.Attributes.HasFlag(MethodAttributes.Abstract);//过滤abstract方法         if (!attrs)         {             base.PerformProceed(invocation);//此处会调用真正的方法 invocation.Proceed();         }     }     protected override void PostProceed(IInvocation invocation)     {         Console.WriteLine(拦截器调用方法后方法名是{0}。, invocation.Method.Name);     } }   Main 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 static void Main(string[] args) {     var generator  new ProxyGenerator();       //实例化【代理类生成器】      var interceptor  new SimpleInterceptor();  //实例化【拦截器】      //使用【代理类生成器】创建Person对象而不是使用new关键字来实例化      var person generator.CreateClassProxyPerson(interceptor);     Console.WriteLine(当前类型:{0},父类型:{1}, person.GetType(), person.GetType().BaseType);     Console.WriteLine();     person.SayHello();//拦截     Console.WriteLine();     person.SayName(Never、C);//拦截     Console.WriteLine();     person.SayOther();//普通方法,无法拦截         person.AbstactSayOther();//抽象方法,可以拦截         Console.ReadLine(); }   Castle Windsor 特性式AOP 1 2 3 4 5 6 7 8 9 10 11 12 13 public interface IPerson {     void Say(); } [Interceptor(typeof(LogInterceptor))] public class Person : IPerson {     public void Say()     {         Console.WriteLine(Persons Say Method is called!);     } }   1 2 3 4 5 6 7 8 9 public class LogInterceptor : IInterceptor {     public void Intercept(IInvocation invocation)     {         Console.WriteLine({0}:拦截{1}方法{2}前,, DateTime.Now.ToString(O), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);         invocation.Proceed();         Console.WriteLine({0}:拦截{1}方法{2}后,, DateTime.Now.ToString(O), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);     } }   1 2 3 4 5 6 7 8 9 10 11 static void Main(string[] args) {     using (var container  new WindsorContainer())     {         container.Register(Component.ForPerson, IPerson());         container.Register(Component.ForLogInterceptor, IInterceptor());         var person container.ResolveIPerson();         person.Say();     }     Console.ReadKey(); } 非侵入式AOP 1 2 3 4 5 6 7 8 9 10 11 12 public interface IPerson {     void Say(); } public class Person : IPerson {     public void Say()     {         Console.WriteLine(Persons Say Method is called!);     } }   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 internal static class LogInterceptorRegistrar {     public static void Initialize(WindsorContainer container)     {         container.Kernel.ComponentRegistered Kernel_ComponentRegistered;     }     private static void Kernel_ComponentRegistered(string key, IHandler handler)     {         handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(LogInterceptor)));     } } public class LogInterceptor : IInterceptor {     public void Intercept(IInvocation invocation)     {         Console.WriteLine({0}:拦截{1}方法{2}前,, DateTime.Now.ToString(O), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);         invocation.Proceed();         Console.WriteLine({0}:拦截{1}方法{2}后,, DateTime.Now.ToString(O), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);     } }   1 2 3 4 5 6 7 8 9 10 11 12 static void Main(string[] args) {     using (var container  new WindsorContainer())     {         container.Register(Component.ForIInterceptor, LogInterceptor());//先注入拦截器         LogInterceptorRegistrar.Initialize(container);         container.Register(Component.ForIPerson, Person());         var person container.ResolveIPerson();         person.Say();     }     Console.ReadKey(); }   Autofac Install-Package Autofac.Aop 通过特性标签绑定 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class LogInterceptor : IInterceptor  {      public void Intercept(IInvocation invocation)      {          Console.WriteLine({0}:拦截{1}方法{2}前,, DateTime.Now.ToString(O), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);          invocation.Proceed();          Console.WriteLine({0}:拦截{1}方法{2}后,, DateTime.Now.ToString(O), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);      }  }  public interface IPerson  {      void Say();  }  [Intercept(typeof(LogInterceptor))]  public class Person : IPerson  {      public void Say()      {          Console.WriteLine(Persons Say Method is called!);      }  }   启用拦截器执行 1 2 3 4 5 6 7 8 9 10 11 static void Main(string[] args) {     var builder  new ContainerBuilder();     builder.RegisterTypePerson().AsIPerson().EnableInterfaceInterceptors();     builder.RegisterTypeLogInterceptor();     using (var container builder.Build())     {         container.ResolveIPerson().Say();     }     Console.ReadLine(); }   或采用非侵入性方法(去掉class上的特性仍可以) 1 2 3 4 5 6 7 8 9 10 11 static void Main(string[] args) {     var builder  new ContainerBuilder();     builder.RegisterTypePerson().AsIPerson().EnableInterfaceInterceptors().InterceptedBy(typeof(LogInterceptor));     builder.RegisterTypeLogInterceptor();     using (var container builder.Build())     {         container.ResolveIPerson().Say();     }     Console.ReadLine(); }      Unity Unity默认提供了三种拦截器TransparentProxyInterceptor、InterfaceInterceptor、VirtualMethodInterceptor。 TransparentProxyInterceptor代理实现基于.NET Remoting技术它可拦截对象的所有函数。缺点是被拦截类型必须派生于MarshalByRefObject。 InterfaceInterceptor只能对一个接口做拦截好处时只要目标类型实现了指定接口就可以拦截。 VirtualMethodInterceptor对virtual函数进行拦截。缺点是如果被拦截类型没有virtual函数则无法拦截这个时候如果类型实现了某个特定接口可以改用   Install-Package Unity.Interception 1 2 3 4 5 6 7 8 9 10 11 12 13 public class MyHandler : ICallHandler {     public int Order { get; set; }//这是ICallHandler的成员表示执行顺序     public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)     {         Console.WriteLine(方法执行前);         //这之前插入方法执行前的处理         var retvalue getNext()(input, getNext);//在这里执行方法         //这之后插入方法执行后的处理         Console.WriteLine(方法执行后);         return retvalue;     } }   1 2 3 4 5 6 7 public class MyHandlerAttribute : HandlerAttribute {     public override ICallHandler CreateHandler(IUnityContainer container)     {         return new MyHandler();//返回MyHandler     } }   1 2 3 4 5 6 7 8 9 10 11 12 13 public interface IPerson {     void Say(); } [MyHandler] public class Person : IPerson {     public virtual void Say()     {         Console.WriteLine(Persons Say Method is called!);     } }   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 static void Main(string[] args) {     using (var container  new UnityContainer())     {         container.AddNewExtensionInterception();         //1.TransparentProxyInterceptor         //container.ConfigureInterception().SetInterceptorForIPerson(new TransparentProxyInterceptor());         //2.InterfaceInterceptor (使用1,2,3均可,这种侵入性最小)         container.ConfigureInterception().SetInterceptorForIPerson(new InterfaceInterceptor());         //3.VirtualMethodInterceptor         //container.ConfigureInterception().SetInterceptorForPerson(new VirtualMethodInterceptor());         container.RegisterTypeIPerson, Person();         container.ResolveIPerson().Say();     }     Console.ReadKey(); } 转载于:https://www.cnblogs.com/chenyishi/p/9676011.html
http://www.pierceye.com/news/649901/

相关文章:

  • 如何做免费网站制作郑州网站建设搜索优化
  • 北京网站制作17页谈谈对seo的理解
  • 西安专业建网站网站可信度必须做吗
  • 做神马网站如何做网站的推广
  • 如何提高网站排名的方法建设一个商业网站费用
  • 电商网站平台有哪些做自己的第一个网站
  • 源码资源下载站百度指数 多少流量 网站名
  • 合肥比较好的网站建设公司青阳网站建设
  • 上海地产网站建设甘肃建设厅网站二级建造师报名时间
  • 扬州网站建设推广泊头网站建设甘肃
  • 什么行业要做网站建设推广这些水墨网站设计欣赏
  • 渠道网站wap百度
  • 在网站上如何做天气预报栏wordpress 分类列表
  • 做网站需要投资多少钱做网站的销售团队
  • 苏州哪个公司做门户网站seo优化方案报价
  • 电力建设官方网站做网站送优化
  • 门户网站建设模式包括网站群和中企动力企业邮箱登陆首页
  • 做调查网站的问卷哪个给的钱高wordpress邮箱注册功能
  • 上海php网站开发基于php网站建设
  • 大丰专业做网站做旅游网站当地人服务赚钱吗
  • 长沙网站制作公司推荐seo关键词排名优化
  • 内蒙古住房与城乡建设部网站广州十大软件公司排名
  • 营销型网站 易网拓德阳做网站
  • 网站建设seo虾哥网络购物网站技术实施方案
  • 门户网站框架下载陕西省建设工会网站
  • 网站有信心做的更好做外贸到什么网站上发布比较好
  • wex5做网站wordpress页面的设置
  • 绍兴市建设银行网站网站建设的基本术语
  • 建筑企业网站模板免费下载seo 网站换程序
  • wordpress怎么做排名seo怎么样