简体   繁体   English

在 web 应用程序中的何处以及如何使用拦截器?

[英]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?使用过滤器而不是拦截器更好吗?

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:例如,我注意到我的许多执行输入验证的 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 :以下是我认为可能会发生这种类型的代码重复的一些情况的列表,即闻起来像它们可以从拦截器中受益的场景

  • 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.您的问题是关于 web 应用程序,但如果您正在开发具有 MVP 样式视图的 Windows 应用程序,您可以应用一个拦截器来确保所有方法调用都同步回 UI 线程。
  • 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 event implementations. PropertyChanged 事件实现。 This code is usually very repetitive and annoying to write.这段代码通常非常重复且写起来很烦人。 Sacha Barber has thoroughly explored how to automatically implement this event using various frameworks . Sacha Barber 彻底探索了如何使用各种框架自动实现此事件
  • 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. AuthorizeAttribute正是对此的过滤器。
  • Web service request throttling. Web 服务请求限制。 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.一些 API,例如 Basecamp 的 API,要求您将请求限制为每个给定时间范围内的一定数量的请求。 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.如果您编写了 Basecamp 客户端 class,则可以对其应用拦截器以确保所有方法调用都符合速度限制,必要时使用Thread.Sleep
  • Result caching.结果缓存。 MVC has some filters pre-built for this purpose . MVC 为此目的预先构建了一些过滤器 You could write your own interceptor to cache results for layers underneath the UI layer.您可以编写自己的拦截器来缓存 UI 层下层的结果。
  • WCF error handling. WCF 错误处理。 You can't 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.如果 WCF 客户端位于Faulted state 中,则您无法Dispose它,因此创建和销毁客户端实例的每个方法都需要检查 Z9ED39E2EA931586B6A985A6942EF573Z 客户端,而不是简单地在客户端周围using调用子句,而不是简单地包装Abort子句。 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 .修复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.拦截器的可能应用程序与您编写的应用程序一样多种多样。

How to use interceptors如何使用拦截器

I can think of three primary places where you might like to apply an interceptor: Controllers, Services, and Domain objects.我可以想到您可能希望应用拦截器的三个主要位置:控制器、服务和域对象。

  • With an MVC controller , it makes the most sense to go ahead and useMVC's filters .使用MVC controller ,最有意义的是提前 go 并使用MVC 的过滤器
  • 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 .对于要从 IoC 容器中提取的中间层服务,过滤器不是一个选项(因为它不是控制器),因此您应该使用IoC 容器的拦截功能
  • 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 .对于您通常使用构造函数直接实例化(如果它是新实体)或从您选择的 ORM 获取(如果它是现有实体)的域对象,您需要使用某种object 工厂而不是构造函数并指导您的 ORM 如何使用工厂

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.

相关问题 如何在web应用程序中使用sendgrid发送邮件? - how to use sendgrid in web application to send mail? 如何将PrincipalContext与MVC Web应用程序一起使用 - How to use PrincipalContext with MVC Web Application Web应用程序项目 - 如何使用ProfileCommon - Web Application Project - how to use ProfileCommon 如何在Web应用程序中使用配置文件 - How I can use profile in web application Azure Web 应用程序 - 如何使用委托和应用程序权限 - Azure Web Application - how to use both delegated and Application permissions 如何使用WCF服务支持Web应用程序和移动应用程序? - How to use WCF service to support Web Application as well as Mobile Application? 如何在Windows应用程序和Web窗体应用程序中使用dll,即使它引用Web内容(如nUnit)? - How to use a dll in both windows application and web forms application even if it references web stuff (like nUnit)? .NET Web应用程序本机使用了多少个线程? - How many threads does a .NET web application natively use? 如何在不同的Web应用程序中使用一个angularjs应用程序? - How to use one angularjs app in a different web application? 如何在ASP .NET Web应用程序中使用动态本地化 - How to use dynamic localization in an asp .net web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM