在Web应用程序中的何处以及如何使用拦截器? [英] Where and how to use interceptors in web application?
问题描述
我最近对拦截器概念感兴趣.我知道这个概念已在NHibernate,Entity Framework等许多库中使用.但是我对如何在ASP.NET MVC Web应用程序中使用此概念感兴趣.
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.
在Mvc Web应用程序中有用的地方?
Where it is usefull to use it in Mvc Web application?
有没有使用拦截器的开源Asp.Net Mvc项目?
Is there any open source Asp.Net Mvc project which use interceptors ?
Asp.net Mvc已经支持一种带有过滤器的控制器拦截器.最好使用过滤器而不是拦截器?
Asp.net Mvc already support a kind of interceptor for controller with filters. It is better to use filters instead of interceptors ?
推荐答案
在何处/何时使用拦截器
看看您以前开发的应用程序并检查代码.查找在方法和属性的开头或结尾经常重复的代码.您可能会考虑将此代码从所有这些方法转移到拦截器中.例如,我注意到我执行输入验证的许多MVC动作都是使用相同的几行代码来完成的:
Where/when to use interceptors
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);
这是可能会移动到拦截器(在这种情况下,可能是MVC过滤器)的代码.编写和应用过滤器的成本是否超过了重复代码的成本? (2行代码乘以使用此代码的控制器操作数).在这种情况下,也许不是.但是,在其他情况下,使用拦截器的好处会更大.
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:
- 输入验证(如上所述).
- 调试日志记录..您可以编写拦截器记录每个方法调用的进入和退出.
- 线程同步..您的问题是有关Web应用程序的,但是如果您要开发具有MVP样式视图的Windows应用程序,则可以应用拦截器,以确保所有方法调用都同步到UI线程.
- 数据库事务.我的大多数数据库事务代码如下:
- Input validation (as illustrated above).
- Debug logging. You could write an interceptor that records the entrance and exit of every method call.
- Thread synchronization. Your question is about web apps, but if you're developing a Windows application with an MVP style view, you could apply an interceptor that ensures that all method calls are synchronized back to the UI thread.
- Database transactions. Most of my database transaction code looks like this:
using (var transaction = Session.BeginTransaction())
{
// ... do some work that is unique to this method ...
transaction.Commit();
}
- PropertyChanged事件实现.该代码通常非常重复且令人讨厌. Sacha Barber彻底探索了如何使用各种框架自动实施此事件.
- 安全性.您的应用程序中可能有许多方法应仅限于特定用户使用.
AuthorizeAttribute
是针对就是这个. - Web服务请求限制..某些API(例如Basecamp的API)要求您 MVC为此目的预先构建了一些过滤器.您可以编写自己的拦截器,以缓存UI层下面各层的结果.
- WCF错误处理.如果WCF客户端处于
Faulted
状态,则不能Dispose
WCF客户端,因此创建和销毁该客户端实例的每个方法都需要检查该状态. ,然后在必要时调用Abort
,而不是简单地在客户端周围包装using
子句.在这种情况下,拦截器可能不是最合适的. 也许更容易解决Dispose
实现或使用某种包装器. - PropertyChanged event implementations. This code is usually very repetitive and annoying to write. Sacha Barber has thoroughly explored how to automatically implement this event using various frameworks.
- Security. There are probably many methods in your application that should be restricted to only certain users. The
AuthorizeAttribute
is a filter for exactly this. - Web service request throttling. Some API's, such as the API for Basecamp, ask that you limit your requests to a certain number of requests per a given timeframe. If you wrote a Basecamp client class, you could apply an interceptor to it to ensure that all the method calls honored the speed limit, using
Thread.Sleep
when necessary. - Result caching. MVC has some filters pre-built for this purpose. You could write your own interceptor to cache results for layers underneath the UI layer.
- WCF error handling. You can't
Dispose
a WCF client if it's in theFaulted
state, so every method that creates and destroys an instance of the client needs to check the state, then callAbort
if necessary instead of simply wrapping ausing
clause around the client. An interceptor might not be the best fit in this case. It's probably easier to just fix theDispose
implementation or use some kind of wrapper. - 使用 MVC控制器,最有意义的是继续使用 您的IoC容器的拦截功能 .
- 对于您的域对象,通常要么直接使用构造函数实例化(如果是新实体),要么从选择的ORM中获取(如果它是现有实体),则需要使用某种对象工厂而不是构造函数和
- With an MVC controller, it makes the most sense to go ahead and use MVC's filters.
- For a middle-tier service that you would pull out of your IoC container, filters are not an option (because it's not a controller), so you should use the interception features of your IoC container.
- For your domain objects that you typically either instantiate directly with a constructor (if it's a new entity) or fetch from your ORM of choice (if it's an existing entity), you'll need to use some sort of object factory instead of the constructor and instruct your ORM how to use the factory.
以上示例是否适合用作拦截器,取决于您应用程序的独特复杂性.当然,这份清单并不详尽,也不是全部.拦截器的可能应用与您编写的应用是一样的.
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.
这篇关于在Web应用程序中的何处以及如何使用拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!