基于微软企业库的分层代码框架

 

一般我们都采用分层架构,

先思考业务,要解决什么样的业务问题,再去思考技术实现。

l  正向思维:

界面(UI)—>业务逻辑(BLL—>数据逻辑(DAL—>数据实体(Model—>数据存储(Db

l  反向思维:

数据存储(Db)—>数据实体(Model)—>数据逻辑(DAL)—>业务逻辑(BLL)—>界面(UI)

从数据库推导到界面,从界面推导到数据库。搞清楚类之间的关系。数据之间传递关系。

 

界面(UI):由控件组成。系统与用户交互的窗口。

业务逻辑(BLL):封装业务处理功能。对象的组合,应用。

数据逻辑(DAL):对数据的操作(算法)。(增删改查的多样性),对象函数,方法。

数据实体(Model):操作的数据结构。对象属性

数据存储(Db):数据的存储持久化。(表)。

 

体会:程序=数据结构+算法,程序=类对象+类对象之间的关系。

 

 

 

 

我们的很多项目都是基于微软企业库5.0搭建的。具体说一下我们在企业级项目中是怎么应用的。下面是架构图:

 

 

1.基于微软企业库的工具类

 

封装了微软企业库访问数据库常用的最基本的操作。


2.Model层(数据库表映射)

 

封装了很多属性。对应数据库表c_hbbb_code

 

代码:

 using System; namespace SFS.Entity.Model { /// summary /// c_hbbb_code /// summary [Serializable] public class c_hbbb_code { public c_hbbb_code() { } private int _CodeID = 0; ///

/// /// public int CodeID { get { return _CodeID; } set { _CodeID = value; } } private string _CODENO = ""; /// /// /// public string CODENO { get { return _CODENO; } set { _CODENO = value; } } private string _CODENAME = ""; /// /// /// public string CODENAME { get { return _CODENAME; } set { _CODENAME = value; } } private string _ISMX = ""; /// /// /// public string ISMX { get { return _ISMX; } set { _ISMX = value; } } private string _ParentCodeNO = ""; /// /// /// public string ParentCodeNO { get { return _ParentCodeNO; } set { _ParentCodeNO = value; } } private string _ZT = ""; /// /// /// public string ZT { get { return _ZT; } set { _ZT = value; } } } }


3.Dal层(封装数据库操作增,删,改,查)

 namespace SFS.DAL { public class c_hbbb_code { private SFS.Utility.DbUtility.DbHelper db = null; ///

/// 是否存在该记录 /// public bool Exists(string CodeID) { string strSql = "SELECT count(1) FROM c_hbbb_code WHERE CodeID=@CodeID "; DbCommand dbCommand = db.GetSqlStringCommand(strSql); db.AddInParameter(dbCommand, "CodeID", DbType.String, CodeID); int result; object obj = db.ExecuteScalar(dbCommand); int.TryParse(obj.ToString(), out result); if (result == 1) { return true; } else { return false; } } /// /// 增加一条数据 /// public int Add(SFS.Entity.Model.c_hbbb_code model) { return Add(model, null); } /// /// 增加一条数据 /// public int Add(SFS.Entity.Model.c_hbbb_code model, DbTransaction transaction) { try { string strSql = "INSERT INTO c_hbbb_code(CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT)VALUES(@CodeID,@CODENO,@CODENAME,@ISMX,@ParentCodeNO,@ZT)"; DbCommand dbCommand = db.GetSqlStringCommand(strSql); db.AddInParameter(dbCommand, "CodeID", DbType.Int32, model.CodeID); db.AddInParameter(dbCommand, "CODENO", DbType.String, model.CODENO); db.AddInParameter(dbCommand, "CODENAME", DbType.String, model.CODENAME); db.AddInParameter(dbCommand, "ISMX", DbType.String, model.ISMX); db.AddInParameter(dbCommand, "ParentCodeNO", DbType.String, model.ParentCodeNO); db.AddInParameter(dbCommand, "ZT", DbType.String, model.ZT); if (transaction == null) { return db.ExecuteNonQuery(dbCommand); } else { return db.ExecuteNonQuery(dbCommand, transaction); } } catch (AddException addex) { addex.DeveloperMessage = "添加c_hbbb_code记录失败!" + addex.Message + "在" + this.ToString() + ".Add(SFS.Entity.Model.c_hbbb_code model)中"; addex.UserMessage = "添加c_hbbb_code记录失败!"; throw addex; } } /// /// 更新一条数据 /// public int Update(SFS.Entity.Model.c_hbbb_code model) { return Update(model, null); } /// /// 更新一条数据 /// public int Update(SFS.Entity.Model.c_hbbb_code model, DbTransaction transaction) { try { string strSql = "UPDATE c_hbbb_code SET CODENO=@CODENO,CODENAME=@CODENAME,ISMX=@ISMX,ParentCodeNO=@ParentCodeNO,ZT=@ZT WHERE CodeID=@CodeID "; DbCommand dbCommand = db.GetSqlStringCommand(strSql); db.AddInParameter(dbCommand, "CodeID", DbType.Int32, model.CodeID); db.AddInParameter(dbCommand, "CODENO", DbType.String, model.CODENO); db.AddInParameter(dbCommand, "CODENAME", DbType.String, model.CODENAME); db.AddInParameter(dbCommand, "ISMX", DbType.String, model.ISMX); db.AddInParameter(dbCommand, "ParentCodeNO", DbType.String, model.ParentCodeNO); db.AddInParameter(dbCommand, "ZT", DbType.String, model.ZT); if (transaction == null) { return db.ExecuteNonQuery(dbCommand); } else { return db.ExecuteNonQuery(dbCommand, transaction); } } catch (UpdateException updateex) { updateex.DeveloperMessage = "修改c_hbbb_code记录失败!" + updateex.Message + "在" + this.ToString() + ".Update(SFS.Entity.Model.c_hbbb_code model)中"; updateex.UserMessage = "修改c_hbbb_code记录失败!"; throw updateex; } } /// /// 删除一条数据 /// public int Delete(string CodeID) { return Delete(CodeID, null); } /// /// 删除一条数据 /// public int Delete(string CodeID, DbTransaction transaction) { try { if (string.IsNullOrEmpty(CodeID)) { NOIsNullOrEmptyException exception = new NOIsNullOrEmptyException(); exception.DeveloperMessage = "CodeID编号不能为空!在" + this.ToString() + ".Delete(string CodeID)中"; exception.UserMessage = "CodeID编号不能为空!"; throw exception; } string strSql = "DELETE FROM c_hbbb_code WHERE CodeID=@CodeID "; DbCommand dbCommand = db.GetSqlStringCommand(strSql); db.AddInParameter(dbCommand, "CodeID", DbType.String, CodeID); if (transaction == null) { return db.ExecuteNonQuery(dbCommand); } else { return db.ExecuteNonQuery(dbCommand, transaction); } } catch (DeleteException deleteex) { deleteex.DeveloperMessage = "删除c_hbbb_code记录失败!" + deleteex.Message + "在" + this.ToString() + ".Delete(string CodeID)中"; deleteex.UserMessage = "删除c_hbbb_code记录失败!"; throw deleteex; } } /// /// 得到一个对象实体 /// public SFS.Entity.Model.c_hbbb_code GetModel(string CodeID) { if (string.IsNullOrEmpty(CodeID)) { NOIsNullOrEmptyException exception = new NOIsNullOrEmptyException(); exception.DeveloperMessage = "CodeID编号不能为空!在" + this.ToString() + ".GetModel(string CodeID)中"; exception.UserMessage = "CodeID编号不能为空!"; throw exception; } StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT "); strSql.Append(" FROM c_hbbb_code "); strSql.Append(" WHERE CodeID=@CodeID "); DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); db.AddInParameter(dbCommand, "CodeID", DbType.String, CodeID); SFS.Entity.Model.c_hbbb_code model = null; using (IDataReader dataReader = db.ExecuteReader(dbCommand)) { if (dataReader.Read()) { model = ReaderBind(dataReader); } } return model; } /// /// 获得数据列表 /// public DataSet GetDataSet(string strWhere) { try { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT "); strSql.Append(" FROM c_hbbb_code "); if (strWhere.Trim() != "") { strSql.Append(" WHERE " + strWhere); } return db.ExecuteDataSet(CommandType.Text, strSql.ToString()); } catch (SelectException selectex) { selectex.DeveloperMessage = "查询记录失败!" + selectex.Message + "在" + this.ToString() + ".GetDataSet(string strWhere)中"; selectex.UserMessage = "查询记录失败!"; throw selectex; } } /// /// 获得数据列表(比DataSet效率高,推荐使用) /// public List GetList(string strWhere) { try { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT CodeID,CODENO,CODENAME,ISMX,ParentCodeNO,ZT "); strSql.Append(" FROM c_hbbb_code "); if (strWhere.Trim() != "") { strSql.Append(" WHERE " + strWhere); } List list = new List(); using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (dataReader.Read()) { list.Add(ReaderBind(dataReader)); } } return list; } catch (SelectException selectex) { selectex.DeveloperMessage = "查询记录失败!" + selectex.Message + "在" + this.ToString() + ".GetList(string strWhere)中"; selectex.UserMessage = "查询记录失败!"; throw selectex; } } /// /// 对象实体绑定数据 /// public SFS.Entity.Model.c_hbbb_code ReaderBind(IDataReader dataReader) { SFS.Entity.Model.c_hbbb_code model = new SFS.Entity.Model.c_hbbb_code(); object obj; obj = dataReader["CodeID"]; if (obj != null && obj != DBNull.Value) { model.CodeID = (int)obj; } obj = dataReader["CODENO"]; if (obj != null && obj != DBNull.Value) { model.CODENO = obj.ToString(); } obj = dataReader["CODENAME"]; if (obj != null && obj != DBNull.Value) { model.CODENAME = obj.ToString(); } obj = dataReader["ISMX"]; if (obj != null && obj != DBNull.Value) { model.ISMX = obj.ToString(); } obj = dataReader["ParentCodeNO"]; if (obj != null && obj != DBNull.Value) { model.ParentCodeNO = obj.ToString(); } obj = dataReader["ZT"]; if (obj != null && obj != DBNull.Value) { model.ZT = obj.ToString(); } return model; } } }


4.Bll层(跟具体业务有关的逻辑)

 namespace SFS.BLL { ///

/// 业务逻辑类bllADDRESS 的摘要说明。 /// public class c_hbbb_code { private readonly SFS.DAL.c_hbbb_code dal = new SFS.DAL.c_hbbb_code(); public c_hbbb_code() { } #region 成员方法 /// /// 是否存在该记录 /// public bool Exists(string CodeID) { try { return dal.Exists(CodeID); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return false; } } /// /// 增加一条数据 /// public int Add(SFS.Entity.Model.c_hbbb_code model) { try { return dal.Add(model); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return 0; } } /// /// 更新一条数据 /// public int Update(SFS.Entity.Model.c_hbbb_code model) { try { return dal.Update(model); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return 0; } } /// /// 删除一条数据 /// public int Delete(string CodeID) { try { return dal.Delete(CodeID); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return 0; } } /// /// 得到一个对象实体 /// public SFS.Entity.Model.c_hbbb_code GetModel(string CodeID) { try { return dal.GetModel(CodeID); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return null; } } /// /// 获得数据列表 /// public DataSet GetDataSet(string strWhere) { try { return dal.GetDataSet(strWhere); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return null; } } /// /// /// 获得数据列表 /// public List GetList(string strWhere) { try { return dal.GetList(strWhere); } catch (ExceptionBase ex) { SFS.Utility.ExceptionUtility.ExceptionHelper.HandleException(ex, Policy.BLLPolicy); return null; } } #endregion 成员方法 } }
5.UI层(用户界面)

 

 

常用界面功能代码

界面模式:单选(新建,删除,修改,查看)

功能名称

查询 新建 删除 修改 查看

数据列表

 

 

加载数据

 

分页

 

自动加入序号

 

添加

 

删除

 

修改

 

查看

 

导出

 

 

 

加载数据

private void LoadData(string SJJG)

{

SFS.Bll.c_hbbb_code objC_SUBCOMPANY=new SFS.Bll.c_hbbb_code();

this.GridView1.DataSource = objC_SUBCOMPANY.GetList(“”);

this.GridView1.DataBind();

}

分页

protected voidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgs e)

{

this.GridView1.PageIndex = e.NewPageIndex;

LoadData(“”);

}

绑定时自动加入序号

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

      if (e.Row.RowType == DataControlRowType.DataRow)

      {

RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");

rb.Attributes.Add("onclick", "CheckOne(this);");

e.Row.Cells[1].Text = Convert.ToString(e.Row.RowIndex + 1);

       }

}

添加

protected void btnAdd_Click(object sender, EventArgs e)

{

    Response.Redirect("Add.aspx?JGDM=" + SJJG);

}

删除

protected void btnUpdate_Click(object sender, EventArgs e)

{

    foreach (GridViewRow row in GridView1.Rows)

    {

RadioButton radio = row.FindControl("rbSelect") as RadioButton;

if (radio != null && radio.Checked)

{

       Label lblID = (Label)row.FindControl("lblJGDM");

       Response.Redirect("Delete.aspx?JGDM=" + SJJG);

       break;

        }

}

}

 

修改

protected void btnUpdate_Click(object sender, EventArgs e)

{

    foreach (GridViewRow row in GridView1.Rows)

    {

RadioButton radio = row.FindControl("rbSelect") as RadioButton;

if (radio != null && radio.Checked)

{

       Label lblID = (Label)row.FindControl("lblJGDM");

       Response.Redirect("Update.aspx?JGDM=" + SJJG);

       break;

        }

}

}

一般写界面代码逻辑顺序

步骤

 

 

 

1

Page_Load

中判断Session是否超时。

 

获取页面参数。

 

加载初始化控件(DropDownListTextBox等控件)。

调用BBL层。

执行初始化查询绑定GridView控件。

调用BBL层。

2

Button_Click

判断界面参数设置数据有效性

脚本判断,或页面上后台代码判断。

处理数据一般包括。

增加:跳转到增加页面。

删除:删除前要提示。

修改:先判断是否选中,跳转到修改页面,需要传递参数。

查询:先检查查询条件的有效性,再执行查询。

 

调用BLL

判断是否刷新页面。

 

3

GridView

一般性代码(分页,加单选,多选处理,绑定超连接,特殊列处理)

 

获得选种行,传递相关参数。

 

4

其他特殊处理

 

 

 

 

你可能感兴趣的:(技术,微软,string,radiobutton,null,exception,dataset)