Emit learning-the real story-implementing a simple AOP Framework (ii)

Source: Internet
Author: User
Tags aop definition bool emit exception handling

It's 9 o'clock, no nonsense, directly to the lecture, today is a simple introduction to the framework of the use of some of the interfaces and properties. Yesterday we divided the sections of our attention into three categories, now give the definition of these 3 interfaces:

Interface definition

/// <summary>
/// 在调用方法体前执行的处理接口
/// </summary>
public interface IPreProcess
{   
    bool PreProcess(MethodContext methodContext);
}
/// <summary>
/// 在调用方法体后执行的处理接口
/// </summary>
public interface IPostProcess
{
    void PostProcess(MethodContext methodContext);
}
/// <summary>
/// 处理方法调用所产生的异常的接口
/// </summary>
public interface IExceptionHandler
{
    void ProcessException(MethodContext methodContext, Exception exception);
}

L The inherited Ipreprocess interface enables processing of a method before it is executed, passing in a Methodcontext object containing some contextual information about the execution of the method, and then returning a bool value indicating whether the following method continues to be executed;

L Inherit the Ipostprocess interface to implement the processing of the method, the same pass to a Methodcontext object, and, unlike before, the Methodcontext object is also set up the true method of the implementation of the results, for some subsequent processing ;

L an inherited Iexceptionhandler interface can implement exception handling in the event of an exception to the method, which has a exception parameter that represents the exception information that was intercepted, compared to the above two methods.

For any one interface, each method can have more than one handler, and the framework will call execution in the order defined in the configuration file. Here is an example of a pseudo code after a method is implanted:

Pseudo code

public virtual void Test1(string text1)
{
    MethodContext context;
    try
    {
        //调用预处理程序
        if (_addPreProcessLog.PreProcess(context))
        {
            //调用真正的方法
            base.Test1(text1);
            //调用事后处理程序
            _addPostProcessLog.PostProcess(context);
        }
    }
    catch (Exception exception)
    {
        //调用异常处理程序
        _simplyExceptionHandler.ProcessException(context, exception);
    }
}

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.