Automatically Generating Class Diagrams from a Type Using Reflection

Posted by: Imar Spaanjaars at Sunday, September 20, 2009 4:17:54 PM in: .NET 3.5 General
Before I launched my web site in early 2009 I generated most of the content automatically using reflection and web look ups. I extracted all the various attributes from .NET assemblies using the Managed GAC API Wrappers and I extracted a lot of class details and documentation from the MSDN site using the HttpWebRequest class. One thing I hadn't automated at first and was planning to do manually using Visual Studio was generating class diagrams. However, soon after the first full import attempt I ended up with more than 1,200 attributes so manually creating the diagrams suddenly did not seem so attractive anymore.
Read on ...

Fun With Extension Methods - Extending Object Part 1

Posted by: Imar Spaanjaars at Monday, July 14, 2008 7:02:55 PM in: .NET 3.5 General

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.

Read on ...

Fun With Extension Methods - Extending IDataRecord Part 2

Posted by: Imar Spaanjaars at Wednesday, May 14, 2008 5:27:19 PM in: .NET 3.5 General

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.

Read on ...

Fun With Extension Methods - Extending IDataRecord

Posted by: Imar Spaanjaars at Monday, March 17, 2008 5:39:00 PM in: .NET 3.5 General

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 = 
  myEmailAddress.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.

Read on ...

Fun With Extension Methods - Extending String to Provide a Better Split Method

Posted by: Imar Spaanjaars at Sunday, March 2, 2008 2:46:13 PM in: .NET 3.5 General
Do you feel that the standard Split implementation of the String class is a bit awkward to use? Do you keep forgetting you have to declare a char array for the separator? And do you often need to split on multiple characters, like \r\n to split on a line break? In that case, read on. A simple extension method might fix that for you.
Read on ...

Fun With Extension Methods - Extending Response.Redirect

Posted by: Imar Spaanjaars at Friday, August 31, 2007 10:17:25 PM in: .NET 3.5 General

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.

Read on ...

Retrieving the Parameter Marker Format When Using Generic Database Factories

Posted by: Imar Spaanjaars at Tuesday, September 19, 2006 7:57:27 PM in: .NET 3.5 General

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?

Read on ... 1