Lightweight. NET object Lookup service and AOP development Framework Netop.core Source Commentary (4)--AOP

Source: Internet
Author: User

The above section discusses the class factory/object lookup service, this section talks about the implementation of AOP.

AOP is the abbreviation of Aspect oriented programming, which means: face-cutting programming, through the pre-compilation method and run-time dynamic agent implementation of the unified maintenance of the program functions of a technology.

Netop.core's AOP uses the proxy implementation. With proxy, your class must inherit a base class (not the default object, of course, if your class already has a parent class that can inherit from that class's parent)--

ContextBoundObject or Netop.Core.Aspect.AspectObject. Of course Netop.Core.Aspect.AspectObject is inherited ContextBoundObject.

Netop.core recommended interface for AOP processing

AOP Pre-processing recommendations
public interface Ibeforeadvisor:iaspect
{
void Preprocess (IMessage msg);
}

AOP post-processing recommendations
public interface Iafteradvisor:iaspect
{
void PostProcess (IMethodReturnMessage msg);
}

AOP Exception Recommendations (actually post-processing interfaces)
public interface Iexceptionadvisor:iafteradvisor
{
}

AOP Pre/Post processing recommendations
Public interface Iaroundadvisor:ibeforeadvisor, Iafteradvisor
{
}

Netop.core's AOP proxy class is the Netop.Core.Aspect.AspectProxy that inherits RealProxy, and it wants to rewrite the IMessage Invoke (IMessage msg) method. By the way, remote service is a rewrite

The RealProxy IMessage Invoke (IMessage msg) method is implemented. This, of course, is also a way to implement AOP.

The above mentioned the interface and proxy class, the following first talk about the configuration. Remember the configuration of the Netop.core class factory/object lookup service?
<Application.ObjectLocator>
<defaultappobjectlocatorservice>netop.core.locatorservice.defaultappobjectlocatorservice,netop.core</ Defaultappobjectlocatorservice>
<ObjectServiceAgentName>AppObjectService</ObjectServiceAgentName>
<AspectAgentName>AppAspect</AspectAgentName>
</Application.ObjectLocator>
<Application.Agent>
<agent name= "Appobjectservice" type= "Netop.core.configuration.fileagentconfiguration,netop.core" >
<File>Service.xml</File>
</Agent>
<agent name= "Appaspect" type= "Netop.core.configuration.fileagentconfiguration,netop.core" >
<File>Aspect.xml</File>
</Agent>
</Application.Agent>
</Application.ObjectLocator>

<AspectAgentName>AppAspect</AspectAgentName> is the configuration of the AOP service, and the Appaspect name corresponds to the Application.agent node agent Name= Information on "Appaspect":

<agent name= "Appaspect" type= "Netop.core.configuration.fileagentconfiguration,netop.core" >
<File>Aspect.xml</File>
</Agent>

View the contents of the AspectConfiguration.cs file, Appaspectconfigurationmanager class by calling AppObjectLocatorConfigurationManager.Current.AspectAgentNa Me get Aspectagentname

The corresponding configuration proxy name, and then by invoking the Configure Proxy service to get aspect.xml content. The contents of the test program Aspect.xml are as follows:
<Application.Aspect>
<object name= "A0" type= "test.core.myadvisor,test.core" Issingleton = "true" pointcut= "construction| method| Property "Match=" *,he*,* "/>
</Application.Aspect>

Name: Interceptor name, no special meaning, as long as the only line;
Type: The full name of the Interceptor class;
Issingleton: Whether this interceptor is a singleton, with a value of "true" or "1" or "yes", or a single case. Single example can improve performance, not set the default is a singleton;
Pointcut: There are three types of interception: method, constructor (construction), and property; multiple combinations with "|" Separated.
Match: matches the rule. Rules can be defined on class names, methods, and properties. The class name, method, and attribute are separated by the rules of the three. In the case of Match= "*,get*,*", the match of the class name is "*" before the first comma, and the method matches the second

A comma before the "get*", the property's match is "*" after the second comma.
Match of Class name: A class that conforms to the true time of the regular expression match;
Method Matching: If no method in Pointcut is not matched, if there is a method in Pointcut, it is the way to match the regular expression to true.
Property matching: No property in pointcut, such as if there is an attribute in the Pointcut, which is true when the regular expression matches.

