Thursday, June 25, 2015

Poor man's interceptors: passthrough proxies

So let's say this is my greate setup:

And I want to log calls to the service without putting ugly logging lines inside the service or the calling object. How do I do this?

Well, you could go all the way with Ninject Interceptors - the way I normally do it:

Code here. Get both Nuget.Exensions.Interceptors and either the Castle or LinFu proxy package ( don't forget those ! ).

OK - that's nice - that's elegant. But I realised that there's also a more lightweight alternative to this - writing a passthroughproxy:

So now I have two implementations of IMyService: the PassThroughProxy and the MyService. The passthrough passes the call through to the injected IMyService but allows for that call to be overridden. That's what I've done in the LogAspect.
The LogAspect overrides the call - logs the call and calls its base method to propagate the call down to MyService.

Hooking this up goes as follows:

The reason I built something like this, is because I was trying to listen in on events. You can do the same thing for events, where you build a pass through proxy that propagates the events, but also logs. And I couldn't listen in on these with interceptors.

Code here

No comments:

Post a Comment