Where and how to use interceptors in web application?

血红的双手。 提交于 2019-12-03 13:13:39

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.

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!