本篇內(nèi)容介紹了“C#怎么實(shí)現(xiàn)AOP微型框架”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),靖州企業(yè)網(wǎng)站建設(shè),靖州品牌網(wǎng)站建設(shè),網(wǎng)站定制,靖州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,靖州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
在向大家詳細(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接口
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 用于創(chuàng)建特定的Aop代理的實(shí)例,
IAopProxyFactory的作用是使AopProxyAttribute獨(dú)立于具體的AOP代理類(lèi)。/// </summary>
public interface IAopProxyFactory
{
AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;
}
}
2. AopProxyBase AOP代理的基類(lèi)
所有自定義AOP代理類(lèi)都從此類(lèi)派生,覆寫(xiě)IAopOperator接口,實(shí)現(xiàn)具體的前/后處理 。
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代理類(lèi)都從此類(lèi)派生,
覆寫(xiě)IAopOperator接口,實(shí)現(xiàn)具體的前/后處理 。/// 2005.04.12
/// </summary>
public abstract class AopProxyBase : RealProxy ,IAopOperator
{
private readonly MarshalByRefObject target ; //默認(rèn)透明代理
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 ;
//查詢(xún)目標(biāo)方法是否使用了啟用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) ;
}
//如果觸發(fā)的是構(gòu)造函數(shù),此時(shí)target的構(gòu)建還未開(kāi)始
IConstructionCallMessage ctor = call as IConstructionCallMessage ;
if(ctor != null)
{
//獲取***層的默認(rèn)真實(shí)代理
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) ;
//將消息轉(zhuǎn)化為堆棧,并執(zhí)行目標(biāo)方法,方法完成后,再將堆棧轉(zhuǎn)化為消息
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
}
}
“C#怎么實(shí)現(xiàn)AOP微型框架”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前標(biāo)題:C#怎么實(shí)現(xiàn)AOP微型框架
標(biāo)題來(lái)源:http://jinyejixie.com/article40/ggcgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站設(shè)計(jì)、虛擬主機(jī)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)