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

发布于 2024-11-29 18:48:12 字数 228 浏览 4 评论 0原文

我最近对拦截器概念很感兴趣。我知道这个概念在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世态炎凉 2024-12-06 18:48:12

何时/何时使用拦截器

查看您之前开发的应用程序并检查代码。查找在方法和属性的开头或结尾处经常重复的代码。您可以考虑将这段代码从所有这些方法移至拦截器中。例如,我注意到许多执行输入验证的 MVC 操作都使用相同的几行代码来执行此操作:

if (!ModelState.IsValid)
    return View(model);

这些代码可能会被移动到拦截器(在本例中可能是 MVC 过滤器)。编写和应用过滤器的成本是否超过了重复代码的成本? (2 行代码乘以使用此代码的控制器操作数)。在这种情况下,也许不是。然而,在其他情况下,使用拦截器的好处会更大。

以下列出了我认为可能会发生此类代码重复的一些情况,即闻起来好像可以从拦截器中受益的场景

  • 输入验证 >(如上图所示)。
  • 调试日志记录。您可以编写拦截器记录每个方法调用的进入和退出。
  • 线程同步。您的问题是关于 Web 应用程序的,但如果您正在开发具有 MVP 样式视图的 Windows 应用程序,您可以应用一个拦截器来确保所有方法调用都同步回 UI 线程。
  • 数据库事务。我的大多数数据库事务代码如下所示

using (var transaction = Session.BeginTransaction())
{
    // ... do some work that is unique to this method ...
    transaction.Commit();
}

上述示例是否适合拦截器取决于您的应用程序的独特复杂性。当然,这个清单并不详尽,也不可能详尽。拦截器的可能应用程序与您编写的应用程序一样多种多样。

如何使用拦截器

我可以想到您可能希望应用拦截器的三个主要位置:控制器、服务和域对象。

  • 对于 MVC 控制器,继续使用 MVC 的过滤器
  • 对于从 IoC 容器中提取的中间层服务,过滤器不是一个选项(因为它不是控制器),因此您应该使用 IoC 容器的拦截功能
  • 对于您通常使用构造函数直接实例化(如果它是新实体)或从您选择的 ORM 获取(如果它是现有实体)的域对象,您需要使用某种排序对象工厂而不是构造函数和指导您的 ORM 如何使用工厂

有关如何完成所有这些的具体细节取决于您使用的工具。

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);

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:

  • 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();
}

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 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.

The nitty gritty details about how to accomplish all of this will depend on which tools you are using.

从来不烧饼 2024-12-06 18:48:12

拦截可以用于很多事情 - 最值得注意的是解决跨领域问题,例如检测、日志记录、审计、安全、计量等。

不需要 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?

池木 2024-12-06 18:48:12

我想说你使用更通用的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文