tags:

views:

89

answers:

3

Is there a way to implement this pattern in a generic way?

A dispatcher object and a bunch of worker objects all derive from the same interface.

Any method call into the dispatcher object needs to be dispatched (forwarded) to one of the worker objects (with all the arguments).

Each method would need to discover it's own name, find the corresponding method in the worker objects, discover the arguments, and then make the call. If possible, not using the variable argument mechanism.

Is there some way to do this? Reflection? Code generation?

+3  A: 

It might not be particularly simple, but it's very solid - take a look at Castle.DynamicProxy: http://kozmic.pl/archive/2009/04/27/castle-dynamic-proxy-tutorial.aspx

Rob Fonseca-Ensor
A: 

If you'd like to take the code-gen route, this t4 file does something similar: http://gist.github.com/647885

Rob Fonseca-Ensor
+1  A: 

One possible approach would be to have each method in the dispatcher object raise an event, and have all the worker objects subscribe to that event. (Think this is called a "multicast delegate" pattern).

I suppose this might not be quite as "generic" as you're looking for, but could be a simpler way to achieve mostly the same ends.

Tom Bushell