I am interested in interceptor concept in recent times. I know that this concept is used in many libraries like NHibernate, Entity Framework and others. But i am interested in how to use this concept in ASP.NET MVC web application.
Where it is usefull to use it in Mvc Web application?
Is there any open source Asp.Net Mvc project which use interceptors ?
Asp.net Mvc already support a kind of interceptor for controller with filters. It is better to use filters instead of interceptors ?
Spring Interceptor are used to intercept client requests and process them. Sometimes we want to intercept the HTTP Request and do some processing before handing it over to the controller handler methods.
An interceptor aircraft, or simply interceptor, is a type of fighter aircraft designed specifically for the defensive interception role against an attacking enemy aircraft, particularly bombers and reconnaissance aircraft.
An Interceptor is a function that is invoked by the framework BEFORE or AFTER an action invocation. It allows a form of Aspect Oriented Programming, which is useful for some common concerns such as: Request logging. Error handling.
Use the @AroundInvoke annotation to designate interceptor methods for managed object methods. Only one around-invoke interceptor method per class is allowed. Around-invoke interceptor methods have the following form: @AroundInvoke visibility Object method-name(InvocationContext) throws Exception { ... }
Take a look at a previous application you've developed and examine the code. Look for code that is frequently duplicated at the beginning or end of methods and properties. This is code that you may consider moving from all of those methods into an interceptor. For example, I've noticed that many of my MVC actions that perform input validation do so with same same couple lines of code:
if (!ModelState.IsValid)
return View(model);
This is code that could potentially be moved to an interceptor (probably an MVC filter in this case). Does the cost of writing and applying the filter outweigh the cost of this duplicated code? (2 lines of code times the number of controller actions using this). In this case, perhaps not. There are other situations, however, where the benefit of using an interceptor would be greater.
Here's a list of some situations where I imagine this type of code duplication might occur, i.e. scenarios that smell like they could benefit from interceptors:
using (var transaction = Session.BeginTransaction())
{
// ... do some work that is unique to this method ...
transaction.Commit();
}
AuthorizeAttribute
is a filter for exactly this.Thread.Sleep
when necessary.Dispose
a WCF client if it's in the Faulted
state, so every method that creates and destroys an instance of the client needs to check the state, then call Abort
if necessary instead of simply wrapping a using
clause around the client. An interceptor might not be the best fit in this case. It's probably easier to just fix the Dispose
implementation or use some kind of wrapper.Whether or not the above examples would be good candidates for interceptors depends on the unique intricacies of your application. This list of course is not exhaustive, nor can it be. The possible applications of interceptors are as varied as the applications you write.
I can think of three primary places where you might like to apply an interceptor: Controllers, Services, and Domain objects.
The nitty gritty details about how to accomplish all of this will depend on which tools you are using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With