Since I last posted, my wife and I have bought a new home, moved, sold our previous home, and ran the Chicago marathon. All while getting up the learning curve and taking on more responsibility at our new jobs (both started in June). Needless to say, I’ve had no time for tech blogging. Until now. OK, let’s get to it:

Dapper is a great micro-ORM library. It’s simple and highly performant. I love its Multi-Mapping feature, which simplifies mapping columns from a SQL result set into multiple object types. It has an annoyance though: The lambda method provided by the caller (to map objects) must be a function. That is, it must return a value. Often I simply need to map properties from a row into one or many Dictionaries, HashSets, or KeyedCollections. I don’t need the lambda method to return a value because I never intend to iterate over the IEnumerable returned by Dapper’s QueryAsync extension method. In other words, rather than writing this code:

I’d rather write this code:

For an explanation of how Dapper Multi-Mapping works, see the official documentation or the WebAPI Service Method section of my previous post, The Best Domain-Specific Language for Manipulating Data is SQL

Compare lines 14 and 17 in the first example above to line 16 in the second. The second example is more concise. The “map” lambda method doesn’t return an unused value. I realize this is a minor improvement. But it has a positive impact beyond merely scrubbing away a single line of code: It removes a misdirect that may cause a programmer unfamiliar with Dapper to wonder, “What am I supposed to return here? And what am I expected to do with the returned value?” If the answer is nothing, then the programmer shouldn’t be forced to return a value.

Unfortunately, Dapper’s extension methods for IDbConnection don’t support passing an Action, only a Func. Remember, Func<int, int, int> means “pass in two integer parameters and return an integer.” The return type is the last generic type parameter. Action<int, int> means “pass in two integer parameters and do not return anything.”

OK, let’s write our own extension methods.

And so on up to seven parameters.

You may review full source code of the extension methods in my Data project in GitHub. Or download the NuGet package. Refer to example code in my Sandbox project in GitHub that demonstrates how to use the extension methods.

Leave a Reply

Your email address will not be published.