In the previous series, I introduced the message, the relationship between agent and AOP, and this time I took out an AOP micro-framework that I realized and shared with you.
The most basic function of AOP is to implement specific preprocessing and reprocessing, and I have implemented this micro framework through proxies.
Let's take a look at the 4. cs files that make up this micro-frame.
1.commondef.cs is used to define the most basic AOP interfaces
/************************************* CommonDef.cs **************************
Using System;
Using System.Runtime.Remoting.Messaging;
Namespace ENTERPRISESERVERBASE.AOP
{
<summary>
Iaopoperator AOP operator interface, including pre-processing and post-processing
2005.04.12
</summary>
public interface Iaopoperator
{
void Preprocess (IMessage requestmsg);
void PostProcess (IMessage requestmsg, IMessage Respond);
}
<summary>
Iaopproxyfactory is used to create instances of specific AOP proxies, Iaopproxyfactory is designed to make Aopproxyattribute independent of the specific AOP proxy classes.
</summary>
public interface Iaopproxyfactory
{
Aopproxybase createaopproxyinstance (MarshalByRefObject obj, type type);
}
}
2. Aopproxybase the base class for AOP agents, all custom AOP proxy classes derive from this class, overwrite the Iaopoperator interface, and implement specific pre/post processing.
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 All custom AOP proxy classes derive from this class, overwrite the Iaopoperator interface, and implement specific pre/post processing.
/// 2005.04.12
///</summary>
public abstract class Aopproxybase: RealProxy, Iaopoperator
{
private readonly MarshalByRefObject target;//default transparent proxy
&NBSP
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;
Query whether the target method uses an AOP-enabled 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);
}
If the constructor is triggered, then target construction has not started
IConstructionCallMessage ctor = call as IConstructionCallMessage;
if (ctor!= null)
{
Get the lowest-level default real Agent
RealProxy default_proxy = Remotingservices.getrealproxy (this.target);
Default_proxy. Initializeserverobject (ctor);
MarshalByRefObject TP = (MarshalByRefObject) this. Gettransparentproxy (); Custom Transparent Proxy This
Return Enterpriseserviceshelper.createconstructionreturnmessage (CTOR,TP);
}
IMethodReturnMessage result_msg = Remotingservices.executemessage (this.target, call); Converts a message to a stack and executes the target method, after which the stack is converted to a message
if (Useaspect)
{
This. PostProcess (msg, result_msg);
}
return result_msg;
}
#endregion
#region Iaopoperator Members
public abstract void Preprocess (IMessage requestmsg);
public abstract void PostProcess (IMessage requestmsg, IMessage Respond);
#endregion
}
}