服务器动态网站设计(三)
本文介绍“皖北牛肉当涂专卖店”网站设计过程,该网站为服务端动态网站,服务器端程序采用.NET技术编写,数据库管理系统采用Access。本文只针对新闻表News介绍项目的三层结构设计模式,其它表的操作类似,在此不作介绍。
1. 项目设计
需求分析及数据库设计
本网站需求和数据库设计同“企业网站设计任务书(二)”。
系统分层设计
分层是一种工程方法并非开发技术。工程方法是从实践中来,再回到实践中去,是为了解决实际工程问题的。分层就是将功能进行分组,化繁为简。并不是每个系统都要分层,一般只针对一些大型系统才采用分层方法开发。如果是小型项目且在今后不需要移植到其它数据库管理系统或操作界面,完全没有必要采用分层设计开发。
1.分层目的
多人协作开发。 代码复用。
方便维护管理、移植。 2.三层结构
表示层 业务逻辑层 数据访问层 数据库 Model 表示层(Presentation Layer,PL) 为客户提供交互的应用服务界面,呈现业务逻辑层中传递的数据,驱动业务逻辑层的业务操作。
业务逻辑层:(Business Logic Layer,BLL): 按业务需求调用数据访问层中的方法访问数据层。从数据层取数据、修改数据以及删除数据,并将结果返回给表现层。如果说数据层是积木,那逻辑层就是对这些积木的搭建。
数据访问层(Data Access Layers,DAL):
为业务逻辑层或表示层提供数据服务。该层只是对数据进行增、删、改、查询和判断存
在等单纯的数据访问方法,它是对数据的操作,而不是数据库本身。
数据实体类(Model)
数据实体类对应数据库相应的数据表,在业务逻辑层和数据访问层中要引用数据实体类。数据实体类没有行为,仅用于表现对象的数据。将数据实体与相关的数据表分离符合面向对象的精神。
3.七层结构
表示层 BLL DALFactory IDAL OleDbDAL DBUtility 数据库 Model 将数据访问层细分为DALFactory、IDAL、OleDbDAL和DBUtility四层,再将Model也作为一层看待,就构成七层的结构。
在本网站中,用一个类库实现业务逻辑层,类库名为BLL。用DALFactory、IDAL、OleDbDAL和DBUtility四个类库实现数据访问层。此外还有一实体类库Modal。数据访问层中各子层的功能如下:
DALFactory:提供对象的工厂。通过调用该类库,可以创建对象实例,这些实例
的数据类型是IDAL接口类型。
IDAL:为业务层的业务对象提供一个一致的接口。 OleDbDAL:实现IDAL接口类方法,支持数据访问。
DBUtility:为OleDbDAL类提供访问数据库的公共方法,实现数据库操纵能力。
2. Model类库
新建解决方案、项目和类文件
1.在“新建项目”对话框中,选择“Visual C#”中的“类库”项目,并设置项目名称为Model、位置为E:\\Example、解决方案名称WBWebS3;
2.删除默认新建的Class1.cs文件。
3.在上述项目中添加其它类库项目,在添加时,应选择“加入解决方案”。
创建News类
1.添加类文件News.cs; 2.查看类关系图;
3.在类关系图中,为News类添加属性、构造函数。添加后的代码如下:
using System;
using System.Collections.Generic;
using System.Linq; using System.Text;
namespace Model {
class News {
///
private string _Content; ///
private DateTime _issuedate; ///
private string _author; ///
private string _picpath;
///
/// 编号 /// 标题 /// 内容 /// 日期 /// 作者 /// 图片
public News(int nID, string nTitle, string nContent, DateTime nIssueDate, string nAuthor, string nPicPath) {
_id = nID; _title = nTitle; _Content = nContent; _issuedate = nIssueDate; _author = nAuthor; _picpath = nPicPath; }
///
return _id; } set {
_id = value; } }
///
return _title; } set {
_title = value; } }
///
return _Content; }
set {
_Content = value; } }
///
public DateTime IssueDate { get {
return _issuedate; } set {
_issuedate = value; } }
///
return _author; } set {
_author = value; } }
///
return _picpath;
} set {
_picpath = value; } } } }
添加Product、Message等其它数据对象类
1. 添加Product、Message等其它数据类操作同上一小节,在此省略。
2. 鼠标右击“Model”项目,执行“生成”命令,编译生成Model.dll文件,该文件在
Web\\bin下有一复本。
3. DBUtility类库
DBUtility提供访问数据库的公共方法,实现数据库操纵能力。本类库项目只包含一个类文件OleDbHelper.cs,类名为OleDbHelper,代码如下:
using System;
using System.Collections.Generic; using System.Text;
using System.Configuration; using System.Data; using System.Data.OleDb; using System.Web;
namespace DBUtility {
public class OleDbHelper {
public OleDbHelper() { }
/// /// 返回数据库连接字符串 ///
///
public static String GetSqlConnection() {
System.Web.UI.Page page = new System.Web.UI.Page();
String conn = ConfigurationSettings.AppSettings[\"OleDbConnectionString\"].ToString() + page.Server.MapPath(\"~/\" + ConfigurationSettings.AppSettings[\"AccessDbPath\"].ToString());
return conn; }
///
/// 参数名称 /// 数据类型 /// 长度 /// 源列名称 /// 参数实值 ///
public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Int32 paramSize, String ColName, Object paramValue) {
OleDbParameter param = new OleDbParameter(paramName, paramType, paramSize, ColName); param.Value = paramValue; return param; }
///
/// 参数名称 /// 数据类型 /// 长度 /// 源列名称 ///
public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Int32 paramSize, String ColName) {
OleDbParameter param = new OleDbParameter(paramName, paramType, paramSize, ColName); return param; }
///
/// 参数名称 /// 数据类型 /// 长度 /// 源列名称 ///
public static OleDbParameter GetParameter(String paramName, OleDbType paramType, Object paramValue)
{
OleDbParameter param = new OleDbParameter(paramName, paramType); param.Value = paramValue; return param; }
///
/// SQL语句 /// 参数对象数组 ///
public static int ExecuteSql(String Sqlstr, OleDbParameter[] param) {
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr)) {
OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; cmd.CommandText = Sqlstr; cmd.Parameters.AddRange(param); conn.Open();
cmd.ExecuteNonQuery(); conn.Close(); return 1; } }
/// /// 执行SQL语句并返回数据表 ///
/// SQL语句 ///
public static DataTable ExecuteDt(String Sqlstr) {
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr)) {
OleDbDataAdapter da = new OleDbDataAdapter(Sqlstr, conn); DataTable dt = new DataTable(); conn.Open(); da.Fill(dt); conn.Close(); return dt;
} }
/// /// 执行SQL语句并返回数据表 ///
/// SQL语句 /// 参数对象列表 ///
public static DataTable ExecuteDt(String Sqlstr, OleDbParameter[] param) {
String ConnStr = OleDbHelper.GetSqlConnection();
using (OleDbConnection conn = new OleDbConnection(ConnStr)) {
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(); OleDbCommand cmd = new OleDbCommand(Sqlstr, conn); cmd.Connection = conn;
cmd.Parameters.AddRange(param); da.SelectCommand = cmd; conn.Open(); da.Fill(dt); conn.Close(); return dt; } }
///
/// SQL语句数组 /// SQL参数对象数组 ///
public static Int32 ExecuteSqls(String[] Sqlstr, List String ConnStr = OleDbHelper.GetSqlConnection(); using (OleDbConnection conn = new OleDbConnection(ConnStr)) { OleDbCommand cmd = new OleDbCommand(); OleDbTransaction tran = null; cmd.Transaction = tran; try { conn.Open(); tran = conn.BeginTransaction(); cmd.Connection = conn; cmd.Transaction = tran; Int32 count = Sqlstr.Length; for (Int32 i = 0; i < count; i++) { cmd.CommandText = Sqlstr[i]; cmd.Parameters.AddRange(param[i]); cmd.ExecuteNonQuery(); } tran.Commit(); return 1; } catch { tran.Rollback(); return 0; } finally { cmd.Dispose(); conn.Close(); } } } } } 4. IDAL类库 IDAL类库为业务层的业务对象提供一个一致的接口,在此主要用于实现数据访问接口,每一个数据对象对应于一个接口,为此,必须引用Model类。以下给出INews接口代码,读者自行完成其它数据对象的接口。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Model; namespace IDAL { public interface INews { /// /// 添加新闻(不含图片) /// /// 新闻对象 void Insert(Model.NewsInfo newsinfo); /// /// 添加新闻(包含图片) /// /// 新闻对象 void InsertWithPic(Model.NewsInfo newsinfo); /// /// 获取若干个NewsInfo对象的集合 /// /// 获取前n条新闻。若n=0表示获取所有记录 IList /// /// 新闻编号 NewsInfo RetrieveOne(int nID); /// /// 获取若干个NewsInfo对象的集合 /// /// 表示在标题或内容中包含的查询关键词 IList /// /// 需更新的新闻对象 void Update(NewsInfo newsinfo); /// /// 新闻编号 void Delete(int nID); /// /// 获取最大新闻编号 /// 5. OleDbDAL类库 OleDbDAL用于实现IDAL接口类方法,它需要引用IDAL、DBUtility、System.Data.OleDb、System.Data名空间。以下给出News类的代码,它继承了INews接口,读者自行完成其它数据对象的接口。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Model; using IDAL; using DBUtility; using System.Data.OleDb; using System.Data; namespace OleDbDAL { public class News :INews { /// /// 新闻对象 public void Insert(Model.NewsInfo newsinfo) { string strSQL = \"insert into News(nTitle,nContent,nIssueDate,nAuthor) values(@Title,@Content,@IssueDate,@Author)\"; OleDbParameter[] param = { OleDbHelper.GetParameter(\"@Title\",OleDbType.VarChar,newsinfo.Title), OleDbHelper.GetParameter(\"@Content\",OleDbType.VarChar,newsinfo.Content), OleDbHelper.GetParameter(\"@IssueDate\",OleDbType.Date,newsinfo.IssueDate), OleDbHelper.GetParameter(\"@Author\",OleDbType.VarChar ,newsinfo.Author), }; OleDbHelper.ExecuteSql(strSQL, param); } /// /// 添加新闻(PicPath不为空) /// /// 新闻对象 public void InsertWithPic(Model.NewsInfo newsinfo) { string strSQL = \"insert into News(nTitle,nContent,nIssueDate,nAuthor,nPicPath) values(@Title,@Content,@IssueDate,@Author,@PicPath)\"; OleDbParameter[] param = { OleDbHelper.GetParameter(\"@Title\",OleDbType.VarChar,newsinfo.Title), OleDbHelper.GetParameter(\"@Content\",OleDbType.VarChar,newsinfo.Content), OleDbHelper.GetParameter(\"@IssueDate\",OleDbType.Date,newsinfo.IssueDate), OleDbHelper.GetParameter(\"@Author\",OleDbType.VarChar ,newsinfo.Author), OleDbHelper.GetParameter(\"@PicPath\",OleDbType.VarChar,newsinfo.PicPath) }; OleDbHelper.ExecuteSql(strSQL, param); } /// /// 获取若干个NewsInfo对象的集合 /// /// 获取前n条新闻对象。若n=0表示获取所有记录 public IList IList strSQL = \"select * from news order by nIssueDate desc,nID desc\"; else strSQL = \"select top \" + n + \" * from news order by nIssueDate desc,nID desc\"; DataTable dt = new DataTable(); dt = OleDbHelper.ExecuteDt(strSQL); DataRow dr; for (int i = 0; i < dt.Rows.Count; i++) { dr=dt.Rows[i]; if (dr[5] is DBNull) dr[5] = string.Empty; NewsInfo temp = new NewsInfo( (int)dr[0], (string)dr[1], (string)dr[2], (DateTime)dr[3], (string)dr[4], (string)dr[5]); list.Add(temp); } return list; } /// /// 新闻编号 public NewsInfo RetrieveOne(int nID) { string strSQL = \"select * from news where nID=\"+nID; DataTable dt = new DataTable(); dt = OleDbHelper.ExecuteDt(strSQL); DataRow dr= dt.Rows[0]; if (dr[5] is DBNull) dr[5] = string.Empty; NewsInfo temp = new NewsInfo( (int)dr[0], (string)dr[1], (string)dr[2], (DateTime)dr[3], (string)dr[4], (string)dr[5]); return temp; } /// /// 获取若干个NewsInfo对象的集合 /// /// 表示在标题或内容中包含的查询关键词 public IList IList string strSQL = \"select * from news where nTitle like '%\"+c+\"%' or nContent like '%\"+c+\"%'\"; DataTable dt = new DataTable(); dt = OleDbHelper.ExecuteDt(strSQL); DataRow dr; for (int i = 0; i < dt.Rows.Count; i++) { dr = dt.Rows[i]; if (dr[5] is DBNull) dr[5] = string.Empty; NewsInfo temp = new NewsInfo( (int)dr[0], (string)dr[1], (string)dr[2], (DateTime)dr[3], (string)dr[4], (string)dr[5]); list.Add(temp); } return list; } /// /// 需更新的新闻对象 public void Update(NewsInfo newsinfo) { string strSQL = \"update news set nTitle=@Title,nContent=@Content,nIssueDate=@IssueDate,nAuthor=@Author,nPicPath=@PicPath where nID = @ID\"; OleDbParameter[] param = { OleDbHelper.GetParameter(\"@Title\",OleDbType.VarChar,newsinfo.Title), OleDbHelper.GetParameter(\"@Content\",OleDbType.VarChar,newsinfo.Content), OleDbHelper.GetParameter(\"@IssueDate\",OleDbType.Date,newsinfo.IssueDate), OleDbHelper.GetParameter(\"@Author\",OleDbType.VarChar ,newsinfo.Author), OleDbHelper.GetParameter(\"@PicPath\",OleDbType.VarChar,newsinfo.PicPath), OleDbHelper.GetParameter(\"@ID\",OleDbType.Integer,newsinfo.ID) }; OleDbHelper.ExecuteSql(strSQL, param); } /// /// 新闻编号 public void Delete(int nID) { string strSQL = \"delete news where nID=@ID\"; OleDbParameter[] param = { OleDbHelper.GetParameter(\"@ID\",OleDbType.Integer,nID), }; OleDbHelper.ExecuteSql(strSQL, param); } /// int m; string strSQL = \"select max(nID) as a from news\"; DataTable dt=new DataTable(); dt=OleDbHelper.ExecuteDt(strSQL); m = (int)dt.Rows[0][0]; return m + 1; } } } 6. DALFactory DALFactory类是一工厂类,通过该调用,可以创建对象实例。需要注意的是,这些实例的数据类型是数据访问接口类型,而非普常见的实例。下代码只以创建新闻实例,其它实例的创建基本相同。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using IDAL; namespace DALFactory { class DataAccess { /// public static INews CreateNews() { OleDbDAL.News news = new OleDbDAL.News(); return news; } } } 7. BLL类库 业务逻辑层主要用于实现应用程序的业务逻辑。当表示层向业务逻辑层发送请求时,BLL将调用相应的逻辑处理此请求,根据处理情况,常常还需调用数据访问层对数据进行操作。通常,为每一个实体构造一个类文件,以下为News类的代码。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Model; using DALFactory; using System.Web; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; using System.IO; namespace BLL { public class News { private readonly IDAL.INews news = DataAccess.CreateNews(); /// /// 添加新闻(不含图片) /// /// 新闻对象 public void Insert(NewsInfo newsinfo) { news.Insert(newsinfo); } /// /// 新闻对象 public void InsertWithPic(NewsInfo newsinfo,FileUpload fu) { string fName = news.GetMaxID().ToString()+ Path.GetExtension(fu.FileName); newsinfo.PicPath=fName; System.Web.UI.Page page = new System.Web.UI.Page(); string path = page.Server.MapPath(@\"~\\newsImages\\\") + fName; news.InsertWithPic(newsinfo); try { fu.SaveAs(path); } catch { news.Delete(newsinfo.ID); } } /// /// 获取若干个NewsInfo对象的集合 /// /// 获取前n条新闻对象。若n=0表示获取所有记录 public IList return news.Retrieve(n); } /// /// 新闻编号 public NewsInfo RetrieveOne(int nID) { return news.RetrieveOne(nID); } /// /// 获取若干个NewsInfo对象的集合 /// /// 表示在标题或内容中包含的查询关键词 public IList return news.Retrieve(c); } /// /// 需更新的新闻对象 public void Update(NewsInfo newsinfo) { news.Update(newsinfo); } /// /// 新闻编号 public void Delete(int nID) { news.Delete(nID); } } } 8. 表示层 1.获取并显示数据 News objNews = new News(); IList 2.添加新闻 News objNews = new News(); NewsInfo ni = new NewsInfo(3, \"abc\", \"cba\", DateTime.Now, \"aaa\", \"ccc.jpg\"); objNews.InsertWithPic(ni, FileUpload1); 3.更新新闻 News objNews = new News(); NewsInfo ni = new NewsInfo(112, \"zzz\", \"cba\", DateTime.Now, \"aaa\", \"ccc.jpg\"); objNews.Update(ni); 9. 小结 参见目录
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务