Welcome to imar.spaanjaars.com

image
Welcome to the personal web site of Imar Spaanjaars where I write about software development with a focus on Microsoft web technologies
Image representing the Blogs category

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

Published 15 years ago

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 ...
Image representing the Articles category

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

Published 16 years ago

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 ...
Image representing the Articles category

Fun With Extension Methods - Extending IDataRecord

Published 16 years ago

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 ...
Image representing the Articles category

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

Published 16 years ago

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 ...
Image representing the Blogs category

My Books Have Finally Arrived!

Published 16 years ago

After almost a year of hard work, today was the day: my new ASP.NET book arrived.

Read on ...
Image representing the Blogs category

www.planetwrox.com Now Live!!

Published 16 years ago

The demo web site that is used in my new book - www.planetwrox.com - is now available on-line.

Read on ...
Image representing the Blogs category

Grrrr, I Hate automatic Spam Detection Systems

Published 16 years ago

I just spent some time answering a question I received from someone called Bill who found an old thread on the p2p.wrox.com forum. 10 seconds after I sent my reply, I received the following automatic reply.

Read on ...
Image representing the Snippets category

Dirty Tricks for Dirty Problems

Published 16 years ago

A while ago I needed the ability to display a "no image available" image in a list of products when an image for that product was not available on the server.

Read on ...
Image representing the Blogs category

Talking About My Generation

Published 16 years ago

Ever since I posted my article series about N-Layer design, people asked me if I use any code generators to create the basic framework for the classes used in my model. The usual answer was: yes, I do, but I use a home grown code generator for it, making it impossible to share it with others. Fortunately, that has changed....

Read on ...
Image representing the Blogs category

Woohoo It's Done!!!

Published 16 years ago

A few days ago I sent in the last corrections for my new book Beginning ASP.NET 3.5 with C# and VB.

Read on ...

Mobile: False

Crawler: True

I: False