Ed previously blogged about our
null
-propagating extension
method. This extension method is
one example of a pattern that I find very interesting:
Another interesting example of this pattern is DisposeAfter
:
DisposeAfter
is an extension method constrained to types which implement
IDisposable
. It lets you pass in work to be done
before calling Dispose
, and returns the result of that work.
Why is this useful?
Let’s look at a simple case of reading a row of data from an IDataReader
:
This is one case where using an anonymous type instance to encapsulate the read data could be
useful. Doing so would enable us to know if data was read; we could simply
return null
when it wasn’t. It would also somewhat simplify the method by
reducing the number of local variables.
There’s a big problem though: if we declare the anonymous type instance inside
the using
block, its scope will be constrained. But we cannot declare the
anonymous type separately from its initialization.
We want to say something like:
But that won’t compile.
Enter DisposeAfter
:
There, that’s nice! Since DisposeAfter
is a generic method, the return value
is strongly typed. And it doesn’t matter to the compiler that we’re returning
an anonymous type instance; it can still infer the type of result.
Looking at the implementation of DisposeAfter
we see that all of the passed
in work is still done within a using
statement, so objects will properly be
disposed (even in the face of troublesome exceptions):
Let me know in the comments if you find this method useful. Hopefully you’ll also start to think about other circumstances where passing a delegate (or perhaps an expression tree) to an object via an extension method could be useful.
Posted by Jacob Carpenter on February 04, 2008