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

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

问题描述

来自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 装饰器或代理的设计模式来执行代码拦截.

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

所以,我的问题是: 相对于DI拦截,必须优先选择哪种(最重要的为什么)AOP?还是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天全站免登陆