程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> AOP的兩個應用(上)(2)

AOP的兩個應用(上)(2)

編輯:關於C語言

EntityListUpdatableProperty類

//*******************************************************************
// 模塊:實體集更新的上下文屬性
// 日期:2009-9-19 14:24:24
// 作者:Faib
// 版權:Copyright Faib Studio 2009
// 官網:http://www.faib.Net.cn
// 郵箱:[email protected]
// 備注:
//*******************************************************************
using System;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Contexts;
namespace FaibClass.Data.ASPect
{
    /// <summary>
    /// 實體集更新的上下文屬性。
    /// </summary>
    internal class EntityListUpdatableProperty : IContextProperty, IContributeObjectSink
    {
        void IContextProperty.Freeze(Context newContext)
        {
        }
        string IContextProperty.Name
        {
            get { return EntityListUpdatableAttribute.propertyName; }
        }
        bool IContextProperty.IsNewContextOK(Context newCtx)
        {
            EntityListUpdatableProperty property = 
                newCtx.GetProperty(EntityListUpdatableAttribute.propertyName) as EntityListUpdatableProperty;
            if (property == null)
            {
                return false;
            }
            return true;
        }
        IMessageSink IContributeObjectSink.GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
        {
            IMessageSink sink = new EntityListUpdatableSink(obj, nextSink);
            return sink;
        }
    }
}

EntityListUpdatableSink類

//*******************************************************************
// 模塊:用於處理屬性修改後的消息接收器
// 日期:2009-9-19 14:24:12
// 作者:Faib
// 版權:Copyright Faib Studio 2009
// 官網:http://www.faib.Net.cn
// 郵箱:[email protected]
// 備注:
//*******************************************************************
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. 下一頁:
Copyright © 程式師世界 All Rights Reserved