日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#實(shí)現(xiàn)AOP微型框架深入剖析

在向大家詳細(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. ///  
  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代理的實(shí)例,
    IAopProxyFactory的作用是使AopProxyAttribute獨(dú)立于具體的AOP代理類。  
  18. ///  
  19. public interface IAopProxyFactory  
  20. {  
  21. AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;  
  22. }  
  23.  

2. AopProxyBase AOP代理的基類

所有自定義AOP代理類都從此類派生,覆寫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. ///  
  11. /// AopProxyBase 所有自定義AOP代理類都從此類派生,
    覆寫IAopOperator接口,實(shí)現(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ù),此時(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類概述

本文名稱:C#實(shí)現(xiàn)AOP微型框架深入剖析
網(wǎng)站鏈接:http://www.dlmjj.cn/article/dheccpi.html