一个简单的Aspect框架解读

using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using Itabby.Product.EfficDevFramework.Library;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace Project.DAL
{
    [AspectManaged(true)]
    public class BusinessObject : ContextBoundObject
    {
        public void Method(object arg)
        {
           
        }
    }
    public enum AspectActionPosition { Before, After, Both }
    [AttributeUsage(AttributeTargets.Class)]
    [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
    internal class AspectManagedAttribute : ProxyAttribute
    {
        private bool aspectManaged;
        public AspectManagedAttribute()
        {
            aspectManaged = true;
        }
        public AspectManagedAttribute(bool AspectManaged)
        {
            aspectManaged = AspectManaged;
        }
        //本方法将在为BusinessObject实例化时被.net框架调用
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            MarshalByRefObject mobj = base.CreateInstance(serverType);
            if (aspectManaged)
            {
                RealProxy realProxy = new AspectProxy(serverType, mobj);
                MarshalByRefObject retobj = realProxy.GetTransparentProxy() as MarshalByRefObject;
                return retobj;
            }
            else
            {
                return mobj;
            }
        }
    }
    //本类型将在BusinessObject实例化时被.net框架调用
    internal class AspectProxy : RealProxy
    {
        public AspectProxy()
            : base()
        {
        }
        public AspectProxy(Type myType)
            : base(myType)
        {

        }
        public AspectProxy(Type myType, MarshalByRefObject obj)
            : base(myType)
        {
            target = obj;
        }
        MarshalByRefObject target;

        public override IMessage Invoke(IMessage msg)
        {
            PreProcess(msg);
            IMessage retMsg;
            if (msg is IConstructionCallMessage)
            {
                IConstructionCallMessage ccm = (IConstructionCallMessage)msg;
                RemotingServices.GetRealProxy(target).InitializeServerObject(ccm);
                ObjRef oRef = RemotingServices.Marshal(target);
                RemotingServices.Unmarshal(oRef);
                retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage
                    (ccm, (MarshalByRefObject)this.GetTransparentProxy());
            }
            else
            {
                IMethodCallMessage mcm = (IMethodCallMessage)msg;
                retMsg = RemotingServices.ExecuteMessage(target, mcm);
            }

            PostProcess(msg);
            return retMsg;
        }

        private void PreProcess(IMessage msg)
        {
            IAspect[] aspectBefore = AspectsManager.GetAspect(msg, AspectActionPosition.Before);
            if ((aspectBefore != null) && (aspectBefore.Length > 0))
            {
                for (int i = 0; i < aspectBefore.Length; i++)
                {
                    aspectBefore[i].Execute((msg as IMethodMessage).Args);
                }
            }
        }

        private void PostProcess(IMessage msg)
        {
            IAspect[] aspectAfter = AspectsManager.GetAspect(msg, AspectActionPosition.After);
            if ((aspectAfter != null) && (aspectAfter.Length > 0))
            {
                for (int i = 0; i < aspectAfter.Length; i++)
                {
                    aspectAfter[i].Execute((msg as IMethodMessage).Args);
                }
            }
        }
    }
    internal class AspectsManager
    {
        static AspectsManager()
        {

        }
        public static IAspect[] GetAspect(IMessage msg, AspectActionPosition position)
        {
            IMethodMessage mmsg = msg as IMethodMessage;
            //返回EventWatcher实例,可通过AspectActionPosition来分别返回不同的监视对象
            return new IAspect[] { new EventWatcher() };
        }
    }
    public interface IAspect
    {
        void Execute(object[] paramList);
    }
    public class EventWatcher : IAspect
    {
        public void Execute(object[] paramList)
        {
            //获取调用参数
        }

    }

}

客户端编码:

BusinessObject biz = new BusinessObject();
biz.Method(null);

运行效果:

EventWatcher的Execute方法将被调用4次.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值