av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

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

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

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

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

2. AopProxyBase AOP代理的基類

所有自定義AOP代理類都從此類派生,覆寫IAopOperator接口,實現(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. ///  
  11. /// AopProxyBase 所有自定義AOP代理類都從此類派生,
    覆寫IAopOperator接口,實現(xiàn)具體的前/后處理 。  
  12. /// 2005.04.12  
  13. ///  
  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ù),此時target的構(gòu)建還未開始  
  49. IConstructionCallMessage ctor = call as IConstructionCallMessage ;  
  50. if(ctor != null)  
  51. {  
  52. //獲取***層的默認(rèn)真實代理  
  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)建表單簡單介紹
  2. C#修改DataReader默認(rèn)行為
  3. C#設(shè)置CooperativeLevel概述
  4. C#表單增加控件簡單描述
  5. C# EmployeePlug類概述

當(dāng)前標(biāo)題:C#實現(xiàn)AOP微型框架深入剖析
URL網(wǎng)址:http://uogjgqi.cn/article/dheccpi.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流