我一共建了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.csusing System;
using System.Data;
using DAL;
using Model;namespace BLL
{
/// <summary>
/// Business 的摘要说明。
/// </summary>
public class Business
{
guestbook gb=new guestbook(); public Business()
{
} #region 留言
/// <summary>
/// 添加留言
/// </summary>
/// <param name="mygb"></param>
/// <returns></returns>
public bool Add(Gb mygb)
{
return gb.InsertGB(mygb);
} /// <summary>
/// 读取全部留言
/// </summary>
/// <returns></returns>
public DataSet GetList()
{
return gb.GetGB();
} /// <summary>
/// 删除留言
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool Del(int id)
{
return gb.DelGB(id);
} #endregion
}
}
代码下面列出。我觉得那个 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.csusing System;
using System.Data;
using DAL;
using Model;namespace BLL
{
/// <summary>
/// Business 的摘要说明。
/// </summary>
public class Business
{
guestbook gb=new guestbook(); public Business()
{
} #region 留言
/// <summary>
/// 添加留言
/// </summary>
/// <param name="mygb"></param>
/// <returns></returns>
public bool Add(Gb mygb)
{
return gb.InsertGB(mygb);
} /// <summary>
/// 读取全部留言
/// </summary>
/// <returns></returns>
public DataSet GetList()
{
return gb.GetGB();
} /// <summary>
/// 删除留言
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool Del(int id)
{
return gb.DelGB(id);
} #endregion
}
}
解决方案 »
- GridView如何在显示时进行值处理
- FCK富文本出现错误,有请高人出场。。。。
- ASP.NET 网站,闪屏制作?
- 急!!!如何获取服务器HtmlTableCell 的ID
- 请老师帮我推荐几本ASP.NET+VB方面的书籍!
- TextBox的服务端OnTextChanged事件之后,怎么样可以让光标focus到下一个控件?
- asp.net 中的ClientScriptManager对象 创建问题??
- 关于GridView自动生成字段的问题,郁闷啊。。。各位大虾帮帮小弟吧
- 为什么数据不刷新?
- 做了个东东,想买个域名个空间,各位兄弟知道哪里去买好些.
- 如何在更新数据库时,同时更新缓存的数据(不丢弃缓存再重新读取数据来缓存)
- Server.MapPath()中参数的用法?
using System.Data;
using System.Data.SqlClient;
using Model;namespace DAL
{
/// <summary>
/// guestbook 的摘要说明。
/// </summary>
public class guestbook
{ SqlServer SqlDB = new SqlServer(); public guestbook()
{} #region 留言本 /// <summary>
/// 添加留言
/// </summary>
public bool InsertGB( Gb gb )
{
SqlParameter[] prams = {
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@content",SqlDbType.Text)};
prams[0].Value = gb.name;
prams[1].Value = gb.content;
try
{
SqlDB.ExecuteProc("gb_add",prams);
return true;
}
catch
{
return false;
}
} /// <summary>
/// 读取留言
/// </summary>
public DataSet GetGB()
{
DataSet ds = new DataSet();
string sql = "select * from gb";
try
{
ds = SqlDB.ExecuteSqlDataSet(sql);
return ds;
}
catch
{
return null;
}
} /// <summary>
/// 删除留言
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool DelGB(int id)
{
SqlParameter[] prams = {new SqlParameter("@id",SqlDbType.Int,4)};
prams[0].Value = id;
try
{
SqlDB.ExecuteProc("gb_del",prams);
return true;
}
catch
{
return false;
}
}
#endregion }
}
4、DAL下SqlServer.csusing System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace DAL
{
/// <summary>
/// SqlServer 的摘要说明。
/// </summary>
public class SqlServer
{
protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
protected static SqlConnection Conn;
protected static SqlCommand Comm; public SqlServer()
{
//
// TODO: 在此处添加构造函数逻辑
//
} #region 打开数据库 Open()
public void Open()
{
Conn = new SqlConnection(connectionString);
Conn.Open();
}
#endregion #region 关闭数据库 Close()
public void Close()
{
if ( Conn.State.ToString() == "Open" )
{
Conn.Dispose();
Conn.Close();
}
}
#endregion #region 执行SQL语句并返回受影响行数 ExecuteSql(string strSQL)
/// <summary>
/// 功能:执行SQL语句并返回受影响行数(使用范围:Insert、Update、Delete等)
/// </summary>
/// <param name="strSQL">sql语句</param>
public int ExecuteSql(string strSQL)
{
int i = -1;
Comm = new SqlCommand(strSQL,Conn);
try
{
Open();
i = Comm.ExecuteNonQuery();
}
catch
{
i = -1;
}
finally
{
Comm.Dispose();
Close();
}
return i;
}
#endregion #region 执行SQL语句并返回DataSet ExecuteSqlDataSet(string strSQL)
/// <summary>
/// 功能:返回一个DataSet结果集
/// </summary>
public DataSet ExecuteSqlDataSet(string strSQL)
{
DataSet ds = new DataSet();
try
{
Open();
SqlDataAdapter da = new SqlDataAdapter(strSQL,Conn);
da.Fill(ds);
}
catch
{
}
finally
{
Close();
}
return ds;
}
#endregion #region 执行SQL语句并返回SqlDataReader ExecuteSqlDataReader(string strSQL)
/// <summary>
/// 功能:返回一个SqlDataReader
/// </summary>
public SqlDataReader ExecuteSqlDataReader( string strSQL )
{
Comm = new SqlCommand(strSQL,Conn);
SqlDataReader dr = null;
try
{
Open();
dr = Comm.ExecuteReader();
}
catch
{
dr=null;;
}
finally
{
Comm.Dispose();
Close();
}
return dr;
}
#endregion #region 执行存储过程,返回DataSet
/// <summary>
/// 功能:返回一个DataSet结果集
/// </summary>
/// <param name="procName">存储过程名.</param>
/// <param name="prams">存储过程入参数组.</param>
///<returns>dataset对象.</returns>
public DataSet ExecuteProcDataSet(string procName,SqlParameter[] prams)
{
DataSet ds=new DataSet();
SqlCommand Cmd = CreateProcCmd(procName,prams);
try
{
Open();
SqlDataAdapter da = new SqlDataAdapter(Cmd);
da.Fill(ds);
}
catch(Exception Ex)
{
throw Ex;
}
finally
{
Cmd.Dispose();
Close();
}
return ds;
}
#endregion #region 执行存储过程,返回受影响的行数
/// <summary>
/// 执行存储过程,返回受影响的行数
/// </summary>
/// <param name="procName">过程名称</param>
/// <param name="prams">参数</param>
/// <returns></returns>
public int ExecuteProc(string procName,SqlParameter[] prams)
{
int i=-1;
SqlCommand Cmd = CreateProcCmd(procName,prams);
try
{
Open();
i = Cmd.ExecuteNonQuery();
}
catch
{
i=-1;
}
return i;
}
#endregion #region 生成一个存储过程使用的SqlCommand
/// <summary>
/// 生成一个存储过程使用的SqlCommand
/// </summary>
/// <param name="procName">存储过程名.</param>
/// <param name="prams">存储过程入参数组.</param>
/// <returns>SqlCommand对象</returns>
public SqlCommand CreateProcCmd(string procName, SqlParameter[] prams)
{
Open();
SqlCommand Cmd = new SqlCommand(procName, Conn);
Cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if(parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}
#endregion }
}
{
/// <summary>
/// gb 的摘要说明。
/// </summary>
public class Gb
{
public Gb()
{
} #region Model实体类 private int _id;
private string _name;
private string _content;
private DateTime _addtime; public int id
{
set{ _id=value;}
get{return _id;}
} public string name
{
set{ _name=value;}
get{return _name;}
} public string content
{
set{ _content=value;}
get{return _content;}
} public DateTime addtime
{
set{ _addtime=value;}
get{return _addtime;}
} #endregion
}
}
---------------------------------------------------------------
谢谢,我按你说的去试试!看能不能体会出三层的好处
请问,哪些代码不规范?应该怎么写?多谢指教
其次业务逻辑层太单薄,inputCheck都没有失去了业务逻辑层的作用.但层有了功能就方便添加和抽象了
2. Page_Load绑定应该加IsPostBack判断
3. 删除之后e.Item.EditItemIndex = -1;
4.
if ( Conn.State.ToString() == "Open" )
=======================================
if (Conn.State == ConnectionState.Open)
5.
public SqlDataReader ExecuteSqlDataReader( string strSQL )
========================================================
这个方法没什么价值,连接一关,DataReader就over
我觉得规范是为了使代码的可读性更高,跟据你定义的名字能够猜出大概这个变量是干什么用的。一般公司都会有定义变量名和缩进的规范,如果你想了解更多可以参考下面的资料
http://download.csdn.net/down/148800
Button1建议改为btn_Save或其他
DataGrid1建议改为dg_GuestBookItems或其他
这样大多数人一看id就知道这个控件大概是干什么的了(个人观点仅供参考)
http://www.51aspx.com/CV/AjaxMyPage
BLL层,你要做什么判断,就放在这行啦。例如发布的内容不能为空啊,只有管理员才能删除啊等等。
Model,这是抽象出一个对象,面向对象嘛,然后在层之间传输一个实体对象。
DAL应该写数据访问类,而不是SQL语句。