Clear the basic configuration, through the test procedures to explain the use and source code commentary:

Namespace Test.core
{
public interface Iservice:idisposable
{
void Hello ();
}

public class Service3:aspectobject, IService
{
public void Hello ()
{
Console.WriteLine ("");
Console.WriteLine ("Do3 Begin");
Console.WriteLine ("Do3 ...");
Console.WriteLine (this. GetType (). FullName);
Console.WriteLine ("Do3 End");
Console.WriteLine ("");
}

public void Dispose ()
{
Console.WriteLine ("Service3:dispose");
}
}

public class Myadvisor:iaroundadvisor
{
public void Preprocess (IMessage msg)
{
Console.WriteLine ("Preprocess ...");
}

public void PostProcess (IMethodReturnMessage msg)
{
Console.WriteLine ("PostProcess ...");
}
}
}

Class Service3 inherits the Aspectobject, the Interceptor class is myadvisor, and the Iaroundadvisor method is implemented.

Aspect.xml to configure the Interceptor class Myadvisor:

<Application.Aspect>
<object name= "A0" type= "test.core.myadvisor,test.core" Issingleton = "true" pointcut= "construction| method| Property "Match=" *,he*,* "/>
</Application.Aspect>

Service.xml Configure the class Service3:

<Application.ObjectService>
<object name= "A3" type= "Test.core.service3,test.core" isaspect= "1" >
</Object>
</Application.ObjectService>

Note: It is possible to activate the AOP service channel when the Isaspect property is added with a value of "true" or "1" or "yes" (after entering this channel, the actual execution depends on the matching problem of the interceptor configuration).

When performing iservice s3 = Netop.Core.LocatorService.AppObjectLocatorManager.GetObject ("A3") as IService The Defaultappobjectlocatorservice method is called when the GetObject

, when the isaspect of "A3" (Service3) is found to be true, and the Service3 class is inherited from Netop.Core.Aspect.AspectObject (MarshalByRefObject), a transparent proxy is obtained,

Defaultappobjectlocatorservice Related codes are:

o = typeutil.createinstance (t);
if (OS. Isaspect)
{
If (O is MarshalByRefObject)
{
RealProxy RealProxy = new Aspectproxy (t, (MarshalByRefObject) o);
o = Realproxy.gettransparentproxy () as MarshalByRefObject;
}
}

Creating TransparentProxy for client code through a custom RealProxy is realproxy taken over for every call through TransparentProxy, so we add the Invoke method in RealProxy

Code is called every time the method is called.

The code of the Invoke method in Aspectproxy is not described in detail, the main logic is to obtain a matching interceptor first, and then carry out the pre-processing, the processing of the message itself, post-processing and other actions.

The above-mentioned activation of the AOP service channel is to set the Isaspect property, which is a more flexible one:
<Application.ObjectService>
<object name= "A3" type= "Test.core.service3,test.core" isaspect= "1" >
</Object>
</Application.ObjectService>

Another very inflexible approach is not to configure Service.xml, but to add attributes Aspectattribute to the corresponding class, such as:
[Aspectattribute]
public class Service3:aspectobject, IService
{
public void Hello ()
{
Console.WriteLine ("");
Console.WriteLine ("Do3 Begin");
Console.WriteLine ("Do3 ...");
Console.WriteLine (this. GetType (). FullName);
Console.WriteLine ("Do3 End");
Console.WriteLine ("");
}

public void Dispose ()
{
Console.WriteLine ("Service3:dispose");
}
}

This is an option, but it is not recommended to use this. Interested can look at the Aspectattribute code.

Netop.core's AOP is well integrated with object lookup services, and the code is simple enough to achieve the goal of micro-level.

Lightweight. NET Object Lookup Service and AOP development Framework source netop.core3.5:http://download.csdn.net/detail/tom_cat_xie_jxdy/9837303

Lightweight. Net Object Lookup Service and AOP Development Framework test Source: http://download.csdn.Net/detail/tom_cat_xie_jxdy/9837278

netop.core--lightweight. NET Object Lookup service and AOP Development Framework Documentation: HTTP://DOWNLOAD.CSDN.NET/DETAIL/TOM_CAT_XIE_JXDY/9838212


Lightweight. NET object Lookup service and AOP development Framework Netop.core Source Commentary (4)--AOP

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.