Brief introduction and example of MVC AOP aspect oriented programming

MVC AOP aspect oriented programming

The word AOP is believed that everyone has not touched too much, but in fact you have touched it, which is in the design pattern. The idea of AOP is actually the same as that of design pattern, that is, uniformly adding or modifying functions without modifying the original code. In addition, AOP is mostly used in spring, but what this article writes is only the application in MVC. Pay attention to it.

1、 Introduction

So called AOP (abbreviation of aspect oriented programming) means aspect oriented programming, which realizes the unified maintenance of program functions through precompiled mode and runtime dynamic agent. AOP is the continuation of OOP, a hot spot in software development, an important content in spring framework, and a derivative paradigm of functional programming. AOP can be used for all aspects of business logic The two parts are isolated, which reduces the coupling between the parts of the business logic, improves the reusability of the program, and improves the efficiency of development.

The above is the official explanation of Baidu Encyclopedia, but in my opinion, AOP is more an idea, an idea that can move and pass through the whole body. In fact, AOP is more a program body written by agreeing functions or calling processes, From its first word aspect (aspect) refers to an aspect. You can also understand that this method is aimed at the implementation of an aspect. In fact, it is very similar to the global file in MVC. It is also an important content in the spring framework. It is a derivative paradigm of functional programming. AOP can be used to isolate all parts of business logic, so as to make the connection between all parts of business logic The coupling degree is reduced, the reusability of the program is improved, and the development efficiency is improved.

Secondly, its precompiled nature can handle some errors or judge some situations in advance, which also makes it more common to judge permissions and output something uniformly in design. "It is a technology to dynamically and uniformly add functions to the program without modifying the source code through precompile and runtime dynamic agent. AOP is actually the continuation of GOF design pattern, which tirelessly pursues the decoupling between the caller and the callee, and improves the flexibility and scalability of the code. AOP can also be said to be an implementation of this goal. ”The above sentence is also a good interpretation of AOP.

2、 Implementation in MVC

Having said so much, let's practice. First of all, we need to create an MVC project in VS, select mvc3 or 4, and then create a controller. The name is optional, and then create his view. Write the simplest HelloWorld on the view.

The figure on the right shows the MVC solution I created, the controller I added, and the view. Write the above code in the view to display Hello! World。

After running (self Baidu that won't run) does it display the word HelloWorld on the web page?

OK, let's start to create a new AOP file and then use it. First, add a new class called filterpublic in the project, and add using system. In the reference Web. MVC is referenced, and then this class inherits from actionfilterattribute. We should pay attention to the word action, which indicates that this thing is based on action.

Then we write the following code:

Here are various actions triggered by filter, and then we modify the default method in controller as follows:

Then add a sentence [filterpublic (message = "controller")] on the class class of the controller, and run it. What will happen?

You can see that the methods in the controller will execute the code in the filterpublic we set below before execution, and you can see which method is triggered with the different action time.

But the controller based method written on the controller did not trigger. Why?

In fact, the problem is very simple. When setting our AOP program, we did not set the parameters and did not let the filter run superimposed. At this time, we only need to add: [attributeusage (attributetargets. All, AllowMultiple = true)] to the written filterpublic class to trigger the various filters or superimposed filters you set, At this time, let's run again:

In this way, the filter on the controller is also triggered, so we only need to write a label of filter class on our own writing method or the default page loading method when using AOP method.

So, if we have a function that requires all pages to be triggered, isn't it troublesome? Don't worry, it's our global file's turn at this time. In global In the registerglobalfilters method under the asax file, just register the filter written by yourself:

Then run and see the results:

In this way, the global trigger is ready (the message in the filter is only used to identify the level, and it can be used formally without definition.)

In this way, if you want to quickly add a global method, you just need to create a new filter and then change the global file. Is it very convenient.

The above are my superficial research on AOP. If there are mistakes, please correct them.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>