M
Mike
I've got an ASP.NET web site that primarily uses the SqlDataSource for data
access tasks, and I am in the process of migrating it to use Business
Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my first
bash at OOP and I've hit a stumbling block.
I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called FillDataRecord,
and a basic method that calls a proc to get the top 15 articles:
public static ArticleList GetList()
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListForFrontPage");
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}
Now I need to return a list of articles by CategoryID:
public static ArticleList GetCategoryList(int categoryid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByCategory");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", categoryid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}
And again by ArticleTypeID:
public static ArticleList GetArticleTypeList(int articletypeid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByArticleType");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", articletypeid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}
The three methods are almost identical, and seem a good candidate for some
consolidation of code. However, I don't have a clue how I should go about
it. Any suggestions?
Thanks
Mike
access tasks, and I am in the process of migrating it to use Business
Objects based on Imar Spaanjaars's articles here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416. This is my first
bash at OOP and I've hit a stumbling block.
I'm returning multiple records as a generic list in my DAL. I've got a
helper method which takes care of filling each "row" called FillDataRecord,
and a basic method that calls a proc to get the top 15 articles:
public static ArticleList GetList()
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListForFrontPage");
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}
Now I need to return a list of articles by CategoryID:
public static ArticleList GetCategoryList(int categoryid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByCategory");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", categoryid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}
And again by ArticleTypeID:
public static ArticleList GetArticleTypeList(int articletypeid)
{
ArticleList tempList = null;
using (SqlConnection conn = new SqlConnection(Utils.GetConnString()))
{
SqlCommand cmd = new SqlCommand("GetArticleListByArticleType");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", articletypeid);
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows)
{
tempList = new ArticleList();
while (sdr.Read())
{
tempList.Add(FillDataRecord(sdr));
}
}
}
sdr.Close();
}
conn.Close();
return tempList;
}
The three methods are almost identical, and seem a good candidate for some
consolidation of code. However, I don't have a clue how I should go about
it. Any suggestions?
Thanks
Mike