Functional ADO.NET
- Posted: Sep 02, 2007 at 9:41 AM
- 5,159 Views
- 2 Comments
Download
How do I download the videos?
- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”
Why should I download videos from Channel9?
- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!
Which version should I choose?
- If you want to view the video on your PC, Xbox or Media Center, download the High Quality WMV file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality WMV file.
- If you have a Zune, WP7, iPhone, iPad, or iPod device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.
Right click “Save as…”
- Mid Quality WMV (Lo-band, Mobile)
In this screencast, I present a functional approach to using ADO.NET that wraps most of the boiler-plate code into a Database class.
It makes use of anonymous methods to allow the developer to provide the required code at different points in the ADO.NET code.
The result is a syntactically lightweight way to interact with a database.
The code used in the project can be downloaded here:
C# 3.0 http://www.aboutcode.net/screencasts/functional-ado.3.zip
C# 2.0 http://www.aboutcode.net/screencasts/functional-ado.2.zip (same as 3.0 but with non-inferred anonymous methods).
Comments Closed
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Instead of:
reader =>
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}",
reader.GetInt32(0),
reader.GetString(1),
reader.GetString(2));
}
});
I sugest to abstract away the reader usage and put it inside the DataBase.ExecuteReader(...) method and replace the last parameter Action<IDataReader> action with Action<IDataRecord> and iterate inside the method, like so :
).
replace :
...
using (IDataReader reader = cmd.ExecuteReader())
{
action(reader);
}
...
with :
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read()) {
action(reader);
}
}
so:
reader =>
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}",
reader.GetInt32(0),
reader.GetString(1),
reader.GetString(2));
}
});
becomes:
record =>
{
Console.WriteLine("{0}\t{1}\t{2}",
record .GetInt32(0),
record .GetString(1),
record .GetString(2));
});
... and all loops are gone inside user code. (perfect candidate for thread level parallelism now
Other than that nice vid and good way of using the old ado.net.
A good approach would be to have an overloaded method. One that passes the reader, another that passes each data record.
I hope people find this pattern useful and adapt it to meet their needs as you have done
Also, does anyone know if this pattern has a name?
Remove this comment
Remove this thread
close