Paul Hiles
Paul Hiles

Reputation: 9778

Unity 2 Interception equivalent of CreateInterfaceProxyWithoutTarget in Castle Dynamic Proxy

I want to use Unity interception for an interface without an implementation, so that the IInterceptionBehavior actually becomes the implementation. This is the same as the CreateInterfaceProxyWithoutTarget method in Castle Dynamic Proxy.

Is this possible in Unity?

Upvotes: 0

Views: 931

Answers (2)

Xose Lluis
Xose Lluis

Reputation: 915

It's been a long while since this question was posted, but I came across it while trying to figure out just the same, and well, it seems like I've come up with something.

I've made it work by using Intercept.NewInstanceWithAdditionalInterfaces. In my sample below I want a Unity to create an instance of a non defined class implementing IUserDao. All I want that instance of that Unity created class to do is defined in RetrieveSavedResultBehavior.

IUserDao userDao = (IUserDao)(Intercept.NewInstanceWithAdditionalInterfaces<Object>(
            new VirtualMethodInterceptor(),
            new List<IInterceptionBehavior>(){new RetrieveSavedResultBehavior()},
            new List<Type>() { typeof(IUserDao) }
            ));

You can check my post regarding this topic here

Upvotes: 0

onof
onof

Reputation: 17367

I think no, the only way I know to intercept an interface is with an InterfaceInterceptor but it requires a base class to be registered in the container:

Container.RegisterType<IRepository, BaseRepository>(
    "repo1",
    new Interceptor(new InterfaceInterceptor()),
    new InterceptionBehavior(new RepoLoggingBehavior())
    );

Upvotes: 3

Related Questions