中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
C# AOP微型框架实现(一)
作者:未知 时间:2005-07-01 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无

在前面的系列文章中,我介绍了消息、代理与AOP的关系,这次将我自己实现的一个AOP微型框架拿出来和大家交流一下。

AOP的最基本功能就是实现特定的预处理和后处理,我通过代理实现了此微型框架。

先来看看构成此微型框架的4个.cs文件。

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

/************************************* CommonDef.cs **************************

using System;
using System.Runtime.Remoting.Messaging ;

namespace EnterpriseServerBase.Aop
{
 /// <summary>
 /// IAopOperator AOP操作符接口,包括前处理和后处理
 /// 2005.04.12
 /// </summary>
 public interface IAopOperator
 {
  void PreProcess(IMessage requestMsg ) ;
  void PostProcess(IMessage requestMsg ,IMessage Respond) ;
 }


 /// <summary>
 /// IAopProxyFactory 用于创建特定的Aop代理的实例,IAopProxyFactory的作用是使AopProxyAttribute独立于具体的AOP代理类。
 /// </summary>
 public interface IAopProxyFactory
 {
  AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;
 }

}

2. AopProxyBase  AOP代理的基类,所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。

using System;
using System.Runtime.Remoting ;
using System.Runtime.Remoting.Proxies ;
using System.Runtime.Remoting.Messaging ;
using System.Runtime.Remoting.Services ;
using System.Runtime.Remoting.Activation ;

namespace EnterpriseServerBase.Aop
{
 /// <summary>
 /// AopProxyBase 所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。
 /// 2005.04.12
 /// </summary>
 public abstract class AopProxyBase :  RealProxy ,IAopOperator
 {
  private readonly MarshalByRefObject target ; //默认透明代理
  
  public AopProxyBase(MarshalByRefObject obj ,Type type) :base(type)
  {
   this.target = obj ;
  }
  
  #region Invoke
  public override IMessage Invoke(IMessage msg)
  {
   bool useAspect = false ;
   IMethodCallMessage call = (IMethodCallMessage)msg ;

   //查询目标方法是否使用了启用AOP的MethodAopSwitcherAttribute
   foreach(Attribute attr in call.MethodBase.GetCustomAttributes(false))
   {
    MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;
    if(mehodAopAttr != null)
    {
     if(mehodAopAttr.UseAspect)
     {
      useAspect = true ;
      break ;
     }
    }
   }

   if(useAspect)
   {
    this.PreProcess(msg) ;
   }

   //如果触发的是构造函数,此时target的构建还未开始
   IConstructionCallMessage ctor = call as IConstructionCallMessage ;
   if(ctor != null)
   {
    //获取最底层的默认真实代理
    RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;

    default_proxy.InitializeServerObject(ctor) ;
    MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; //自定义的透明代理 this

    return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);    
   }   
   
   IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ; //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息
   
   if(useAspect)
   {
    this.PostProcess(msg ,result_msg) ;
   }

   return result_msg ;

  }
  #endregion

  #region IAopOperator 成员

  public abstract void PreProcess(IMessage requestMsg) ;
  public abstract void PostProcess(IMessage requestMsg, IMessage Respond) ;
  #endregion

 }

}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有