About Me

Training

Nothin But .Net Developer Bootcamp

Navigation

Search

Categories

On this page

Explicit Strongly Typed Selective Proxies - Part 2

Archive

Blogroll

 Agile Developer Venkat's Blog
 Ayende @ Blog
 B#
 Barry Gervin's Software Architecture Perspectives
 Boy Meets World
 Brad Abrams
 Canadian Developers
 Christopher Steen
 Claritude Software News
 Clemens Vasters: Enterprise Development and Alien Abductions
 Coding Horror
 Coding in an Igloo
 Dare Obasanjo aka Carnage4Life
 Darrell Norton's Blog [MVP]
 David Hayden [MVP C#]
 Don Box's Spoutlet
 Eric Gunnerson's C# Compendium
 EZWeb guy: Jeffrey Palermo [C# MVP]
 Fear and Loathing
 Generalities & Details: Adventures in the High-tech Underbelly
 Greg Young [MVP]
 Greg's Cool [Insert Clever Name] of the Day
 IanG on Tap
 Ingo Rammer's Weblog
 ISerializable - Roy Osherove's Blog
 James Kovacs' Weblog
 Jason Haley
 Jean-Luc David
 Jeremy D. Miller -- The Shade Tree Developer
 JetBrains .NET Tools Blog
 Jimmy Nilsson's weblog
 John Bristowe's Weblog
 John Papa [MVP C#]
 Jon Skeet's Coding Blog
 JonGalloway.ToString()
 Jump the Fence or Walk Around
 Lambda the Ultimate - Programming Languages Weblog
 Larkware News
 Lutz Roeder
 Marquee de Sells: Chris's insight outlet
 Martin Fowler's Bliki
 Mike Nichols - SonOfNun Technology
 MSDN Magazine - .NET Matters
 MSDN Magazine - All Articles
 OdeToCode Blogs
 Onion Blog
 Planet TW
 Raymond Lewallen [MVP]
 Rockford Lhotka
 RodMan's Corner
 Roger Johansson's blog
 Sahil Malik - blah.winsmarts.com
 Sam Gentile's Blog
 Scott Bellware [MVP]
 Scott Hanselman's Computer Zen
 ScottGu's Blog
 secretGeek
 Service Station, by Aaron Skonnard
 Signum sine tinnitu--by Guy Kawasaki
 Stephen Toub
 Steve Eichert's Blog
 Steven Rockarts
 The Blog Ride
 The Coding Hillbilly
 The Daily WTF
 TheServerSide.net: News
 Tim Gifford
 Vance Morrison's Weblog
 you've been HAACKED

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 431
This Year: 1
This Month: 1
This Week: 1
Comments: 1158

 Wednesday, January 30, 2008
Wednesday, January 30, 2008 7:27:34 PM (Mountain Standard Time, UTC-07:00) ( 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<T> 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!!