[英]Where and how to use interceptors in web application?
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.
我知道这个概念被用于许多库,如 NHibernate、Entity Framework 等。 But i am interested in how to use this concept in ASP.NET MVC web application.
但我对如何在 ASP.NET MVC web 应用程序中使用这个概念很感兴趣。
Where it is usefull to use it in Mvc Web application?在 Mvc Web 应用程序中使用它在哪里有用?
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. Asp.net Mvc 已经支持一种带有过滤器的 controller 拦截器。 It is better to use filters instead of 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:
例如,我注意到我的许多执行输入验证的 MVC 操作都使用相同的几行代码:
if (!ModelState.IsValid)
return View(model);
This is code that could potentially be moved to an interceptor (probably an MVC filter in this case).这是可能被移动到拦截器(在这种情况下可能是 MVC 过滤器)的代码。 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).
(2 行代码乘以 controller 使用此操作的次数)。 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, ie 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. AuthorizeAttribute
正是对此的过滤器。Thread.Sleep
when necessary.Thread.Sleep
。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.Faulted
state 中,则您无法Dispose
它,因此创建和销毁客户端实例的每个方法都需要检查 Z9ED39E2EA931586B6A985A6942EF573Z 客户端,而不是简单地在客户端周围using
调用子句,而不是简单地包装Abort
子句。 An interceptor might not be the best fit in this case.Dispose
implementation or use some kind of wrapper .Dispose
实现或使用某种 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.关于如何完成所有这些的细节将取决于您使用的工具。
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.您不需要 DI 容器来应用该概念,但它会有所帮助。
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?您可以使用 ASP.NET MVC 过滤器来实现大致相同的效果,但是当您可以应用通常可重用的实现时,为什么还要限制自己使用 MVC 框架呢?
I would say you use a more generic DI container for injecting your dependencies.我会说您使用更通用的 DI 容器来注入您的依赖项。 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.
这不仅将依赖项注入到您的 controller 中,它还提供这些依赖项的依赖项,从而生成所有依赖对象的完整 object 图。
Using a DI container for the front end also brings nice opportunities for making your back end more unit testable and loosly coupled.为前端使用 DI 容器也为使您的后端更可单元测试和松耦合带来了很好的机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.