AOP implementation technology based on .NET
Foreword
In the article of the author's "object-oriented application service layer design", the author discusses the problems required to design the application service layer in the software system, as well as the basic thinking of the system. These discussions are concerned that the problems of concern are divided into the longitudinal level of the system. However, when designing the software system, we must not only consider the longitudinal relationship. In many cases, we also need to pay attention to the so-called "cross-cut point" problem, for example, the log record, security of each part of the system Verification, etc. The appearance of AOP (facing aspect) is to solve these "cross-cutting points" issues.
Although AOP is still very mature, there is already a number of products that support AOP, which is more famous as ASPECTJ, AspectWerkz, etc., these products are based on the Java platform. On the .NET platform, there are also some implementations, such as LOOM, but the implementation is still immature relative to the Java platform, the functions are relatively weak, and it is not very convenient to use. Therefore, the author implements a lightweight AOP framework under the .NET platform, now take it out to discuss together.
In this article, the author will first summarize the basic means of implementing AOP, and then give an example of the AOP that has been implemented, and provides all source code, which is a lightweight AOP implementation, although the current function Not very powerful, the implementation is relatively simple, but he can do most of the AOP function we need, you can use him directly in the project. Importantly, there are some technologies and ideas to implement AOP in the .NET environment, and the author also hopes that by providing source code, it is possible to help you learn AOP.
Method for implementing AOP
Implementing the key to an AOP is to intercept normal method calls, and we need additional functionally transparent "woven" to these methods to complete some additional requirements. From the overall method, the woven method has two categories: static woven and dynamically woven.
Static woven method, generally need to extend the compiler, will need to be woven, by modifying the word code (Java) or IL code (.NET) method, directly add to the corresponding woven point; Alternatively, we need to add new syntax structures to the original language, support AOP from syntax. Aspectj is the way it is used. Using this way to implement AOP, the advantage is that the code execution is high, and the disadvantage is that the implementation needs to have a deep understanding of the virtual machine to modify the bytecode. Since the woven method is static, when the new woven method is needed, it is often necessary to recompile, or the operation of the bytecode enhancer re-executes the static woven. Of course, on the .NET platform, we can also use the powerful features provided by EMIT to achieve this. In addition, the bytecode enhancer brings a lot of opacity, and the programmer is difficult to intuitively debug the enhanced bytecode, so many programmers always resist such bytecode enhancer.
Dynamic woven methods, there are many options in the specific implementation. On the Java platform, you can use the Proxy mode, or custom ClassLoader to implement the AOP function. On the .NET platform, to realize the dynamics of AOP, in summary, the following methods can be used:
l Use contettribute and contextBoundObject to intercept the object's method. For specific use methods for ContextAttribute, readers can refer to relevant information such as MSDN. l Use the EMIT to dynamically build a class after running time, when the program call is woven into the class, actually calls the modified class. LOOM is used in this way, however, individual thinks that LOOM current is very hard, and its scalability and flexibility are not very good.
l Use the Proxy mode. This is also a method described in detail herein.
Of course, in the ASP.NET project, we have a choice, which is to use HTTPHANDLER and HTTPModule to access the custom to the ASP.NET page, add some processing we need. About how to use HTTPHANDAL and HTTPMODULE, you can refer to the author's article "ASP.NET to customize HTTP processing and application httphandler articles", and "HTTPModule articles" and Application in ASP.NET "
Below, let's explore how to use the Proxy mode, implement an available AOP framework on the .NET platform.
First example
First, let's take a look at the effect of WebsharPaspect. We can use the following steps to complete our first example:
1. Create a console application in VisualStudio to add Websharp.aspect.dll to the reference.
2, add a class, named firstaspect, and make him realize 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 adds the AspectManaged feature, 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 an app.config configuration file to the project, and add the following:
xmlversion = "1.0" eNCoding = "UTF-8"?>
configsections>
Websharp.Anpects>
configure>
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, the results are as follows:
It can be seen that Firstaspect is successfully intercepting the BusinessClass method as we expect.
Source code and full text See: http://www.websharp.org can also download all source code from http://www.uml.org.cn/dvbbs6.0.0/index.asp