Interpretation of a simple aspect framework

Source: Internet
Author: User

Using system;
Using system. collections;
Using system. IO;
Using system. reflection;
Using system. Security. permissions;
Using system. Text. regularexpressions;
Using itabby. Product. efficdevframework. Library;
Using system. runtime. remoting;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. runtime. remoting. activation;
Using system. runtime. remoting. contexts;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. messaging;
Using system. runtime. remoting. proxies;
Using system. runtime. remoting. Services;
Namespace project. dal
{
[Aspectmanaged (true)]
Public class businessobject: contextboundobject
{
Public void method (Object Arg)
{

}
}
Public Enum aspectactionposition {before, after, both}
[Attributeusage (attributetargets. Class)]
[Securitypermissionattribute (securityaction. Demand, flags = securitypermissionflag. Infrastructure)]
Internal class aspectmanagedattriattribute: proxyattribute
{
Private bool aspectmanaged;
Public aspectmanagedattribute ()
{
Aspectmanaged = true;
}
Public aspectmanagedattried (bool aspectmanaged)
{
Aspectmanaged = aspectmanaged;
}
// This method will be called by the. NET Framework when it is instantiated for businessobject
Public override implements albyrefobject createinstance (type servertype)
{
Export albyrefobject mobj = base. createinstance (servertype );
If (aspectmanaged)
{
Realproxy = new aspectproxy (servertype, mobj );
Export albyrefobject retobj = realproxy. gettransparentproxy () as your albyrefobject;
Return retobj;
}
Else
{
Return mobj;
}
}
}
// This type will be called by the. NET Framework when businessobject is instantiated
Internal class aspectproxy: realproxy
{
Public aspectproxy ()
: Base ()
{
}
Public aspectproxy (type mytype)
: Base (mytype)
{

}
Public aspectproxy (type mytype, externalbyrefobject OBJ)
: Base (mytype)
{
Target = OBJ;
}
Marshalbyrefobject target;

Public override iMessage invoke (iMessage MSG)
{
Preprocess (MSG );
IMessage retmsg;
If (MSG is iconstructioncallmessage)
{
Iconstructioncallmessage CCM = (iconstructioncallmessage) MSG;
Remotingservices. getrealproxy (target). initializeserverobject (CCM );
Objref oref = remotingservices. Marshal (target );
Remotingservices. unmarshal (oref );
Retmsg = enterpriseserviceshelper. createconstructionreturnmessage
(CCM, (FIG) This. gettransparentproxy ());
}
Else
{
Imethodcallmessage MCM = (imethodcallmessage) MSG;
Retmsg = remotingservices. executemessage (target, MCM );
}

Postprocess (MSG );
Return retmsg;
}

Private void preprocess (iMessage MSG)
{
Iaspect [] aspectbefore = aspectsmanager. getaspect (MSG, aspectactionposition. Before );
If (aspectbefore! = NULL) & (aspectbefore. length> 0 ))
{
For (INT I = 0; I <aspectbefore. length; I ++)
{
Aspectbefore [I]. Execute (MSG as imethodmessage). ARGs );
}
}
}

Private void postprocess (iMessage MSG)
{
Iaspect [] aspectafter = aspectsmanager. getaspect (MSG, aspectactionposition. After );
If (aspectafter! = NULL) & (aspectafter. length> 0 ))
{
For (INT I = 0; I <aspectafter. length; I ++)
{
Aspectafter [I]. Execute (MSG as imethodmessage). ARGs );
}
}
}
}
Internal class aspectsmanager
{
Static aspectsmanager ()
{

}
Public static iaspect [] getaspect (iMessage MSG, aspectactionposition position)
{
Imethodmessage mmsg = MSG as imethodmessage;
// Return the eventwatcher instance. You can use aspectactionposition to return different monitoring objects.
Return new iaspect [] {New eventwatcher ()};
}
}
Public interface iaspect
{
Void execute (object [] paramlist );
}
Public class eventwatcher: iaspect
{
Public void execute (object [] paramlist)
{
// Obtain the call Parameters
}

}

}

Client code:

Businessobject biz = new businessobject ();
Biz. Method (null );

Running effect:

The execute method of eventwatcher will be called four times.

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.