需要OOP建议 [英] OOP advice needed

查看:83
本文介绍了需要OOP建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET网站主要使用SqlDataSource来获取数据

访问任务,我正在将其迁移到使用Business

对象基于Imar Spaanjaars的文章:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416 。这是我在OOP上的第一个

bash,我遇到了一个绊脚石。


我正在将多个记录作为通用列表返回给我DAL。我有一个

辅助方法,它负责填充每个行。叫做FillDataRecord,

和一个调用proc获取前15篇文章的基本方法:


public static ArticleList GetList()

{

ArticleList tempList = null;

使用(SqlConnection conn = new SqlConnection(Utils.GetConnString()))

{

SqlCommand cmd = new SqlCommand(" GetArticleListForFrontPage");

cmd.CommandType = CommandType.StoredProcedure;

conn.Open();

使用(SqlDataReader sdr = cmd.ExecuteReader())

{

if(sdr.HasRows)

{

tempList = new ArticleList();

while(sdr.Read())

{

tempList.Add (FillDataRecord(sdr));

}

}

}

sdr.Close();

}

conn.Close();

返回tempList;

}


现在我需要按类别ID返回文章列表:


public static ArticleList GetCategoryList(int categoryid)

{

ArticleList tempList = null;

使用(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();

返回tempList;

}


再次由ArticleTypeID:


public static ArticleList GetArticleTypeList(int articletypeid)

{

ArticleList tempList = null;

using(SqlConnection conn = new Sql Connection(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();

返回tempList;

}


三种方法是几乎相同,似乎是一些代码合并的好b候选。但是,我不知道怎么回事

呢。有什么建议吗?


谢谢


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

推荐答案

Ysgrifennodd Mike:
Ysgrifennodd Mike:

我有一个ASP.NET网站主要使用SqlDataSource来获取数据

访问任务,而我我正在迁移它以使用商业

基于Imar Spaanjaars的文章的对象:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416 。这是我在OOP上的第一个

bash,我遇到了一个绊脚石。


我正在将多个记录作为通用列表返回给我DAL。我有一个

辅助方法,它负责填充每个行。叫做FillDataRecord,

和一个调用proc获取前15篇文章的基本方法:

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:



有什么理由吗为什么你不能使用DataSet(或Typed DataSet,

甚至更好的恕我直言)?这就是他们的目的。


可重用性以及所有这些;)

Peter

Is there any reason why you can''t use DataSets (or Typed DataSets, which
are even better IMHO)? It''s what they''re for.

Re-usability and all that ;)
Peter


真的不是OOP的答案,但它在这里并不合适:)


尝试编写一个带有此签名的函数


private static ArticleList GetArticleList(string storedProcedureName,

List< SqlParameterparameters)...


然后用上面的每个函数包装这个函数。


really isnt an OOP answer, but its not really appropriate here :)

Try writing a function that with this signature

private static ArticleList GetArticleList(string storedProcedureName,
List<SqlParameterparameters)...

then wrap this function with each of the ones you have above.





" Peter Bradley" < p。******* @ dsl.pipex.com写信息

news:7 _ ******************** *@pipex.net ...

"Peter Bradley" <p.*******@dsl.pipex.comwrote in message
news:7_*********************@pipex.net...

Ysgrifennodd Mike:
Ysgrifennodd Mike:

>我有一个主要使用SqlDataSource进行数据访问任务的ASP.NET网站,我正在将其迁移到使用基于Imar Spaanjaars的文章的商业对象:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc = 416个。这是我在OOP的第一次打击,我遇到了绊脚石。

我在DAL中将多条记录作为通用列表返回。我有一个
辅助方法,负责填充每个行。叫做FillDataRecord,以及一个调用proc获取前15篇文章的基本方法:

>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:



有什么理由吗为什么你不能使用DataSet(或Typed DataSet,

甚至更好的恕我直言)?这就是他们的目的。


可重用性和所有这些;)


Is there any reason why you can''t use DataSets (or Typed DataSets, which
are even better IMHO)? It''s what they''re for.

Re-usability and all that ;)



完全没有,除了我还没看过那个(Typed Datasets)。我是

首先尝试着解决这个问题。


Mike

None at all, except that I haven''t looked at that yet (Typed Datasets). I''m
trying to get my head around this approach first.

Mike


这篇关于需要OOP建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