Passed My MCPD Web Exams Today

Posted by: Imar Spaanjaars at Tuesday 08/05/2008 11:39 in: Blogs - Imar's Blogs
Today I passed the exam 70-528, part of the MCPD training.
Read On...

Spaanjaars.Toolit.ContentRating: Version 1.1

Posted by: Imar Spaanjaars at Wednesday 07/16/2008 20:29 in: Articles - ASP.NET 3.5

Ever since I wrote the initial version of my ContentRating control back in 2006, I received a massive amount of feedback, both as comments below the article and as private e-mails. Not surprisingly, if you consider the article has been read over 19,000 times and has been rated 444 times (at the time of writing).

Besides getting a lot of "thank you's" from people who liked the control, I also got a lot of requests for a real-world example of a test site using the control. The test site that shipped with the control used fake data stored in ViewState to simulate a real backing store which obviously didn't cut it for a lot of people.

Also, a reader called vgt pointed out a bug in the control where an existing cookie would be overwritten by a new one one, effectively allowing you to vote for the previous item again.

Finally, I had a few requests of my own: I didn't like the default data source of 5 integers if you didn't supply a data source yourself. I also didn't like it that the control didn't raise an exception when you tried to data bind it without a valid data source.

So, enough reasons to fire up Visual Studio and get my hands dirty on some control fixing.

Read On...

Fun With Extension Methods - Extending Object Part 1

Posted by: Imar Spaanjaars at Monday 07/14/2008 21:02 in: Articles - .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...

Caching Best Practices

Posted by: Imar Spaanjaars at Monday 07/14/2008 15:40 in: Blogs - Imar's Blogs
Shortly after my ASP.NET 3.5 book was released in March 2008, I received feedback from Steven Smith who had reviewed the book. He pointed out that the code I am using to retrieve cached objects has a flaw. Fortunately, the problem is only likely to occur in high traffic sites, and is pretty easy to fix.
Read On...

Fun With Extension Methods - Extending IDataRecord Part 2

Posted by: Imar Spaanjaars at Wednesday 05/14/2008 19:27 in: Articles - .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...

Blogo.NET : A Real World Implementation of my N-Layer Architecture

Posted by: Imar Spaanjaars at Wednesday 04/30/2008 21:31 in: Blogs - Imar's Blogs
Ferdy Christant (also from the Netherlands) recently developed Blogo.NET, a blogging application built with .NET 3.5 and based on the architecture from my article series on N-Layer design.
Read On...

Sometimes the Error Message Says It All...

Posted by: Imar Spaanjaars at Saturday 04/05/2008 19:29 in: Blogs - Imar's Blogs

Sometime ago I was working on a Web Application Project in Visual Studio 2008. The web site I was working on couldn't run against the built-in web development server, so I ran the application against IIS 7 on my Windows Vista machine (IMO, that's probably the best reason to upgrade to Windows Vista if you are a web developer: multiple web sites in IIS 7).

Life was good until I started debugging...

Read On...

Uploaded MyGeneration Templates for C# Code for my N-Layer Design Articles

Posted by: Imar Spaanjaars at Saturday 04/05/2008 18:29 in: Blogs - Imar's Blogs

I just uploaded a ZIP file with four MyGeneration templates to generate C# and SQL code that matches the model in my articles about N-Layer design.

Read On...

How to Check if Two Objects Look Like Each Other Without Using Equals

Posted by: Imar Spaanjaars at Tuesday 03/18/2008 22:27 in: Articles - ASP.NET 3.5

A colleague (from Design IT) and I were discussing a simple way to check two instances of an object. We wanted to know if all the public properties on one instance were holding the same values as the one on the other instance. We wanted to use this knowledge in a few unit tests to simply check all public fields on an instance in one fell swoop.

Since we didn't want this exact behavior at run-time we couldn't override Equals and check all object's properties, so we had to look for a different solution.

Read On...

Fun With Extension Methods - Extending IDataRecord

Posted by: Imar Spaanjaars at Monday 03/17/2008 18:39 in: Articles - .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 = 
          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.

Read On...