Intercept interface methods in Ninject Interception Extension

927 views Asked by At

I'm playing around with the Ninject Interception extension. Ian Davis's blog post about it indicates that interception is always based on the actual service type, rather than the interface. For example, the following code will have no effect because IFoo is an interface:

Kernel.InterceptBefore<IFoo>(f => f.DoSomething(), 
    i => Console.WriteLine("before"));

And of course, the next code piece will only work if Foo.DoSomething is virtual:

Kernel.InterceptBefore<Foo>(f => f.DoSomething(), 
    i => Console.WriteLine("before"));

This seems like a pretty glaring hole when it comes to Aspect-Oriented programming. I've been pretty conscientious about programming to interfaces so that we could use mocking frameworks to mock our various services, but the vast majority of my actual method implementations are not virtual. If a mocking framework can produce an IFoo with a method that does what I ask for, it seems like Ninject ought to be able to.

So I guess my question is two-fold:

  1. Is there any reason Ninject Interception doesn't allow you to bind to interface methods?
  2. Is there an easy way to make Ninject bind to dynamic "wrapper" classes that can let me perform certain interception actions on all the interface methods, and then pass the call through to the real implementation?
1

There are 1 answers

2
Remo Gloor On BEST ANSWER

I did some spiking on this and it seems that it is possible to get that behavior into the interception extension. But as we planned to do the 2.2 release in the very near future you have to be a little patient. I defenately like the change so I planned to add it to 2.4. Also the spike is far from productive. All current unit test are running though. But there are a lot of new ones that need to be added with this feature. If you like I can send you a patch but I won't give you any support and guaranties that it is bug free at the moment.