asp.net mvc case Tutorial (based on asp.net mvc beta)--sixth: Interceptor

Source: Internet
Author: User
Tags aop exception handling

Summary

This article will discuss the use of interceptors in asp.net mvc by adding log and exception handling functions to the announcement feature of the MVC Bulletin Publishing System.

A small problem

We continue to improve the "MVC Bulletin Release System", this time, our demand is to announce the release function to add logging ability, that is, before the announcement, record once, after the announcement of success, and then record again. Then also make it have exception handling, that is, when the business component problems, jump to the appropriate error page and display the corresponding prompts.

Some people may laugh, this is what is difficult, in dorelease this action beginning and end of each join the corresponding log function soon knot. Exception handling is not to mention, direct try...catch to fix.

Yes, the above approach does work, but there are two points to be asked:

1. Code duplication problem. Many of the log processing code and exception handling code are very similar, resulting in a large number of duplicate code in each action.

2. Responsibility was undermined. Don't forget, our controller is just a controller, it should only be responsible for presentation logic, and should not be surrounded by a lot of log processing code and Try...catch block. The action we want should be clean, neat, and contain only the action that represents the logic.

The above two points, causing the bad taste code in our system. So, how do we solve this problem?

From chef to AOP

First imagine a scene: How does a restaurant's senior chef work? We know that he does not need to wash vegetables to cut vegetables, do not carry dishes, if found in the hands of the beef deteriorated he did not take the beef to find the butcher's theory, his work is very single: stir fry.

When the raw material sent, there is a special dish to cut vegetables to wash vegetables, cut vegetables, and then treat the dishes to the chef, Cook just fry, fry the food naturally also do not care, because there is a special waiter responsible for this matter. If it is found that the beef has gone bad, it will just say, naturally, the corresponding person to deal with this matter.

This scenario is typical of AOP (facet-oriented programming). Chefs can be regarded as a business component, it has a method is "stir fry", but before cooking to cut vegetables, fry up to someone to send food, but this is not the chef should care about! So our cutting workers and waiters is the equivalent of interceptors, which cut vegetables before the FRY interception, to cut vegetables, the waiter in the cooking after the interception, responsible for the delivery of vegetables. Of course, we also have an anomaly interceptor: the person dealing with the problem is the one who, when the chef finds that the meat has gone bad, cries out and deals with it.

Based on this scenario, let's see what the benefits are. First of all, the chef has a single responsibility, he can focus on his work: Stir fry, but do not care about their own concerns. and "interceptors" can be reused, for example, a stingy boss can find 3 chefs, but only one waiter, a waiter can give three cooks end dishes, so that the interceptor reuse makes the code repeatedly disappeared!

Come back

OK, now back to our "MVC Bulletin release System." Believe that look at the above scene, your inspiration must come: Yes, action is not the chef, if we can make the log function as a interceptor, in the dorelease before the execution of the interception of a record log function, dorelease execution and then intercept a log once again. It's best to have an interceptor that can be intercepted when the action is abnormal (like the person who processed the tainted beef).

But how do you implement the interception action? It's so lucky, asp.net. This mechanism is built into the MVC framework! Haha, let's hurry up and do it!

Related Article

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.