About Me

Training

Develop With Passion® - Developer Bootcamp

Subscribe

Explicit Strongly Typed Selective Proxies - Part 2

Written January 30, 2008 at 18:27 MST Tagged c sharp

This is a very short post to demonstrate the concept that I mentioned in parting during the last post.

'And of course I could eliminate the method call tracker altogether if I made use of lambda expressions and parsed the expression tree to see what method was called'

Someone fired an email asking how to do this so I thought I would quickly implement to show them the result. You can now also set filters on the interceptor by use of a lambda expression. So instead of this (and you can still do this):

IInterceptorConstraint<IAnInterface>constraint = sut.AddInterceptor<SomeInterceptor>(); constraint.InterceptOn.OneMethod();  

or this:

IInterceptorConstraint<IAnInterface> constraint = sut.AddInterceptor<SomeInterceptor>(); constraint.InterceptOnMethods(constraint.InterceptOn.ValueReturningMethodWithAnArgument(1));  

You can now use a lambda expression which negates the need to store a reference to the constraint to apply constraints:

sut.AddInterceptor<SomeInterceptor>()
   .InterceptCallTo(x => x.OneMethod())
   .InterceptCallTo(x => x.SecondMethod());

The InterceptCallTo method accepts an ExpressionTree of type Action where T is the target to be proxied. The code to parse this expression tree into a method call is very simple. The InterceptorConstraint class just forwards the call onto its MethodCallTracker:

public IInterceptorConstraint<Target> InterceptCallTo(Expression<Action<Target>> expression)
{
    callTracker.AddCallInferredFrom(expression);
    return this;
}

Notice that the method returns the InterceptorConstraint itself, which is what allows for the very limited fluent interface. As far as parsing the expression to store the method to be intercepted on, it is all done by the MethodCallTracker itself:

public void AddCallInferredFrom(Expression<Action<T>> expression)
{
    MethodCallExpression methodCallExpression = expression.Body as MethodCallExpression;
    if (methodCallExpression != null) AddCallFor(methodCallExpression.Method.Name);
}

That's it, that's all. The AddCallFor method is now called from both here and the original Interception tracker to store the list of methods that should be intercepted on.

Develop with Passion!!