WEB开发网
开发学院软件开发C语言 AOP的两个应用:实体集更新(DateEntityListUpdate... 阅读

AOP的两个应用:实体集更新(DateEntityListUpdate)、延迟加载(LazyLoad)(上)

 2010-09-30 21:02:56 来源:WEB开发网   
核心提示: EntityListUpdatableSink类//***//模块:用于处理属性修改后的消息接收器//日期:2009-9-1914:24:12//作者:Faib//版权:CopyrightFaibStudio2009//官网:http://www.faib.net.cn//邮箱:faib920@

EntityListUpdatableSink类

//*******************************************************************
// 模块:用于处理属性修改后的消息接收器
// 日期:2009-9-19 14:24:12
// 作者:Faib
// 版权:Copyright Faib Studio 2009
// 官网:http://www.faib.net.cn
// 邮箱:faib920@126.com
// 备注:
//*******************************************************************
using System;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
namespace FaibClass.Data.Aspect
{
    /// <summary>
    /// 用于处理属性修改后的消息接收器。
    /// </summary>
    internal class EntityListUpdatableSink : IMessageSink
    {
        private IMessageSink m_nextSink;
        private MarshalByRefObject m_target;
        private static object syncRoot = new object();
        public EntityListUpdatableSink(MarshalByRefObject target, IMessageSink nextSink)
        {
            lock (syncRoot)
            {
                m_target = target;
                m_nextSink = nextSink;
            }
        }
        public IMessage SyncProcessMessage(IMessage msg)
        {
            IMethodReturnMessage returnedMessage;
            HandleMessage(msg, false, out returnedMessage);
            return returnedMessage;
        }
        public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            IMethodReturnMessage returnedMessage;
            HandleMessage(msg, true, out returnedMessage);
            return m_nextSink.AsyncProcessMessage(msg, replySink);
        }
        public IMessageSink NextSink
        {
            get { return m_nextSink; }
        }
        private void HandleMessage(IMessage msg, bool IsAsync, out IMethodReturnMessage returnedMessage)
        {
            returnedMessage = null;
            if (!IsAsync)
            {
                if (msg is IMethodCallMessage)
                {
                    IMethodCallMessage mcm = (IMethodCallMessage)msg;
                    bool isFill = (bool)m_target.GetType().GetProperty("InnerIsFill", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(m_target, null);
                    
                    bool isProperty = !isFill && ((mcm.MethodName.Length > 4 &&
                        mcm.MethodName.Substring(0, 4) == "set_" && mcm.MethodName != "set_InnerIsFill" &&
                        mcm.MethodName != "set_InnerDataState" &&
                        mcm.MethodName != "set_InnerData") || mcm.MethodName == "FieldSetter");
                    object oldvalue = null, newvalue = null;
                    MemberInfo minfo = null;
                    string propertyName = string.Empty;
                    //取原来的值
                    if (isProperty)
                    {
                        if (mcm.MethodName == "FieldSetter")
                            propertyName = mcm.InArgs[1].ToString();
                        else
                            propertyName = mcm.MethodName.Replace("set_", "");
                        minfo = Utility.GetMember(m_target.GetType(), propertyName);
                        if (minfo == null)
                            isProperty = false;
                        else if (minfo.IsDefined(typeof(DataColumnAttribute), true))
                            oldvalue = Utility.GetMemberValue(m_target, minfo, propertyName);
                    }
                    returnedMessage = (IMethodReturnMessage)m_nextSink.SyncProcessMessage(msg);
                    
                    if (isProperty)
                    {
                        //新的值
                        newvalue = Utility.GetMemberValue(m_target, minfo, propertyName);
                        if (!newvalue.Equals(oldvalue))
                        {
                            //调用内部方法添加修改了的列
                            m_target.GetType().InvokeMember("AddUpdateColumn", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, m_target, new object[] { mcm.MethodName.Replace("set_", "") });
                        }
                    }
                }
                else
                {
                    returnedMessage = (IMethodReturnMessage)m_nextSink.SyncProcessMessage(msg);
                }
            }
            else
            {
                returnedMessage = null;
            }
        }
    }
}

上一页  1 2 3 4 5  下一页

Tags:AOP 两个 应用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接