Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Using Anonymous delegates as a replacement for a Switch Statement

Recently I was writing some code that interacted with a third party API for doing searching.  This API would return me a single result object fore each found result for my search, but the result object it returned me was a dictionary style object.  This object would contain 1-n different key/value pairs that would contain all sorts of data.  After examining the results for a few searches I realized I only needed a few pieces of data from the result.  Like I said before since the result was a Disctionary object Dictionary<string, object > I knew that in order to grab all the values out of the object I would have to implement my own switch statement and look for each key one by one.

As I started to write this switch statement I started to wonder if there was another way of doing this.  After some brain storming with a co-worker we decided it could be done using Anonymous Delegates.  Below I will show the old way (using switch statements) and the new way (using delegates)

 
Here is the fake result object model that I will pull the data out of.

 
Here is the object model that I will populate with the data from the fake result (yes i know, not well formed, but this is a POC after all)

Here is the code needed in order to use a switch statement

Here is the code using the anonymous delegates:
Setting up the needed variables

Building the needed mapper object

Performing the actual mapping

 
Now, I know that using the switch statement is easy to read and very easy to understand compared to the anonymous delegates, but this was just an fun little exercise to see if there is a different way to do this.  I would consider using the anonymous delegate route if my setting actions were simple and if the case block would be very lengthly.

 Has anyone else done something similar?  Have any suggestions on how to do this better?

 

 


Posted 03-31-2007 2:44 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Eber Irigoyen wrote re: Using Anonymous delegates as a replacement for a Switch Statement
on 03-31-2007 5:54 PM
Derik Whittaker wrote re: Using Anonymous delegates as a replacement for a Switch Statement
on 03-31-2007 8:04 PM

Eber,

Thanks for the post.  I took a look at yours and although the concept is the same, the implementation is different.  Your report factory is a nice use and is a pretty clean way to use delegates in the factory pattern.  

What I was tying to show here is how you can reduce the need for a very large (ugly) case statement with a few lines of code....

Thanks again,

Greg Ennis wrote re: Using Anonymous delegates as a replacement for a Switch Statement
on 04-01-2007 10:08 PM

How about using a simple mapping of expected keys to dataclass property names, i.e.,

"A" -> "Property1"

"B" -> "Property2"

Then you use reflection to set the value on the dataclass according to the mapped property.

Should be minimal amount of code, and easily read and maintained as well.

Derik Whittaker wrote re: Using Anonymous delegates as a replacement for a Switch Statement
on 04-02-2007 6:15 AM

Greg,

That is an great idea.  And you are right, with attributes and reflection this would be pretty easy.  

James Curran wrote re: Using Anonymous delegates as a replacement for a Switch Statement
on 04-02-2007 11:35 AM

Interesting idea, although I think the title of the article is misleading.

The important part of the algorithm is that you are storing delegates into a dictionary (indexed by the value which triggers them).  The fact that you happened to use anonomous delegates is irrelevant.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)