日期:2014-05-18  浏览次数:20475 次

我在学习三层结构,今天写了个添加留言的程序,大家帮我看看代码,三层是这样做的吗?
我一共建了4个项目,分别是   BLL、DAL、Model、Web,(呵呵,学书上讲的)
代码下面列出。

我觉得那个   BLL   的业务逻辑层好像没什么用,好像只是做了个中转,为什么不让前台页面直接调用DAL里的guestbook.cs的方法,处理数据库呢?不理解!
还有,Model实体层,我也不懂这样做有什么好处?

小弟刚学,请大家抽出宝贵时间帮我看看,指点一下小弟,万分感谢!

1、Web层
只有一个页面,显示留言内容列表,和添加留言
using   System;
using   System.Collections;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Web;
using   System.Web.SessionState;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;
using   BLL;
using   Model;

namespace   GuestBook
{
///   <summary>
///   index   的摘要说明。
///   </summary>
public   class   index   :   System.Web.UI.Page
{
protected   System.Web.UI.WebControls.TextBox   TextBox1;
protected   System.Web.UI.WebControls.TextBox   TextBox2;
protected   System.Web.UI.WebControls.Button   Button1;
protected   System.Web.UI.WebControls.Label   Label1;
protected   System.Web.UI.WebControls.DataGrid   DataGrid1;

Business   bll=new   Business();
Common   com=new   Common();
Gb   gb=new   Gb();

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
GetList();
}

#region   Web   窗体设计器生成的代码
override   protected   void   OnInit(EventArgs   e)
{
//
//   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{        
this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);
this.DataGrid1.DeleteCommand   +=   new   System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Load   +=   new   System.EventHandler(this.Page_Load);

}
#endregion

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
gb.name=this.TextBox1.Text.Trim();
gb.content=this.TextBox2.Text.Trim();
if(bll.Add(gb))
{
this.Label1.Text= "添加成功! ";
this.TextBox1.Text= " ";
this.TextBox2.Text= " ";
}
else
{
this.Label1.Text= "添加失败! ";
}
GetList();
}

public   void   GetList()
{
this.DataGrid1.DataSource=bll.GetList();
this.DataGrid1.DataKeyField= "id ";
this.DataGrid1.DataBind();
}

private   void   DataGrid1_DeleteCommand(object   source,   System.Web.UI.WebControls.DataGridCommandEventArgs   e)
{
try
{
bll.Del(Convert.ToInt32(this.DataGrid1.DataKeys[e.Item.ItemIndex]));
Response.Write(com.Alert( "删除成功! "));
}
catch
{
Response.Write(com.Alert( "删除失败! "));
}
GetList();
}
}
}


2、BLL业务逻辑层Business.cs