DOCX

TheThe.net languages of AOP implementation steps

By Kyle Torres,2015-08-02 08:10
8 views 0
TheThe.net languages of AOP implementation steps

    The.net languages of AOP implementation steps

    Article mainly explained in the development of application system. All.net languages of AOP implementation.LogAspect complete the main functions are going to Advice and method to establish the mapping of business object, and add it to the Advice in the collection.Because we in the AOP implementation, using the XML configuration file to configure the PointCut, so for all Aspect, these actions are the same, as long as to define the correct configuration file, read it.In the Aspect of SyncProcessMessage (), as a result of interception and weave method is the same, different is only the logical Advice, so in all aspects of public already provides a default implementation in the base class:

    public class LogAspect:Aspect

    {

    public LogAspect(IMessageSink nextSink): base (nextSink)

    {}

    }

然后定义正确的配置文件?

< aspect value = " LogAOP " >

    < advice type = " before " assembly = " AOP.Advice " class = "

    AOP.Advice.LogAdvice " >

    < pointcut > ADD

    < pointcut > SUBSTRACT

    

    < advice type = " after " assembly = " AOP.Advice " class = " AOP.Advice.LogAdvice " >

    < pointcut > ADD

    < pointcut > SUBSTRACT

    

    

    LogAdvice belongs to the assembly file for AOP) Advice. DLL, the full class called AOP) Advice. LogAdvice.

    Log Advice (LogAdvice)

    Because need to record log method call before and after the related data, therefore LogAdvice should realize IBeforeAdvice and IAfterAdvice interface at the same time:

    public class LogAdvice:IAfterAdvice,IBeforeAdvice {

    #region IBeforeAdvice Members

    public void BeforeAdvice(IMethodCallMessage callMsg) {

    if (callMsg == null )

    {

    return ;

    }

    Console.WriteLine( " {0}({1},{2}) " ,

    callMsg.MethodName, callMsg.GetArg( 0 ),

    callMsg.GetArg( 1 ));

    }

    #endregion

#region IAfterAdvice Members

    public void AfterAdvice(IMethodReturnMessage returnMsg) {

    if (returnMsg == null )

    {

    return ;

    }

    Console.WriteLine( " Result is {0} " , returnMsg.ReturnValue); }

    #endregion

    }

    In BeforeAdvice () method, message type for IMethodCallMessage, through the

    interface object, you can get the method name and parameter value method call.In contrast,

    AfterAdvice () method of message type for IMethodReturnMessage, Advice to obtain data for

    the method return value ReturnValue.

    Performance monitoring

    The realization of the performance monitoring and log is roughly same, for simplicity,

    I'm just in order to realize the performance of the monitoring record method call before and

    after call time.

    Performance monitoring Attribute (MonitorAOPAttribute)

    Same as the log Attribute, MonitorAOPAttribute only need to create and return the

    corresponding MonitorAOPProperty objects:

    [AttributeUsage(AttributeTargets.Class)]

    public class MonitorAOPAttribute:AOPAttribute {

    public MonitorAOPAttribute(): base ()

    {}

    public MonitorAOPAttribute( string aspectXml): base (aspectXml) {}

    protected override AOPProperty GetAOPProperty() {

    return new MonitorAOPProperty();

    }

    }

    Performance monitoring Property (MonitorAOPProperty)

    MonitorAOPProperty attribute name defined as MonitorAOP, distinguish its attributes

    and logging.In addition to the defined performance monitoring of the property name, also

    needs to be rewritten CreateAspect () method, create MonitorAspect object and returns the

    corresponding aspects:

    public class MonitorAOPProperty:AOPProperty {

    protected override IMessageSink CreateAspect (IMessageSink nextSink)

    {

    return new MonitorAspect(nextSink);

    }

    protected override string GetName()

    {

    return " MonitorAOP " ;

    }

    }

    4.4.2.3 performance monitoring Aspect (MonitorAspect)

    The realization of the MonitorAspect class is equally simple:

    public class MonitorAspect:Aspect

    {

    public MonitorAspect(IMessageSink nextSink): base (nextSink) {}

    }

    While the definition of the configuration file is shown below:

    < aspect value = " MonitorAOP " >

    < advice type = " before " assembly = " AOP.Advice " class = " AOP.Advice.MonitorAdvice " >

    < pointcut > ADD

    < pointcut > SUBSTRACT

    

    < advice type = " after " assembly = " AOP.Advice " class = " AOP.Advice.MonitorAdvice " >

    < pointcut > ADD

    < pointcut > SUBSTRACT

    

    

    MonitorAdvice belongs to the assembly file for AOP) Advice. DLL, the full class called

    AOP) Advice. MonitorAdvice.

    Performance monitoring Advice (MonitorAdvice)

    Because the performance monitoring method calls should be recorded before and after

    the specific time, so MonitorAdvice should realize IBeforeAdvice and IAfterAdvice interface

    at the same time:

    public class MonitorAdvice : IBeforeAdvice, IAfterAdvice {

    #region IBeforeAdvice Members

    public void BeforeAdvice(IMethodCallMessage callMsg)

{

    if (callMsg == null )

    {

    return ;

    }

    Console.WriteLine( " Before {0} at {1} " ,

    callMsg.MethodName, DateTime.Now);

    }

    #endregion

#region IAfterAdvice Members

    public void AfterAdvice(IMethodReturnMessage returnMsg)

    {

    if (returnMsg == null )

    {

    return ;

    }

    Console.WriteLine( " After {0} at {1} " ,

    returnMsg.MethodName, DateTime.Now);

    }

    #endregion

    }

    MonitorAdvice only need to record time of the method call, so only need respectively in BeforeAdvice () and AfterAdvice () method, record the current time.

    A business object with the application

    Business object (Calculator)

    Through AOP technology, we have the core focus completely separated crosscutting concerns, and when we define the business objects, do not need to pay attention to include logging, performance monitoring, etc., this is also the advantage of AOP technology.Of course, because use the.net Attribute and agent technology, for on the aspects of the business object, still need some small limit.

    First of all, we should define the good Aspect to the business object.Second, due to the agent technology to get the business object Context (Context), the Context must be specified, rather than the default Context.Context, is in the business object to create and call, if you want to access to the specified context, in the. Net, the request business object must inherit ContextBoundObject class.

    Therefore, the final Calculator class of business objects are defined as shown below:

    [MonitorAOP]

    [LogAOP]

    public class Calculator : ContextBoundObject

    {

    public int Add( int x, int y)

    {

    return x + y;

    }

    public int Substract( int x, int y)

    {

    return x - y;

    }

    }

    [MonitorAOP] and [LogAOP] it was previously defined in the Attribute, in addition the Calculator class inherited ContextBoundObject.In addition, the Calculator class definition with the ordinary object definition.However, it is using AOP technology, can intercept Calculator class Add () and Substract () method, for logging and performance monitoring.To implement the logic code of logging and performance monitoring, is completely with the Calculator class Add () and Substract () method to separate, the dependence between the lift, is advantageous to the module reuse and extension.

    Application (Program)

    We can implement a simple application, take a look at the business object Calculator on the logs and performance testing effect:

    class Program

    {

    [STAThread]

    static void Main( string [] args)

    {

    Calculator cal = new Calculator();

    cal.Add( 3 , 5 );

    cal.Substract( 3 , 5 );

    Console.ReadLine();

    }

    }

    Program creates a Calculator object, and calls the Add () and Substract () method.Because the Calculator object and performance testing on the log, but so will run results method calls the details of the print and call before and after the operation of the current time.

    If you want to change the way logging and performance monitoring results, it is written to a file, for example, is only need to change and the realization of the MonitorAdvice LogAdvice, for the Calculator object, you don't need to make any change.

    In the.net implementation of AOP, I through the use of dynamic proxy technology, basically realized the AOP several technical factors, including the aspect, advice, pointcut.I mentioned at the end of the article using the configuration file, to get advice and pointcuts mapping between, so as to make the construction aspect has extensibility.

    Carefully think about this question, I found that the delegate to build advice, does not seem to be a wise choice.While I'm in the middle of a mapping relationship is going to intercept the method name and intercept needed for the aspect of logic to set up a corresponding relationship, but really can delegate the aspect logic, to make it point to gens method signature with the commissioned completely matching method.This enables the advice to abstraction, so that the expansion of the concrete implementation.Entrust its implementation, however, after all is process oriented category, although in. Net, the delegate itself remains a class object, however, when creating the specific delegate instance are still hard to through the configuration file and reflection to obtain technology.

    Considering the entrusted with the nature of the interface of the abstract, may adopt the way of the interface to replace the entrust more feasible.Before the implementation of the scheme, I trust defines two for the advice:

    public delegate void BeforeAOPHandle(IMethodCallMessage callMsg);

    public delegate void AfterAOPHandle(IMethodReturnMessage replyMsg)

    I can define two interfaces IBeforeAction and IAfterAction, respectively correspond to the two delegate:

    public interface IBeforeAdvice

    {

    void BeforeAdvice(IMethodCallMessage callMsg);

    }

    public interface IAfterAdvice

    {

void AfterAdvice(IMethodReturnMessage returnMsg);

    }

    By defining interfaces, Advice and the Aspect can be separated, which completely

    accords with OO thoughts of the principle of separation of duties.

    (note: why do you want to define two interfaces for Advice? This is considering some

    Aspect only need to provide Before or After one of two logical, such as access control, just

    Before the Action.)

    So when the class library users to define their own Aspect, can define specific Advice,

    to realize the two interfaces, as well as the specific Advice logic.For example, the

    aforementioned log Aspect:

    public class LogAdvice:IAfterAdvice,IBeforeAdvice {

    #region IBeforeAdvice Members

    public void BeforeAdvice(IMethodCallMessage callMsg) {

    if (callMsg == null )

    {

    return ;

    }

    Console.WriteLine( " {0}({1},{2}) " ,

    callMsg.MethodName, callMsg.GetArg( 0 ),

    callMsg.GetArg( 1 ));

    }

#endregion

#region IAfterAdvice Members

    public void AfterAdvice(IMethodReturnMessage returnMsg) {

    if (returnMsg == null )

    {

    return ;

    }

    Console.WriteLine( " Result is {0} " , returnMsg.ReturnValue); }

#endregion

    }

    In AOPSink derived class of a class, add the method name and Advice mapping (this mapping relation, we can understand for AOP pointcut), you can add to achieve the Advice interface class objects, such as:

    public override void AddAllBeforeAdvices()

    {

    AddBeforeAdvice( " ADD " , new LogAdvice());

    AddBeforeAdvice( " SUBSTRACT " , new LogAdvice());

    }

    public override void AddAllAfterAdvices()

    {

    AddAfterAdvice( " ADD " , new LogAdvice());

    AddAfterAdvice( " SUBSTRACT " , new LogAdvice());

    }

    Because LogAdvice IBeforeAdvice and IAfterAdvice class implements the interface, such as new LogAdvice operation can through the reflection to create the instance, such as:

    IBeforeAdvice beforeAdvice =

    (IBeforeAdvice)Activator.CreateInstance( " Wayfarer.AOPSample " , " Wayfarer.AOPSample.LogAdvice " ).Unwrap();

    The CreateInstance () method of the parameter value, it is can completely through the configuration file to configure:

    < aop >

    < aspect value = " LOG " >

    < advice type = " before " assembly = " Wayfarer.AOPSample " class = " Wayfarer.AOPSample.LogAdvice " >

    < pointcut > ADDpointcut >

< pointcut > SUBSTRACTpointcut >

    advice >

    < advice type = " after " assembly = " Wayfarer.AOPSample " class = "

    Wayfarer.AOPSample.LogAdvice " >

    < pointcut > ADDpointcut >

    < pointcut > SUBSTRACTpointcut >

    advice >

    aspect >

    aop >

    This undoubtedly improve the scalability of AOP implementation.

    Implementation of AOP in the.net implementation of AOP, request contains classes

    must inherit ContextBoundObject intercepted method.This is a big restriction.Not only that,

    ContextBoundObject has great influence on the performance of the program.We can do a

    little test.Define two classes, one class inherits ContextBoundObject.They all achieved a

    cumulative operation:

    class NormalObject

    {

    public void Sum( int n)

    {

    int sum = 0 ;

    for ( int i = 1 ; i <= n; i ++ )

    {

    sum += i;

    }

    Console.WriteLine( " The result is {0} " ,sum); Thread.Sleep( 10 );

    }

    }

class MarshalObject:ContextBoundObject

    {

    public void Sum( int n)

    {

    int sum = 0 ;

    for ( int i = 1 ; i <= n; i ++ )

    {

    sum += i;

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com