自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#實(shí)現(xiàn)AOP微型框架深入剖析

開(kāi)發(fā) 后端
這里介紹C#實(shí)現(xiàn)AOP微型框架,AOP的最基本功能就是實(shí)現(xiàn)特定的預(yù)處理和后處理,我通過(guò)代理讓C#實(shí)現(xiàn)AOP微型框架。先來(lái)看看構(gòu)成此微型框架的.cs文件。

在向大家詳細(xì)介紹C#實(shí)現(xiàn)AOP微型框架之前,首先讓大家了解下微型框架的.cs文件,然后全面介紹C#實(shí)現(xiàn)AOP微型框架。

在前面的系列文章中,我介紹了消息、代理與AOP的關(guān)系,這次將我自己用C#實(shí)現(xiàn)AOP微型框架拿出來(lái)和大家交流一下。

AOP的最基本功能就是實(shí)現(xiàn)特定的預(yù)處理和后處理,我通過(guò)代理讓C#實(shí)現(xiàn)AOP微型框架。先來(lái)看看構(gòu)成此微型框架的.cs文件。

1.CommonDef.cs 用于定義最基本的AOP接口

  1. using System;  
  2. using System.Runtime.Remoting.Messaging ;  
  3.  
  4. namespace EnterpriseServerBase.Aop  
  5. {  
  6. /// <summary> 
  7. /// IAopOperator AOP操作符接口,包括前處理和后處理  
  8. /// 2005.04.12  
  9. /// </summary> 
  10. public interface IAopOperator  
  11. {  
  12. void PreProcess(IMessage requestMsg ) ;  
  13. void PostProcess(IMessage requestMsg ,IMessage Respond) ;  
  14. }  
  15.  
  16. /// <summary> 
  17. /// IAopProxyFactory 用于創(chuàng)建特定的Aop代理的實(shí)例,
    IAopProxyFactory的作用是使AopProxyAttribute獨(dú)立于具體的AOP代理類(lèi)。  
  18. /// </summary> 
  19. public interface IAopProxyFactory  
  20. {  
  21. AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;  
  22. }  
  23.  

2. AopProxyBase AOP代理的基類(lèi)

所有自定義AOP代理類(lèi)都從此類(lèi)派生,覆寫(xiě)IAopOperator接口,實(shí)現(xiàn)具體的前/后處理 。

  1. using System;  
  2. using System.Runtime.Remoting ;  
  3. using System.Runtime.Remoting.Proxies ;  
  4. using System.Runtime.Remoting.Messaging ;  
  5. using System.Runtime.Remoting.Services ;  
  6. using System.Runtime.Remoting.Activation ;  
  7.  
  8. namespace EnterpriseServerBase.Aop  
  9. {  
  10. /// <summary> 
  11. /// AopProxyBase 所有自定義AOP代理類(lèi)都從此類(lèi)派生,
    覆寫(xiě)IAopOperator接口,實(shí)現(xiàn)具體的前/后處理 。  
  12. /// 2005.04.12  
  13. /// </summary> 
  14. public abstract class AopProxyBase : RealProxy ,IAopOperator  
  15. {  
  16. private readonly MarshalByRefObject target ; //默認(rèn)透明代理  
  17.  
  18. public AopProxyBase(MarshalByRefObject obj ,Type type) :base(type)  
  19. {  
  20. this.target = obj ;  
  21. }  
  22.  
  23. #region Invoke  
  24. public override IMessage Invoke(IMessage msg)  
  25. {  
  26. bool useAspect = false ;  
  27. IMethodCallMessage call = (IMethodCallMessage)msg ;  
  28.  
  29. //查詢目標(biāo)方法是否使用了啟用AOP的MethodAopSwitcherAttribute  
  30. foreach(Attribute attr in call.MethodBase.GetCustomAttributes(false))  
  31. {  
  32. MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;  
  33. if(mehodAopAttr != null)  
  34. {  
  35. if(mehodAopAttr.UseAspect)  
  36. {  
  37. useAspect = true ;  
  38. break ;  
  39. }  
  40. }  
  41. }  
  42.  
  43. if(useAspect)  
  44. {  
  45. this.PreProcess(msg) ;  
  46. }  
  47.  
  48. //如果觸發(fā)的是構(gòu)造函數(shù),此時(shí)target的構(gòu)建還未開(kāi)始  
  49. IConstructionCallMessage ctor = call as IConstructionCallMessage ;  
  50. if(ctor != null)  
  51. {  
  52. //獲取***層的默認(rèn)真實(shí)代理  
  53. RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;  
  54.  
  55. default_proxy.InitializeServerObject(ctor) ;  
  56. MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; 
  57. //自定義的透明代理 this  
  58.  
  59. return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);  
  60. }  
  61.  
  62. IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ; 
  63. //將消息轉(zhuǎn)化為堆棧,并執(zhí)行目標(biāo)方法,方法完成后,再將堆棧轉(zhuǎn)化為消息  
  64.  
  65. if(useAspect)  
  66. {  
  67. this.PostProcess(msg ,result_msg) ;  
  68. }  
  69.  
  70. return result_msg ;  
  71.  
  72. }  
  73. #endregion  
  74.  
  75. #region IAopOperator 成員  
  76.  
  77. public abstract void PreProcess(IMessage requestMsg) ;  
  78. public abstract void PostProcess(IMessage requestMsg, IMessage Respond) ;  
  79. #endregion  
  80. }  

【編輯推薦】

  1. C#創(chuàng)建表單簡(jiǎn)單介紹
  2. C#修改DataReader默認(rèn)行為
  3. C#設(shè)置CooperativeLevel概述
  4. C#表單增加控件簡(jiǎn)單描述
  5. C# EmployeePlug類(lèi)概述
責(zé)任編輯:佚名 來(lái)源: 博客園
相關(guān)推薦

2009-09-03 15:38:54

C#實(shí)現(xiàn)AOP微型框架

2009-09-02 13:36:58

C#實(shí)現(xiàn)多個(gè)接口

2009-09-11 11:09:36

C#引用類(lèi)型

2009-09-02 18:14:33

C# WebClien

2009-09-04 17:56:22

C#刪除數(shù)據(jù)

2009-09-03 17:42:07

C#開(kāi)發(fā)CF藍(lán)牙模塊

2009-09-29 10:00:40

Spring AOP框

2009-09-04 17:49:34

C#連接數(shù)據(jù)庫(kù)

2015-07-28 10:06:03

C#內(nèi)部實(shí)現(xiàn)剖析

2009-09-10 17:37:01

C# get post

2009-08-28 15:38:49

C#實(shí)現(xiàn)斷點(diǎn)續(xù)傳

2009-09-07 14:29:52

C# ServiceC

2009-08-27 17:14:36

C# Socket

2009-09-01 16:29:03

QuickSort C

2024-12-16 00:50:56

2010-02-06 16:05:51

C++ Vector

2009-09-01 11:04:59

C#調(diào)用擴(kuò)展方法

2009-09-11 11:17:04

C#引用類(lèi)型

2009-08-27 16:29:18

C#動(dòng)態(tài)編譯

2009-08-28 10:44:46

C#字符數(shù)組轉(zhuǎn)換
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)