MVC AOP-Oriented programming
The word AOP is believed that we have not touched too much, but in fact you already have contact, in design mode. AOP uses the same idea as the design pattern, which is to add or modify functionality uniformly without modifying the original code. Also, AOP is mostly used in spring, but this article is written only in the application of MVC, to be aware of.
First, Introduction
The so-called AOP (abbreviation of Aspect Oriented programming) is a kind of technology which is designed for cutting-plane, which realizes the unified maintenance of program function through precompilation and runtime dynamic agent. AOP is a continuation of OOP, a hotspot in software development, an important content in spring framework, and a derivative paradigm of functional programming. The use of AOP can isolate the parts of the business logic, so that the coupling between the parts of the business logic is reduced, the reusability of the program is improved, and the efficiency of the development is improved.
The above is Baidu Encyclopedia official explanation, but in my opinion, in fact, AOP more is a kind of thought, one can move through the whole body of thought, AOP actually face more is a kind of consent function or call process to write a program body, From its first word aspect (cut) also means one aspect, it can also be understood that this approach is also for an aspect of the implementation, which is actually similar to the global file in MVC, is also an important element in the spring framework, is a derivative of functional programming. The use of AOP can isolate the parts of the business logic, so that the coupling between the parts of the business logic is reduced, the reusability of the program is improved, and the efficiency of the development is improved.
The second is that its precompiled nature can be a good deal with a number of errors or prejudge some cases, which also led to it in the design of the decision to judge permissions, unified output of something more common. "A technique for dynamically adding functionality to a program without modifying the source code can be achieved through precompilation and runtime dynamic proxies." AOP is actually a continuation of the GOF design pattern, the design pattern pursues the decoupling between the caller and the callee, improves the flexibility and scalability of the code, and AOP can be said to be an implementation of this goal. "The above sentence is a good illustration of AOP," he said.
Ii. implementation in MVC
So much to say, let's do it, first we need to create an MVC project on VS, choose MVC3 or 4 OK, then build a controller, name it randomly, then build his view and write the simplest HelloWorld on the view.
On the right, I created the MVC solution and added good controller and view, writing the above code in view to show hello! World.
After running (does not run the self-Baidu) is not the display of a HelloWorld on the page?
OK, so let's start with a new AOP file and then use it, and first add a new class to the project, named Filterpublic, where you add a using SYSTEM.WEB.MVC this reference, and then let this class inherit from ActionFilterAttribute, you have to 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 executes" + message + "<br/>"); public override void OnActionExecuted (ActionExecutedContext filtercontext) {base.
OnActionExecuted (Filtercontext);
FilterContext.HttpContext.Response.Write ("After action execution" + message + "<br/>"); public override void Onresultexecuting (ResultExecutingContext filtercontext) {base.
Onresultexecuting (Filtercontext);
FilterContext.HttpContext.Response.Write ("return result before" + message + "<br/>"); public override void Onresultexecuted (ResultExecutedContext filtercontext) {base.
Onresultexecuted (Filtercontext);
FilterContext.HttpContext.Response.Write ("return result" + message + "<br/>"); }
This inside is the various actions that the filter triggers, and then we make the following changes in the default method in controller:
[<span style= "FONT-SIZE:18PX;" >FilterPublic</span> (message = ' Action ')] public
actionresult Index ()
{
HttpContext.Response.Write ("Action is executing ... <br/> ");
Return Content ("Returning result <br/> ");
Then add a sentence to the class class Controller [filterpublic (message = "Controller")], and then run, what will happen?
The action action is executing before the action executes
...
Action
is returning result
before action returns result after action is performed Returns the action after result
You can see that the method inside the controller will execute the following code in the Filterpublic we set up before executing, and we can see which method is triggered as the time of the action occurs.
But why is it not triggered by the Controller method written on the controller?
The problem is simply that we didn't set the parameters when we set up our AOP program, and we didn't let the filter stack up, so we just add the Filterpublic class : [AttributeUsage ( AttributeTargets.All, AllowMultiple = True], you can set a variety of filter or superimposed filter to trigger, this time we run a try again:
Action action is executing before controller action executes before action executes
...
After action executes after the action action
executes controller returns result
before Controller returns result before the
action is returned
result
Returns result after the action
returns result controller
As a result, the filter on the controller is also triggered, so the way we use AOP is simply to write a filter class label on our own writing method or page default loading method.
So if we have a feature that requires all of the pages to be triggered, wouldn't that be too much trouble? Don't worry, it's our turn to make the global file work this time. Under the Global.asax file under the Registerglobalfilters method to register your own write filter can be:
public static void Registerglobalfilters (Globalfiltercollection filters)
{
filters. ADD (New Handleerrorattribute ());
Define your own filter as the global filter
filters. ADD (New <span style= "font-size:18px") >FilterPublic</span> () {message = "global <br/>"});
Then run and see the results:
The action action is executing before the controller action executes before the action executes before the global
action executes
...
After the action executes after the action action executes controller action executes after the global return result before returning the result
Controller
Action
is returning result
before result returns Returns result after the action
returns result controller
So the global trigger is done (the message in filter is only used to identify the level, the formal use of the time can not be defined.) )
Such a look, if you want to quickly add a global all the way, just to create a filter, and then change the global file can be done, is not very convenient.
These are my own for some of the shallow research of AOP, if there are errors in the place also please correct the reader.
Thank you for reading, I hope to help you, thank you for your support for this site!