Based on. NET's AOP implementation technology

Source: Internet
Author: User
Tags aop emit visual studio
Based on. NET's AOP implementation technology

Objective

In the author's "Object-oriented Application service Layer Design", the author discusses the problems that need to be considered in designing application service layer in software system and the basic thinking method of system layering. The issues of concern in these discussions are the vertical divisions of the system. However, when designing software systems, we should not only consider vertical relationships, but often we also need to focus on so-called crosscutting concerns, such as logging, security validation, and so on in every part of the system. AOP (aspect-oriented programming) arises to address these crosscutting concerns.

Although AOP is not as sophisticated as OOP, there are already several products that support AOP, including Aspectj,aspectwerkz, which are based on the Java platform. On the. NET platform, there are some implementations, such as loom, but, compared to the implementation of the Java platform, these implementations are still immature, the function is relatively weak, not very convenient to use. Therefore, the author in. NET platform to implement a lightweight AOP framework, now come out to discuss with you.

In this article, the author will first summarize the current implementation of AOP basic means, and then, give an already implemented AOP example (Websharpaspect), and provide all the source code, this is a lightweight AOP implementation, although the current functionality is not very strong, implementation is relatively simple, but, He can already do most of the AOP that we need, and you can use it directly in your project. The important thing is that it shows up in the. NET environment, I also hope that by providing the source code, we can learn more about AOP.

Ways to implement AOP

The key to implementing AOP is to intercept normal method calls, and to "weave" the extra functionality transparently into these methods to accomplish some additional requirements. Generally speaking, there are two main types of weaving methods: static weaving and dynamic weaving.

Static weaving methods are generally required to extend the function of the compiler, add the code that needs to be woven directly to the appropriate weaving point by modifying the method of bytecode (Java) or Il code (. Net), or we need to add a new syntax structure for the original language, which is syntactically supportive of AOP. ASPECTJ is the way it is used. The advantage of implementing AOP in this way is the high efficiency of code execution, with the disadvantage that the implementation needs a deep understanding of the virtual machine to be able to modify the bytecode. Because the weaving method is static, when a new weaving method needs to be added, it is often necessary to recompile, or to run the bytecode intensifier to perform the static weaving method again. Of course, on the. NET platform, we can also use the powerful features provided by emit to achieve this. In addition, bytecode intensifier brings a lot of opacity, the programmer is difficult to intuitively debug the enhanced bytecode, so many programmers always psychologically resist this bytecode enhancer.

Dynamic weaving method, the specific implementation of the way there are many choices. On the Java platform, you can use proxy mode, or custom ClassLoader to implement AOP functionality. On the. NET platform, to implement the dynamic weaving of AOP, there are several ways to do this:

L use ContextAttribute and ContextBoundObject to intercept the object's methods. On the specific use of contextattribute, readers can refer to MSDN and other related information.

L use emit to dynamically build the class that is woven into the code at run time, and when the program invocation is woven into the class, it actually calls the modified class. Loom is using this approach, but, personally, loom's current implementation is very blunt, and its scalability and flexibility are not very good.

L Use proxy mode. This is also the method that this article will explain in detail.

L of course, in the ASP.net project, we also have the option of using HttpHandler and HttpModule to customize the access to the ASP.net page and add some of the processing we need. on how to use the content of HttpHandler and HttpModule, you can refer to the author's article "ASP." NET in the custom HTTP processing and the application HttpHandler article, as well as "ASP." HttpModule of custom HTTP processing and application in net

Next, we explore how to use proxy mode to implement an AOP framework that is available on the. NET platform.

First example

First, let's look at the effect of Websharpaspect. We can use the following steps to complete our first example:

1. Create a new console application in Visual Studio to add Websharp.Aspect.dll to the reference.

2, add a class, named Firstaspect, and enable him to implement the Iaspect interface, add code as follows:

public class Firstaspect:iaspect

{

public void Execute (object[] paramlist)

{

Console.WriteLine ("Firstaspect is called");

}

}


3, add a Businessclass class, simulate the specific business logic class, so that this class inherits the Aspectobject class, and add the aspectmanaged attribute, and then add two methods, the code is as follows:

[Aspectmanaged (True)]

public class Businessclass:aspectobject

{

Public Businessclass () {}

public void Outputmethod ()

{

Console.WriteLine ("Outputmethod ()");

}

public void GetString ()

{

Console.WriteLine ("GetString ()");

}

}


4, add a app.config profile for the project, and add the following:

<?xmlversion= "1.0" encoding= "Utf-8"?>

<configuration>

< configsections >

<sectionname= "Websharp.aspects" type= "Websharp.aspect.aspectconfighandler,websharp.aspect"/>

</configsections >



< websharp.aspects >

< Aspect type = "Weavetest.firstaspect,weavetest" Deploy-model = "Singleton"

Pointcut-type= "method| Construction "action-position=" Both "match=" *,* "/>"

</websharp.aspects >

</configuration>


5, add the following code in the Main method:

public class MainClass

{

[STAThread]

static void Main ()

{

Businessclass cls=new Businessclass ();

Cls. Outputmethod ();

Cls. GetString ();

Console.ReadLine ();

}

}


Run the above code, and the results are as follows:


As you can see, Firstaspect successfully intercepted the Businessclass method, as we had expected.



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.