1

let's think of an Interface like this:

public interface ITest
{
    void DoSomething();
}

I don't want to implement this interface, and ask Ninject to generate a proxy class implementing it. Then Intercept DoSomething method. Is it possible?

themehrdad
  • 169
  • 4
  • 12
  • Thanks to BatteryBackupUser, I found the solution this way:
    kernel.Bind(typeof(ITest)).To(typeof(Class1)).Intercept().With((IInterceptor)new MyInterceptor());
    Class1 is an empty class and MyInterceptor must implement IInterceptor interface.
    – themehrdad Nov 27 '13 at 10:24
  • Why don't you want to implement that interface. Something is smelly about this. Can you explain what your actual scenario is? Why don't you want to implement that interface? – Steven Nov 27 '13 at 21:10
  • @Steven: Here's the full scenario: Preparing a framework for a customer. I want them to define their own interfaces for their services like: IEmployeeService which has a method like : GetAllEmployees. Then they have to implement this service for the server side using WCF. but my framework will automatically implement the interface to generate client side proxy class using ninject and having exception handling, logging,... Therefore the real case is that I don't know about their interfaces that they are going to define in the future. :) – themehrdad Nov 28 '13 at 08:20
  • In that case it's perhaps better to make the system message-driven and give your customer one single generic interface to implement, such as `ICommandHandler`. That makes it very easy to add all sorts of cross-cutting concerns on top of this, without having to fallback to all sorts of nasty interception. Take a look at [this](http://bit.ly/s7tGEH) and [this](http://bit.ly/s3UUyv) for instance. – Steven Nov 28 '13 at 08:26
  • @Steven: I did this exactly as you said. But I prefer give them a client proxy class having their exact method signatures defined in their interfaces. I translate their method call to a single generic service (like you said) and at the server side, translate it to their own service class. I just made WCF invisible for them. – themehrdad Nov 28 '13 at 12:24

1 Answers1

0

As far as i know, out of the box ninject does not support interception proxies without an implementation (i.E. class inheriting from the interface). If you want an interface proxy with implementation only by interceptors, no class, then you could use castle.dynamicproxy "interface proxy without target" (see http://docs.castleproject.org/Tools.Kinds-of-proxy-objects.ashx), but you would have to implement the binding sugar yourself.

Alternatively you could try to use https://github.com/ninject/ninject.extensions.interception and .Bind().ToConstant(Mock.Of).Intercept().With(new SomeInterceptor);.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68