Is it possible to register an interface component in Castle Windsor and get a proxy version of the implementation?

Consider some cases:

_windsor.Register(Component.For<IProductServices>().ImplementedBy<ProductServices>().Interceptors(typeof(SomeInterceptorType));

In this case, when I ask for IProductServices virtualization, the interface is proxied to intercept interface method calls. If I do this:

_windsor.Register(Component.For<ProductServices>().Interceptors(typeof(SomeInterceptorType));

then I can’t ask Windsor to allow IProductServices, instead I ask for ProductServices and it will return a dynamic subclass that intercepts virtual method calls. Of course, the dynamic subclass still implements "IProductServices"

My question is: can I register an interface component, as in the first case, and get proxy subclasses, as in the second case ?.

There are two reasons for me:
1 - Since the code that you are going to resolve cannot know about the ProductServices class, only about the IProductServices interface. 2 - Since some event calls that the sender passes as a parameter pass a ProductServices object, in the first case this object is a dynamic proxy field, and not the real object returned by the wind computer. Let me give an example of how this might complicate the situation: let's say I have a custom collection that does something when their elements notify me of a property change:

private void ItemChanged(object sender, PropertyChangedEventArgs e)
    {
        int senderIndex = IndexOf(sender);
        SomeActionOnItemIndex(senderIndex);
    }

This code will fail if I add an interface proxy, because the sender will be a field in the proxy interface, and indexOf (sender) will return -1.

+5
1

, :

_windsor.Register(Component.For<ProductServices, IProductServices>()
   .Interceptors(typeof(SomeInterceptorType));
+5

All Articles