新学期我换到了另一个项目组(之前那个只是为了完成“特定”的项目而临时组建的),靠的是和我一起做flash的那个朋友,之前也是他推荐我去那个组的。
这次的开发任务本来是相对简单的,留言系统,完全可以用我之前提问系统的模式。可我拿到代码时,我傻眼了。没有了我熟悉的sqldatasource,没有DataBase.cs没有Operation.cs,没有我熟悉的DataSet,SqlDataAdapter,没有了Fill(),没有了gv.DataSource= ;gv.DataBind();这种方式,我连怎么绑定数据都不会,仔细研究老师给我的代码,发现了ObjectDataSource,这个中间有些曲折,因为老师用vs2005,而我用的是vs2008,结果始终是找不到配置数据源的,试了很多方法,才知道是要删掉bin文件夹里面的有关配置,重新编译才行,而光知道这个就用了3天。老师参照的微软的petshop来做的简化框架,这个短时间根本无法学会,还好找到了“Scott Mitchell的ASP.NET2.0数据指南”才让我稍稍有些进展,使用了数据集的办法,才成功绑定到了ods上,然后做页面和功能,又碰到了GridView的索引行的问题,到了完成期限,才交了一个这样的畸形版,可以看出老师是不满意的。但他仔细研究了数据集,并提出了建议。还是将我这个半成品,自己用css布局并整合进了项目。
之后一段时间就比较低落了,觉得自己什么都做不好。但是我还是坚持在学习。研究3层,从最基本的3层做的登陆开始,慢慢深入了解。才了解了,实体类,dal,bll,ui等这些新的名词所代表的意义。期间还有些模式,架构,接口,反射等更加深的技术一股脑冲入我的大脑。让我有些晕晕乎乎。其中费了很大的劲才找到我需要的资料。
这是参照老师的实体类写出来的留言本的实体类。(当时也没注意到命名规范)

Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
///GuestBookDetails 的摘要说明
/// </summary>
namespace DWCourse.DAL
{
public class GuestBookDetails
{
#region 私有成员
protected int Id;
protected string MsgName = string.Empty;
protected string MsgEmail = string.Empty;
protected string Title = string.Empty;
protected string MsgContent = string.Empty;
protected bool IsShow = false;
protected string Reply = string.Empty;
protected string MsgDate = string.Empty;
#endregion
public GuestBookDetails()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public GuestBookDetails(int Id,string MsgName,string MsgEmail,string Title,string MsgContent,bool IsShow,string Reply,string MsgDate)
{
this.id = Id;
this.msgname = MsgName;
this.msgemail = MsgEmail;
this.title = Title;
this.msgcontent = MsgContent;
this.isshow = IsShow;
this.reply = Reply;
this.msgdate = MsgDate;
}
#region 共有方法
public int id
{
get { return Id; }
set { Id = value; }
}
public string msgname
{
get { return MsgName; }
set { MsgName = value; }
}
public string msgemail
{
get { return MsgEmail; }
set { MsgEmail = value; }
}
public string title
{
get { return Title; }
set { Title = value; }
}
public string msgcontent
{
get { return MsgContent; }
set { MsgContent = value; }
}
public bool isshow
{
get { return IsShow; }
set { IsShow = value; }
}
public string reply
{
get { return Reply; }
set { Reply = value; }
}
public string msgdate
{
get { return MsgDate; }
set { MsgDate = value; }
}
#endregion
}
}
因为老师用到了工厂模式,直接导致我更加难以理解=。=,费了很大的劲才理清了思路,下面是数据层的部分代码。

Code
// GuestBook抽象方法
public abstract List<GuestBookDetails> GetMessage();
//public abstract GuestBookDetails GetMessageById(int messageId);
public abstract bool DeleteMessage(int messageid);
public abstract bool UpdateMessage(string reply,int messageid);
public abstract int InsertMessage(GuestBookDetails message);
public abstract bool VerifyMessage(int messagid);
protected virtual GuestBookDetails GetMessageFromReader(IDataReader reader)
{
return new GuestBookDetails(
(int)reader["Id"],
reader["MsgName"].ToString(),
reader["MsgEmail"].ToString(),
reader["Title"].ToString(),
reader["MsgContent"].ToString(),
(bool)reader["IsShow"],
reader["Reply"].ToString(),
reader["MsgDate"].ToString());
}
protected virtual List<GuestBookDetails> GetMessageCollectionFromReader(IDataReader reader)
{
List<GuestBookDetails> message = new List<GuestBookDetails>();
while (reader.Read())
message.Add(GetMessageFromReader(reader));
return message;
}
然后是参照老师的SqlClient,添加了自己的方法

