Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

Can't see a genuine use for this, but I got the idea in my head and wanted to work it through. It's an extension to the idea of duck typing, for scenarios where types have similar behaviour, but implemented in differently-named members.

So you may have a set of objects you want to treat as an interface, which don't implement the interface explicitly, and don't have the same member names so they can't be duck-typed into implicitly implementing the interface. In a fictitious example, I want to call Get on whichever ICache implementation is current, and have the call passed through to the relevant method – whether it's called Read, Retrieve or whatever:

A sample implementation is up on github here: PassthroughSample.

This uses Castle's DynamicProxy behind the scenes in the same way as my duck typing sample, but allows you to configure the passthrough to specify how the inner (implementation) and outer (interface) members are mapped:

 

    var setup = new Passthrough();
    var cache = setup.Create("PassthroughSample.Tests.Stubs.AspNetCache, PassthroughSample.Tests")
                            .WithPassthrough("Name", "CacheName")
                            .WithPassthrough("Get", "Retrieve")
                            .WithPassthrough("Set", "Insert")
                            .As<ICache>();

- or using some ugly Lambdas to avoid the strings :

    Expression<Func<ICache, string, object>> get = (o, s) => o.Get(s);
    Expression<Func<Memcached, string, object>> read = (i, s) => i.Read(s);
    Expression<Action<ICache, string, object>> set = (o, s, obj) => o.Set(s, obj);
    Expression<Action<Memcached, string, object>> insert = (i, s, obj) => i.Put(s, obj);
 
    ICache cache = new Passthrough<ICache, Memcached>()
                    .Create()
                    .WithPassthrough(o => o.Name, i => i.InstanceName)
                    .WithPassthrough(get, read)
                    .WithPassthrough(set, insert)
                    .As();

 

- or even in config:

  ICache cache = Passthrough.GetConfigured<ICache>();
...
 <passthrough>
    <types>
      <typename="PassthroughSample.Tests.Stubs.ICache, PassthroughSample.Tests"
            passesThroughTo="PassthroughSample.Tests.Stubs.AppFabricCache, PassthroughSample.Tests">
        <members>
          <membername="Name"passesThroughTo="RegionName"/>
          <membername="Get"passesThroughTo="Out"/>
          <membername="Set"passesThroughTo="In"/>
        </members>
      </type>

 

Possibly useful for injecting stubs for dependencies in tests, when your application code isn't using an IoC container. Possibly it also has an alternative implementation using .NET 4.0 dynamic objects, rather than the dynamic proxy.

 
 
Posted on Thursday, May 13, 2010 7:51 PM Code Gallery | Back to top


Comments on this post: Passthrough Objects – Duck Typing++

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Nice post. This is a great blog and I have to congratulate you on the content
Left by Acai Berry on CBS on Jan 11, 2011 5:17 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
hey this was a great post. I really liked what you had to say
Now I know it and will tell to my friends about this blog
Left by News 5 Acai Berry on NBC on Feb 11, 2011 6:17 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
You rock it up and keep rocking.
Left by Colon Cleanse on CBS on Feb 25, 2011 8:14 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Your blog posts looks unique and informative. Thanks for sharing this.
Left by ABC Channel 7 Jobs on Feb 27, 2011 3:16 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Your blog posts looks unique and informative. Thanks for sharing this.
I have invested my time in reading a wonderful post like this
Left by Work at Home on CBS on May 07, 2011 2:32 PM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
I will visit again, keep updating your blogs. I like it.

Acai Berry Diet Channel 8
Left by Acai Berry Diet Channel 8 on Jun 30, 2011 6:14 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Terrific way of expressing those things in your post. Clear cut meanings.Channel 5 Acai Berry
Left by Channel 5 Acai Berry on Jun 30, 2011 6:38 PM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...

There is obviously a lot to know about this.I think you made some good points in Features also.
get rid of yeast
Left by avena bailey on Sep 20, 2011 6:36 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
you are right when you say this.Hats off man,what a superlative knowledge you have on this subject.hope to see more work of yours.thanks
how to get rid of double chin with exercise


Left by ewan lewis on Sep 20, 2011 6:37 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Advantageously, the article is really the best on this notable topic. I harmonize with your conclusions and will thirstily look forward to your approaching updates HCG Diet Belly Fat
Left by HCG Diet Belly Fat on Oct 24, 2011 9:42 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Wonderful posts you have on your blog and I have bookmarked you and will visit yours often. HCG Diet
Left by HCG Diet on Oct 27, 2011 8:14 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Im impressed. Youre truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Im saving this for future use.
Left by price for tantric massage on Nov 11, 2011 1:36 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Thanks so much for the resources, really great material here! With fusion, I think you guys have reached a new level with Drupal Theming - not sure if I would subtheme off anything else again at the moment.
Left by tantric massage on Apr 23, 2012 5:32 PM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...

Looking forward to learn some more from you. Keep it up.
Left by sulejman-velicanstveni.info on Nov 23, 2012 1:56 PM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Well your site is very informative and it will be equally worthwhile.Well thanks for this information it will really gonna help a lot of people.

CSCS Consultant
www.cslinks.co.uk
Left by CSCS Test on Jul 24, 2013 9:40 AM

# re: Passthrough Objects – Duck Typing++
Requesting Gravatar...
Well your site is very informative and it will be equally worthwhile.Well thanks for this information it will really gonna help a lot of people.

Left by www.cslinks.co.uk on Jul 24, 2013 9:41 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net