MVC AOP Oriented Oriented Programming
I believe everyone has not been exposed to the word AOP, but in fact you have already been exposed to it, just in the design mode. The idea used by AOP is actually the same as the design pattern, that is, the functions are added or modified without modifying the original code. Also, AOP is mostly used in spring, but what is written in this article is only used in MVC, so be careful.
1. Introduction
The so-called AOP (Abbreviation of Aspect Oriented Programming) means a technology that implements unified maintenance of program functions through precompilation methods and dynamic agents during runtime. AOP is a continuation of OOP, a hot topic in software development, an important part of Spring framework, and a derivative paradigm of functional programming. The various parts of the business logic can be isolated by using AOP, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development.
The above is the official explanation of Baidu Encyclopedia, but in my opinion, AOP is actually more of a kind of thought, a kind of thought that can move and connect the whole body. AOP actually faces more of a program body written by a consent function or a calling process. From its first word Aspect, which refers to an aspect, we can also understand that this method is to implement one aspect. This is actually very similar to the global file in mvc, and is also an important part of the Spring framework. It is a derivative paradigm of functional programming. The various parts of the business logic can be isolated by using AOP, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and improving the efficiency of development.
Secondly, its precompilation nature can handle some errors well or prejudice some situations, which also leads to it becoming more common in judging permissions and unified output of something when designing. "A technology can be achieved through precompilation methods and dynamic proxying during runtime to dynamically add functions to programs without modifying the source code. AOP is actually a continuation of the GoF design pattern. The design pattern is tirelessly pursuing decoupling between the caller and the callee, improving the flexibility and scalability of the code. AOP can 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 take a look. First, we need to create a new MVC project in vs., select MVC3 or 4 and it is OK, then create a controller, name it at will, and then create its view, and write the simplest HelloWorld on the view.
<!DOCTYPE html> <html> <head> <title>Index</title> </head> <body> <div> <a>Hello! World</a> </div> </body> </html>
The picture on the right shows the MVC solution I created, the added controller and the view. Write the above code in the view to display Hello! World.
After running (it doesn’t work) does the word HelloWorld appear on the web page?
OK, next we start creating a new AOP file and then use it. First, add a new class to this project, called FilterPublic, add the reference using System.Web.Mvc in the reference, and then let this class inherit from ActionFilterAttribute. Everyone should pay attention to the word Action, which means that this thing is based on action.
Then we write the following code:
public string Message { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); filterContext.HttpContext.Response.Write("Before Action execution" + Message + "<br />"); } public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); filterContext.HttpContext.Response.Write("After the execution of the Action" + Message + "<br />"); } public override void OnResultExecuting(ResultExecutingContext filterContext) { base.OnResultExecuting(filterContext); filterContext.HttpContext.Response.Write("Return before Result" + Message + "<br />"); } public override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext); filterContext.HttpContext.Response.Write("Return after Result" + Message + "<br />"); }This is the various actions triggered by Filter, and then we make the following modifications to the default method in the controller:
[<span style="font-size:18px;">FilterPublic</span>(Message = "Action")] public ActionResult Index() { HttpContext.Response.Write("Action is executing・・<br />"); return Content("Result・・<br />"); } Then add a sentence [FilterPublic(Message = "Controller")] on the controller's class class, and then run it. What will happen?
Action before the execution Action is executing ・・・After the execution Action returns Result Before the execution Action is returning Result・・・After the return Result After the return
You can see that before the method in the controller is executed, we will first execute the code in FilterPublic we set below. And as the action takes place in different times, we can also see which method is triggered.
However, the controller-based method written on the controller did not trigger. Why is this?
In fact, this problem is very simple. When we set up our AOP program, we did not set the parameters and did not allow filters to be superimposed. At this time, we just add: [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] to the FilterPublic class you have written to, and we can trigger various filters or superimposed filters. At this time, let's try running:
Controller before Action is executed. Action is executed. Action is executed. After Action is executed. Action is executed. After Action is executed. Controller returns before Result. Controller returns before Result. Action is returning after Result. Action is returning after Result. Controller returns after Result.
In this way, the Filter on the controller is also triggered, so when we use the AOP method, we only need to write a filter class tag on our own written method or the default loading method of the page.
So, if we have a function that requires all pages to be triggered, wouldn’t it be troublesome? Don't worry, it's our turn to work at this time. Just register the Filter you wrote under the RegisterGlobalFilters method under the global.asax file:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); //Define your own filter as global filters.Add(new <span style="font-size:18px;">FilterPublic</span>() { Message = "Global<br/>" }); }Then run and see the results:
Action before execution global Action before execution Controller Action before execution Action before execution Action is being executed ・・・After action after execution Action after execution Controller Action after execution Global return Result before global return Result before Controller Return Result before Result Action is being returned Result after Return Result after Return Result
In this way, the global triggering is done (the message in filter is only used to identify the hierarchy, and it can be not defined when used formally.)
From this perspective, if you want to quickly add a global method, you just need to create a new Filter and then change the global file. Isn't it very convenient?
The above are some of my brief research on AOP. If there are any errors, please correct me.
Thank you for reading, I hope it can help you. Thank you for your support for this site!