在 Web 应用程序中何处以及如何使用拦截器?
我最近对拦截器概念很感兴趣。我知道这个概念在 NHibernate、Entity Framework 等许多库中都有使用。但我对如何在 ASP.NET MVC Web 应用程序中使用这个概念感兴趣。
在 Mvc Web 应用程序中什么地方有用?
有没有使用拦截器的开源 Asp.Net Mvc 项目?
Asp.net Mvc 已经支持一种带有过滤器的控制器拦截器。使用过滤器而不是拦截器更好?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
何时/何时使用拦截器
查看您之前开发的应用程序并检查代码。查找在方法和属性的开头或结尾处经常重复的代码。您可以考虑将这段代码从所有这些方法移至拦截器中。例如,我注意到许多执行输入验证的 MVC 操作都使用相同的几行代码来执行此操作:
这些代码可能会被移动到拦截器(在本例中可能是 MVC 过滤器)。编写和应用过滤器的成本是否超过了重复代码的成本? (2 行代码乘以使用此代码的控制器操作数)。在这种情况下,也许不是。然而,在其他情况下,使用拦截器的好处会更大。
以下列出了我认为可能会发生此类代码重复的一些情况,即闻起来好像可以从拦截器中受益的场景:
:
AuthorizeAttribute
正是针对这一点的过滤器。Faulted
状态,则无法Dispose
,因此每个创建和销毁实例的方法客户端需要检查状态,然后在必要时调用Abort
,而不是简单地在客户端周围包装using
子句。在这种情况下,拦截器可能不是最合适的。 修复Dispose< 可能更容易/code> 实现或使用某种包装器
。
上述示例是否适合拦截器取决于您的应用程序的独特复杂性。当然,这个清单并不详尽,也不可能详尽。拦截器的可能应用程序与您编写的应用程序一样多种多样。
如何使用拦截器
我可以想到您可能希望应用拦截器的三个主要位置:控制器、服务和域对象。
有关如何完成所有这些的具体细节取决于您使用的工具。
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:
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:
AuthorizeAttribute
is a filter for exactly this.Thread.Sleep
when necessary.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.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.
How to use interceptors
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.
拦截可以用于很多事情 - 最值得注意的是解决跨领域问题,例如检测、日志记录、审计、安全、计量等。
您不需要 DI 容器来应用该概念,但它有帮助。
您可以使用 ASP.NET MVC 过滤器来实现大致相同的效果,但是当您可以应用通常可重用的实现时,为什么还要将自己限制在 MVC 框架上呢?
Interception can be used for many things - most notable to address cross-cutting concerns such as instrumentation, logging, auditing, security, metering, etc.
You don't need a DI Container to apply the concept, but it helps.
You can use ASP.NET MVC filters to achieve roughly the same effect, but why constrain yourself to the MVC framework when you can apply a generally reusable implementation?
我想说你使用更通用的 DI 容器来注入依赖项。这不仅将依赖项注入到您的控制器中,它还提供这些依赖项的依赖项,从而生成所有依赖对象的完整对象图。
在前端使用 DI 容器也带来了很好的机会,使您的后端更具单元测试性和松散耦合性。
I would say you use a more generic DI container for injecting your dependencies. Not only does this inject dependencies into your controller, it also serves the dependencies of those dependencies thus resulting in a complete object graph of all your dependant objects.
Using a DI container for the front end also brings nice opportunities for making your back end more unit testable and loosly coupled.