Code
public override List<GuestBookDetails> GetMessage()
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("dw_GuestBook_GetMessage", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
return GetMessageCollectionFromReader(ExecuteReader(cmd));
}
}
public override bool DeleteMessage(int messageid)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("dw_GuestBook_DeleteMessage", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MsgId", SqlDbType.Int).Value = messageid;
cn.Open();
int ret = ExecuteNonQuery(cmd);
return (ret == 1);
}
}
public override bool UpdateMessage(string reply,int messageid)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("dw_GuestBook_UpdateMessage", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MsgId", SqlDbType.Int).Value = messageid;
cmd.Parameters.Add("@reply", SqlDbType.VarChar).Value = reply;
cn.Open();
int ret = ExecuteNonQuery(cmd);
return (ret == 1);
}
}
public override bool VerifyMessage(int messagid)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("dw_GuestBook_VerifyMessage", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MsgId", SqlDbType.Int).Value = messagid;
cn.Open();
int ret = ExecuteNonQuery(cmd);
return (ret == 1);
}
}
public override int InsertMessage(GuestBookDetails message)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("dw_GuestBook_InsertMessage", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MsgName", SqlDbType.VarChar).Value = message.msgname;
cmd.Parameters.Add("@MsgEmail", SqlDbType.VarChar).Value = message.msgemail;
//MsgName,MsgEmail,Title,MsgContent,IsShow,MsgDate
cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = message.title;
cmd.Parameters.Add("@MsgContent", SqlDbType.VarChar).Value = message.msgcontent;
cmd.Parameters.Add("@IsShow", SqlDbType.Bit).Value = message.isshow;
cmd.Parameters.Add("@MsgDate", SqlDbType.Bit).Value = message.msgdate;
cn.Open();
int ret = ExecuteNonQuery(cmd);
return (int)cmd.Parameters["@MsgId"].Value;
}
}
下面是guestbook的业务层代码:

Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DWCourse.DAL;
namespace DWCourse.BLL.Documents
{
/// <summary>
///GuestBook 的摘要说明
/// </summary>
public class GuestBook:BaseDocument
{
#region 私有成员
protected int Id;
protected string MsgName = string.Empty;
protected string MsgEmail = string.Empty;
protected string Title = string.Empty;
protected string MsgContent = string.Empty;
protected bool IsShow = false;
protected string Reply = string.Empty;
protected string MsgDate = string.Empty;
#endregion
public GuestBook(int Id,string MsgName,string MsgEmail,string Title,string MsgContent,bool IsShow,string Reply,string MsgDate)
{
this.id = Id;
this.msgname = MsgName;
this.msgemail = MsgEmail;
this.title = Title;
this.msgcontent = MsgContent;
this.isshow = IsShow;
this.reply = Reply;
this.msgdate = MsgDate;
}
//public GuestBook()
//{
// //
// //TODO: 在此处添加构造函数逻辑
// //
//}
#region
public int id
{
get { return Id; }
set { Id = value; }
}
public string msgname
{
get { return MsgName; }
set { MsgName = value; }
}
public string msgemail
{
get { return MsgEmail; }
set { MsgEmail = value; }
}
public string title
{
get { return Title; }
set { Title = value; }
}
public string msgcontent
{
get { return MsgContent; }
set { MsgContent = value; }
}
public bool isshow
{
get { return IsShow; }
set { IsShow = value; }
}
public string reply
{
get { return Reply; }
set { Reply = value; }
}
public string msgdate
{
get { return MsgDate; }
set { MsgDate = value; }
}
#endregion
public bool Delete()
{
bool success = Document.DeleteDocument(this.DocId);
if (success)
this.DocId = 0;
return success;
}
public static List<GuestBook> GetMessage()
{
List<GuestBook> guestbook = null;
string key = "Documents_guestbook";
if (BaseDocument.Settings.EnableCaching && BizObject.Cache[key] != null)
{
guestbook = (List<GuestBook>)BizObject.Cache[key];
}
else
{
List<GuestBookDetails> recordset = SiteProvider.Documents.GetMessage();
guestbook = GetMessageListFromGuestBookDetailsList(recordset);
BaseDocument.CacheData(key, guestbook);
}
return guestbook;
}
private static List<GuestBook> GetMessageListFromGuestBookDetailsList(List<GuestBookDetails> recordset)
{
List<GuestBook> guestbook = new List<GuestBook>();
foreach (GuestBookDetails record in recordset)
// categories.Add(GetCategoryFromCategoryDetails(record));
guestbook.Add(GetMessageFromGuestBookDetails(record));
return guestbook;
}
private static GuestBook GetMessageFromGuestBookDetails(GuestBookDetails record)
{
if (record == null)
return null;
else
{
return new GuestBook(record.id,record.msgname,record.msgemail,record.title,record.msgcontent,record.isshow,record.reply,record.msgdate);
}
}
}
}
这个项目我至今还在继续研究中,换肤、反射、微软的登陆验证总之还有好多我不懂的技术等着我挖掘。
这个之后,我也就接了现在的系网开发,用到了更加简单的,但是自己写的3层结构。又由于某种原因,我暂时停止了开发,等完成后,希望能与前辈和朋友们交流。
附上"【翻译】Scott Mitchell的ASP.NET2.0数据指南中文版索引"地址:http://www.cnblogs.com/lovecherry/archive/2006/07/02/440840.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?