July 2006 Entries
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.
I've just been working on the data access layer for a project at work and found myself
replicating alot of very similar code when it came to looping through the SqlDataReader
and creating and populating a custom object for each IDataRecord (creating lists of
projects, staff etc). I sat back and thought "there must be an easier way to write
not much code, after all this is the world of .NET and C#".
So here's my solution for returning a a generic List<> populated from a SqlDataReader.
public interface IObjectPopulator
{
object Populate(IDataRecord
dr);
}
public class Project
: IObjectPopulator
{
private int?
_id;
public int?
Id
{
get { return _id;
}
internal set
{ _id = value;
}
}
private string _name;
public...
A few months ago I purchased some X10 modules to automate my house, being able to
turn the lights on and off with a remote control from bed is probably the greatest
application so far.
But in the old house I wrote some software which interfaced with a CM12U device; from
my computer I was able to send commands via RS232 serial communication to the powerlines
in the house and so onto the modules planted around the house. Being a geek I made
it easy to make and run any macro (using the wonders of XML), a set of commands run
in order to set any number...
If you haven't tried databinding in .net 2.0 yet, give it a go.
IMAGE_00022
Originally uploaded by luk32001.
I went to the Madjeski Stadium to see the chilis play last night, it was a great concert.
Tho the 1.5 hour walk home wasn't too enjoyable.
I always forget to take my digital camera with me, so end up with not too great pictures
from my camera phone. Hopefully I'll remember for reading festival in August.
Theres a video up on channel9 of
the UK Imagine Cup team. Good luck to Andy and the others when they head of to India
in August.