Saturday, 10 August 2013

Refactoring techniques for data access layer

Refactoring techniques for data access layer

I've got a data access layer to which I am binding some controls.
Currently I have something along the lines of
public List<Race> GetRaces()
{
List<Race> raceList = new List<Race>();
using (var con = new SqlConnection(this.ConnectionString))
{
using (var cmd = new SqlCommand("spGetRace",con))
{
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Race r = new Race();
r.RaceId = Convert.ToInt32(rdr["raceId"]);
r.RaceDescription =
rdr["RaceDescription"].ToString();
raceList.Add(r);
}
}
return raceList;
}
}
public List<Ses> GetSes()
{
List<Ses> sesList = new List<Ses>();
using (var con = new SqlConnection(this.ConnectionString))
{
using (var cmd = new SqlCommand("spGetSes",con))
{
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Ses s = new Ses();
s.SesId = Convert.ToInt32(rdr["SesId"]);
s.SesDescription = rdr["SesDescription"].ToString();
sesList.Add(s);
}
}
return sesList;
}
}
which will be bound to drop down lists in my presentation layer. Instead
of having to type the lion's share of this ADO.NET code over and over,
what are some useful refactoring techniques for this basic type of data
access? Can I do this by refactoring with a SqlConnection, SqlCommand, and
one of my custom types Race/Ses as a parameter?

No comments:

Post a Comment