DI 拦截 vs. AOP [英] DI Interception vs. AOP

查看:20
本文介绍了DI 拦截 vs. AOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Unity 文档:

From Unity documentation:

Unity 拦截使您能够有效地捕获对对象的调用并向目标对象添加附加功能.当您想修改单个对象而不是整个类的行为时,拦截很有用,这与使用 装饰模式.它提供了一种在运行时向对象添加新行为的灵活方法.

Unity interception enables you to effectively capture calls to objects and add additional functionality to the target object. Interception is useful when you want to modify the behavior for individual objects but not the entire class, very much as you would do when using the Decorator pattern. It provides a flexible approach for adding new behaviors to an object at run time.

由于在面向方面的编程中使用了完全相同的 DP(请参阅此处)

Since the very same DP is used in Aspect Oriented Programming (see here)

...在.NET Framework 中,这些技术中最常用的是后处理和代码拦截.前者是 PostSharp 使用的技术,后者被依赖注入 (DI) 容器使用,例如 Castle DynamicProxy 和 统一.这些工具通常使用名为 Decorator 或 Proxy 的设计模式来执行代码拦截.

...In the .NET Framework, the most commonly used of these techniques are post-processing and code interception. The former is the technique used by PostSharp and the latter is used by dependency injection (DI) containers such as Castle DynamicProxy and Unity. These tools usually use a design pattern named Decorator or Proxy to perform the code interception.

在这些引文中,Proxy 被用作 Decorator 的同义词,尽管它们非常相似,但有一些 差异.

In these quotations Proxy is used as a synonym of Decorator despite they are very alike there are some differences.

所以,我的问题是:什么(最重要的是为什么)AOP 必须优于 DI 拦截?或者 DI 拦截是一种更好的方式来增强对象的功能而不改变其实现?总的来说,如果一个应该比另一个更受欢迎,那么为什么?

So, my question is: What (and most importantly why) AOP has to be preferred over DI interception? Or is DI interception a better way to augment a object's functionality without changing its implementation? In general, if one should be preferred over the other then why?

推荐答案

以下是使用 DI 容器拦截的一些限制:

Here are some of the limitations with using DI container interception:

1) 使用 DI 容器的拦截仅限于使用容器创建的对象.

1) Interception using a DI container is limited to objects that were created using the container.

例如,您可能有一个在容器中注册的工厂.此类工厂创建的对象将不支持拦截.

For example, you might have a factory that you register with your container. Objects that such factory create will not have interception support.

考虑以下代码:

public interface IHelperFactory
{
    IHelper CreateHelper();
}

public interface IHelper
{
    void HelpMe();
}

class Helper : IHelper
{
    public void HelpMe()
    {

    }
}

class HelperFactory : IHelperFactory
{
    public IHelper CreateHelper()
    {
        return new Helper();
    }
}

如果在容器中注册HelperFactory,则可以拦截CreateHelper方法,但是不会拦截对IHelper.HelpMe的调用.

If you register HelperFactory with the container, then the CreateHelper method can be intercepted, however, calls to IHelper.HelpMe will not be intercepted.

2) 通过 DI 容器拦截的另一个限制是您无法拦截私有方法,除非您将它们更改为虚拟保护方法并使用 虚拟方法拦截器,反过来又不允许你拦截对象注册为实例.

2) Another limitation of interception through DI containers is that you cannot intercept private methods unless you change them to become virtual protected methods and use virtual method interceptors, which in turn disallow you to intercept objects registered as instances.

3) (IMO) 首先使用 DI 容器可能不是一个好主意.请参阅我的文章此处

3) (IMO) It might not be a good idea to use a DI container in the first place. See my article here

这篇关于DI 拦截 vs. AOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