Luke Smith

Write today, wrong tomorrow

Creating a generic list from a SqlDataReader v1.1

Since writing the PopulateList method
I’ve made a few improvements, with some pointers from Erik
Porter
.

Firstly the interfaces can be passed a generic type, meaning IObjectPopulator
can be set to return a type T instead of a object type saving a cast in the PopulateList<T>
method.

public interface IObjectPopulator<T>
{
   T Populate(IDataRecord
dr);
}

Erik
pointed me to using dr.GetOrdinal and the dr.Get*Type* methods inside the implementation
of the Populate(SqlDataReader) methods.

This
little helper method really has saved me quite alot of time writing the backend dataaccess,
and reduces the errors that copy and pasteing always introduces.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.