AOP Programming and ASP.net MVC

Source: Internet
Author: User

AOP (Aspect oriented programming)-oriented programming for facets. It's not easy to say cut sections, where the code has slices. And not three-dimensional objects. The concept doesn't matter, we understand the noun from its thought. The main idea of AOP is to pull out the same, similar and fragmented logic and handle it uniformly; this is not only easy to maintain, but also makes the code more concerned about itself, clear.

For example, our common permission checks, logging, exception handling are scattered throughout the system, such as the code to publish an article: public void Post (Article Article) {if (CurrentUser is null)       throw new Authexception ("You haven't logged in yet");   else Articlemanager.save (article); }

Originally a word articlemanager.save can handle things, now to add if else also to deal with the exception, the code appears to be unusually ugly and difficult to maintain.   If replaced by [authorize] public void Post (Article Article) {articlemanager.save (Article); }

Use Authroizeattribute to handle permissions issues, so that the code is much clearer and can be reused, so good thinking is AOP.
Of course attribute is just a way to implement, attribute is to call the Post method, by reflection to get attribute, and then execute its code,
usually we use the most AOP is in the asp.net MVC framework, This authorizeattribute is the MVC itself. We can rewrite some of his methods to achieve the desired functions (such as permission levels, etc.).

To give an example of dealing with an exception, if we have a unified logic for handling exceptions, then we can use the Try/catch in the logical code instead of the throw exception, which makes the code neater.
PS: than to say that there is a unified treatment will no longer need to try/catch, some abnormal should eat or eat, this depends on the specific business needs. Other exceptions that are not captured, such as line Chengri, should pay attention to catch. )
For example: public void Post (Article Article) {if String.IsNullOrEmpty (Article.       title)) {throw new Argumentmissexception ("title");       }       ...   Articlemanager.save (article); } protected override void Onexception (Exceptioncontext filtercontext) {//.. Exception handling Code}

Rewriting MVC to provide a good onexception method can handle all exceptions, or it is a user-friendly error interface, or it is convenient to send an exception log without having to write code everywhere.
Extended to consider, if the unified processing of abnormal information, then the handling of the exception to deal with the grasp of the exception to see the design and application of the class.

In addition, look at the above code, if there are more than one place to post (Article), but completely inconsistent, but the field check is consistent, it will result in the field check code duplication, this situation either in a separate way to verify the validity of Article, and then call each place, Or the modelbinder provided by MVC, an AOP approach implemented by attribute of parameters; Mvc was too thoughtful to be praised. Public void post ([articlebinder]article article)    {        //There's no need to write article validation code here.        articlemanager.save (article);  }       Public class articlebinderattribute : custommodelbinderattribute    {       class ArticleBinder : IModelBinder       {           public object  Bindmodel (Controllercontext controllercontext, modelbindingcontext bindingcontext)            {                //Although the custom binder is very comfortable, but there is no use to get the value of the parameterThe action parameters are so comfortable.                var title =  controllercontext.routedata.values["title"]. ToString ();               if  (string . IsNullOrEmpty (title))                {                     Throw new argumentnullexception ("title");                }                return new Article                {                    Title = title      &Nbsp;        };  

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.