1

From Unity documentation:

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.

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

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

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

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?

Community
  • 1
  • 1
Alexander Christov
  • 9,625
  • 7
  • 43
  • 58
  • Related: https://stackoverflow.com/a/13471092/264697 – Steven Aug 27 '15 at 14:25
  • Related: https://stackoverflow.com/a/23591824/264697 – Steven Aug 27 '15 at 14:25
  • Yes, your post is related to my question. Thanks. In addition many new and emerging frameworks (ASP.NET 5 / MVC 6 and AngularJS to name just two of them) offer DI out of the box. – Alexander Christov Aug 27 '15 at 15:04
  • Therefore, a decision whether to use an AOP tool requires clear answers to some of the following: what we get in addition, what's done better, is there extra functionality helpful in some scenarios we may have, etc. In fact I am looking for an answer why should we use AOP, provided we have DI out of the box? In this respect my question is somewhat broader than what you answered in the quoted question. Anyway, thanks again! – Alexander Christov Aug 27 '15 at 15:10
  • 1
    I would say you SHOULD use AOP, but AOP is not the same as code weaving. Code weaving is just one way of applying AOP. What my answers describe is that good (SOLID) application design will facilitate the use of AOP even WITHOUT the use of ANY framework (so no DI library, no code weaving tool). After applied the right principles, you can think about using a tool to help you. But no tool can make up for bad design. – Steven Aug 27 '15 at 15:21
  • Note that whether something is 'better' is highly subjective and you might find this question to be closed because of this. By now however, you probably guessed that my opinion is that AOP through decorators is superior to both code weaving and interception. – Steven Aug 27 '15 at 20:32

1 Answers1

1

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

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.

Consider the following code:

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

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

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) It might not be a good idea to use a DI container in the first place. See my article here

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62