On the right you find the list of articles published on this Web site in the category .NET 3.5 General conveniently split up in pages.
If you can't find what you're looking for, try another category from the sub menu
| Details |
|
|
| Site Section | Articles | |
| Site Section | .NET 3.5 General | |
| Number of
items |
7 | |
| Last Article Added |
9/20/2009 6:17 PM |
Below you find some of the books that I am currently reading.
Some time ago I was showing a colleague how to enhance an object's Design Time capabilities (or actually Debug Time) by adding a DebuggerDisplayAttribute. I blogged about this attribute earlier, so I won't go into it again now. But what I do want to talk about is the way the attribute gets its data.
It's not uncommon that you have a method that accepts an object that implements the IDataRecord or IDataReader interface. It's also not uncommon that you cannot (fully) control the query that drives the IDataRecord. It could be the result of a SELECT * operation (bad idea) or it could be the results of a Stored Procedure for example. Especially in the latter case, it can be useful if you can check whether the IDataRecord contains a specific field. For example, you may want to check if the IDataRecord has a field called LastModified before you try to read and store this field in a local DateTime variable.
Unfortunately, the IDataRecord and IDataReader do not implement this behavior directly. However, it's easy to add with a simple extension method.
For some reason, the IDataRecord interface and classes that implement it (DbDataReader, SqlDataReader and so on) only have Get* methods that accept the zero-based column index of a column in the result set. They don't allow you to get data by specifying a column name. As an example, consider the private FillDataRecord method to fill an e-mail address, as discussed in my article series about N-Layer development.
private static EmailAddress FillDataRecord(IDataRecord myDataRecord)
{
EmailAddress myEmailAddress = new EmailAddress();
myEmailAddress.Id =
myDataRecord.GetInt32(myDataRecord.GetOrdinal("Id"));
myEmailAddress.Email =
myDataRecord.GetString(myDataRecord.GetOrdinal("Email"));
// More fields here
return myEmailAddress;
}
The GetInt32 and GetString methods only have a single overload: one that accepts the zero-based index of the column. To satisfy these method signatures and make your code more readable at the same time, you can use GetOrdinal as shown in the previous example. Based on the column's name, GetOrdinal returns the column index. So, given the fact that the Id column is the first in the result set and Email the second, the previous piece of code equates to this:
myEmailAddress.Id = myDataRecord.GetInt32(0); myEmailAddress.Email = myDataRecord.GetString(1);
Clearly, this is much more difficult to read and maintain than the previous example as you need to know the column indices and you should take great care not to mess with the column order in your select statements.
With a few extension methods, you can have the short syntax of the latter example, but still have readable code as the first example.
How often have you written code that redirects to another page and passes some local variables? You probably use string.Format to make your code easier to read. E.g.:
int categoryId;
// Code to assign a value to categoryId here
Response.Redirect(string.Format("SomePage.aspx?CatId={0}", categoryId.ToString()));Have you ever wished there was an overload of the Redirect method that allowed you to omit the call to string.Format and simply let you write something like this:
int categoryId;
// Code to assign a value to categoryId here
Response.Redirect("SomePage.aspx?CatId={0}", categoryId.ToString());
This code makes it much easier to redirect to a page with a number of variables in the Query String. All you need to do is provide a composite format string as the new URL and a bunch of values that are used instead of the placeholders.
With Extension Methods - that come with .NET 3.5 - you can easily accomplish this yourself.
With .NET 2, Microsoft introduced something called DbProviderFactories that allows you to work with databases in a generic fashion. That is, you don't create strongly typed objects like a SqlConnection or an OleDbConnection at design-time, but defer the decision what object to create till run-time. This way, you can write code that works against a number of different databases, like SQL Server, Oracle and OleDb databases like Microsoft Access.
One of the big differences between how these databases operate is the way you must name the parameters you send to a stored procedure or query. For example, SQL Server uses the @ symbol (@userName) and even allows you to leave out a prefix altogether. Microsoft Access in turn uses a question mark without a specific name while Oracle uses a colon as the prefix. Now, how do you deal with these differences when you don't know what database you're working with till run-time?