Interface capture methods in the Ninject Interception Extension

I play with the Ninject Interception extension. an Ian Davis blog post about this indicates that the interception is always based on the actual type of service, not the interface. For example, the following code will have no effect since IFoo is an interface:

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

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

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

It looks like a pretty bright hole when it comes to Aspect-oriented programming. I was pretty conscientious in programming interfaces so that we could use mocking frameworks to mock our various services, but the vast majority of my real-world implementations are not virtual. If the mocking framework can create IFoo using a method that does what I ask, it seems like Ninject should be able to.

So, I think my question is double:

  • Is there any reason Ninject Interception doesn't allow you to bind to interface methods?
  • Is there an easy way to bind Ninject to dynamic wrapper classes that can allow me to perform certain interception operations in all interface methods and then pass the call to the actual implementation?
+4
source share
1 answer

I took several steps about this, and it seems that this can be obtained in the extension of the interception. But, as we planned to release 2.2 in the very near future, you should be a little patient. I encourage as a change, so I planned to add it in 2.4. Also, the spike is far from productive. All current unit tests work. But there are many new things to add with this feature. If you like, I can send you a patch, but I will not give you any support or guarantees that at the moment this is a mistake.

+5
source

Source: https://habr.com/ru/post/1339095/


All Articles