C # AOP Micro-Framework implementation (i)

Source: Internet
Author: User

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

}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.