【发布时间】:2010-05-25 13:27:53
【问题描述】:
我希望在我正在开发的几个应用程序中实现一些更好的方法来使用 List。我当前的实现看起来像这样。
MyPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
BLL.PostCollection oPost = new BLL.PostCollection();
oPost.OpenRecent();
rptPosts.DataSource = oArt;
rptPosts.DataBind();
}
BLL 类
public class Post
{
public int PostId { get; set; }
public string PostTitle { get; set; }
public string PostContent { get; set; }
public string PostCreatedDate { get; set; }
public void OpenRecentInitFromRow(DataRow row)
{
this.PostId = (int) row["id"];
this.PostTitle = (string) row["title"];
this.PostContent = (string) row["content"];
this.PostCreatedDate = (DateTime) row["createddate"];
}
}
public class PostCollection : List<Post>
{
public void OpenRecent()
{
DataSet ds = DbProvider.Instance().Post_ListRecent();
foreach (DataRow row in ds.Tables[0].Rows)
{
Post oPost = new Post();
oPost.OpenRecentInitFromRow(row);
Add(oPost);
}
}
}
现在虽然这一切都很好,但我只是想知道是否有任何方法可以改进它,并让它更清晰,因为必须使用两个不同的类来做一些我认为可以在一个中发生的事情类或使用接口。
【问题讨论】:
-
有没有办法让你的 DB 层返回表格以外的东西(例如,使用数据读取器来填充你的对象列表)?
-
这可能是个人喜好,但我也会从您的页面代码中删除匈牙利符号。
-
@Paddy - 我正在努力!我只将它用于一些可以帮助我记住的东西,例如中继器。但这一切都在慢慢从代码中剔除。