博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
步步为营 .NET三层架构解析 五、DAL与IDAL的设计
阅读量:6463 次
发布时间:2019-06-23

本文共 14761 字,大约阅读时间需要 49 分钟。

IDAL:数据访问层接口,接口是一种系列‘功能’的声明或名单,接口没有实现细节.
IDAL的作用是把访问数据的实现与客户端分开,符合“Program to an interface, not an implementation”的设计原理,这样
1。客户端不依赖于DAL的具体实现的类
2。可以通过工厂类/配置设置改换具体实现的类(譬如从Oracle到SQLServer)
DAL:数据访问层,主要用来做数据逻辑处理,具体为业务逻辑层或表示层提供数据服务。

先来看下IDAL的设计:

ICustom.cs

public interface ICustom    {        ///         /// 添加一条记录        ///         ///         /// 
int Addcustom(custom Custom); /// /// 概据帐户名获取用户的信息 /// /// ///
custom Getsinglecname(string nename); /// /// 更样用户的密码 /// /// void Updatepassword(custom Custom); /// /// 获取用户列表 /// ///
List
Getcustom(); ///
/// 根据ID删除用户记录 /// ///
void Deletecustom(int nid); ///
/// 根据ID获取用户信息 /// ///
///
custom Getcustomer(int nid); ///
/// 更新用户信息 /// ///
void updatecustom(custom Custom); ///
/// 根据部门ID获取部门员工列表 /// ///
///
List
Getdepartcustom(int nid);
}

与之想对应的customSQL.cs设计:

public class customSQL:ICustom    {        public int Addcustom(custom Custom)        {            SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();            SqlParameter[] ParamList = {               sqlHelper.CreateInParam("@cname",SqlDbType.NVarChar,50,Custom.cname),               sqlHelper .CreateInParam("@departID",SqlDbType.Int ,4,Custom.departID),               sqlHelper .CreateInParam("@age",SqlDbType.Int,4,Custom.age),               sqlHelper.CreateInParam("@ename",SqlDbType.NVarChar,50,Custom.ename),               sqlHelper.CreateInParam("@password",SqlDbType.NVarChar,50,Custom.password)           };            try            {                return (sqlHelper.RunProc("spInsertCustom", ParamList));            }            catch (Exception ex)            {                SystemError.CreateErrorLog(ex.Message);                throw new Exception(ex.Message, ex);            }        }        public custom Getsinglecname(string nename)        {            SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();            SqlParameter[] Paramlist = {           sqlHelper.CreateInParam("ename",SqlDbType.NVarChar,50,nename)                      };            SqlDataReader dr = null;            try            {                sqlHelper.RunProc("spGetsingleename", Paramlist, out dr);            }            catch (Exception ex)            {                SystemError.CreateErrorLog(ex.Message);                throw new Exception(ex.Message, ex);            }            custom Custom = new custom();            while (dr.Read())            {                Custom.id = Int32.Parse(dr["id"].ToString());                Custom.cname = dr["cname"].ToString();                Custom.ename = dr["ename"].ToString();                Custom.departID = int.Parse(dr["departID"].ToString());                Custom.password = dr["password"].ToString();                Custom.age = int.Parse(dr["age"].ToString());            }            dr.Dispose();            return Custom;        }        public void Updatepassword(custom Custom)        {            SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();            SqlParameter[] ParamList = {               sqlHelper.CreateInParam("@id",SqlDbType.Int,4,Custom.id),               sqlHelper.CreateInParam("@cname",SqlDbType.NVarChar,50,Custom.cname),               sqlHelper .CreateInParam("@departID",SqlDbType.Int ,4,Custom.departID),               sqlHelper .CreateInParam("@age",SqlDbType.Int,4,Custom.age),               sqlHelper.CreateInParam("@ename",SqlDbType.NVarChar,50,Custom.ename),               sqlHelper.CreateInParam("@password",SqlDbType.NVarChar,50,Custom.password)           };            try            {                sqlHelper.RunProc("spUpdatepassword", ParamList);            }            catch (Exception ex)            {                SystemError.CreateErrorLog(ex.Message);                throw new Exception(ex.Message, ex);            }        }        public List
Getcustom() { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlDataReader dr = null; try { sqlHelper.RunProc("spGetcustom", out dr); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } return ConvertDrToCustomList(dr); } public void Deletecustom(int nid) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] Paramlist = { sqlHelper.CreateInParam("id",SqlDbType.Int,4,nid) }; try { sqlHelper.RunProc("spDeletecustom", Paramlist); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } public custom Getcustomer(int nid) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] Paramlist = { sqlHelper.CreateInParam("id",SqlDbType.Int,4,nid) }; SqlDataReader dr = null; try { sqlHelper.RunProc("spGetcustomer", Paramlist, out dr); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } dr.Dispose(); return ConvertDrToCustom(dr); } public void updatecustom(custom Custom) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] ParamList = { sqlHelper.CreateInParam("@id",SqlDbType.Int,4,Custom.id), sqlHelper.CreateInParam("@cname",SqlDbType.NVarChar,50,Custom.cname), sqlHelper .CreateInParam("@departID",SqlDbType.Int ,4,Custom.departID), sqlHelper .CreateInParam("@age",SqlDbType.Int,4,Custom.age), sqlHelper.CreateInParam("@ename",SqlDbType.NVarChar,50,Custom.ename) }; try { sqlHelper.RunProc("spupdatecustom", ParamList); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } public List
Getdepartcustom(int nid) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlDataReader dr = null; SqlParameter[] Paramlist = { sqlHelper.CreateInParam("departID",SqlDbType.Int,4,nid) }; try { sqlHelper.RunProc("spGetdepartcustom", Paramlist, out dr); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } return ConvertDrToCustomList(dr); } private custom ConvertDrToCustom(SqlDataReader dr) { custom Custom = new custom(); while (dr.Read()) { departmentSQL DepartmentSQL = new departmentSQL(); department Department = new department(); Department = DepartmentSQL.Getsingledepartment(Int32.Parse(dr["departID"].ToString())); Custom.id = Int32.Parse(dr["id"].ToString()); Custom.ename = dr["ename"].ToString(); Custom.cname = dr["cname"].ToString(); Custom.age = Int32.Parse(dr["age"].ToString()); Custom.departID = Int32.Parse(dr["departID"].ToString()); Custom.departname = Department.departname; Custom.password = dr["password"].ToString(); } dr.Dispose(); return Custom; } private List
ConvertDrToCustomList(SqlDataReader dr) { List
Customlist = new List
(); while (dr.Read()) { departmentSQL DepartmentSQL = new departmentSQL(); department Department = new department(); Department = DepartmentSQL.Getsingledepartment(Int32.Parse(dr["departID"].ToString())); custom Custom = new custom(); Custom.id = Int32.Parse(dr["id"].ToString()); Custom.ename = dr["ename"].ToString(); Custom.cname = dr["cname"].ToString(); Custom.age = Int32.Parse(dr["age"].ToString()); Custom.departID = Int32.Parse(dr["departID"].ToString()); Custom.departname = Department.departname; Custom.password = dr["password"].ToString(); Customlist.Add(Custom); Custom = null; } dr.Dispose(); return Customlist; } }

接下来再看IDepartment.cs的设计:

public interface IDepartment    {        ///         /// 增加一条部门数据        ///         ///         /// 
int Adddepartment(department Department); /// /// 获取部门列表 /// ///
List
Getdepartment(); ///
/// 根据部门ID获取部门信息 /// ///
///
department Getsingledepartment(int nid); ///
/// 根据部门名称获取部门信息 /// ///
///
department Getdepartmenter(string ndepartname); ///
/// 更新部门信息 /// ///
void Updatepartment(department Department); ///
/// 根据ID删除部门信息 /// ///
void Deletedepart(int nid);
}

与之相对应的departmentSQL.cs的设计:

public class departmentSQL:IDepartment    {       public int Adddepartment(department Department)       {           SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();           SqlParameter[] ParamList = {           sqlHelper.CreateInParam("@departname",SqlDbType.NVarChar,50,Department.departname),           sqlHelper .CreateInParam("@description",SqlDbType.NVarChar,50,Department.description)           };           try           {               return (sqlHelper.RunProc("spInsertDepartment", ParamList));           }           catch (Exception ex)           {               SystemError.CreateErrorLog(ex.Message);               throw new Exception(ex.Message, ex);           }       }       public List
Getdepartment() { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlDataReader dr = null; try { sqlHelper.RunProc("spGetAlldepartment", out dr); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } return ConvertDrToListDepartment(dr); } private List
ConvertDrToListDepartment(SqlDataReader dr) { List
Departmentlist = new List
(); while (dr.Read()) { department Department = new department(); Department.id = Int32.Parse(dr["id"].ToString()); Department.departname = dr["departname"].ToString(); Department.description = dr["description"].ToString(); Departmentlist.Add(Department); Department = null; } dr.Dispose(); return Departmentlist; } public department Getsingledepartment(int nid) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] ParamList = { sqlHelper.CreateInParam("@id",SqlDbType.Int,4,nid) }; SqlDataReader dr = null; try { sqlHelper.RunProc("spGetdepartment",ParamList,out dr); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } return ConvertDrToDepartment(dr); } public department Getdepartmenter(string ndepartname) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] ParamList = { sqlHelper.CreateInParam("@departname",SqlDbType.NVarChar,50,ndepartname) }; SqlDataReader dr = null; try { sqlHelper.RunProc("spGetdepartmenter", ParamList, out dr); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } return ConvertDrToDepartment(dr); } public void Updatepartment(department Department) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] ParamList = { sqlHelper.CreateInParam("@id",SqlDbType.Int,4,Department.id), sqlHelper.CreateInParam("@departname",SqlDbType.NVarChar,50,Department.departname), sqlHelper .CreateInParam("@description",SqlDbType.NVarChar,50,Department.description) }; try { sqlHelper.RunProc("spupdatedepart", ParamList); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } public void Deletedepart(int nid) { SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper(); SqlParameter[] ParamList = { sqlHelper.CreateInParam("@id",SqlDbType.Int,4,nid) }; try { sqlHelper.RunProc("spdeletedepart", ParamList); } catch (Exception ex) { SystemError.CreateErrorLog(ex.Message); throw new Exception(ex.Message, ex); } } private department ConvertDrToDepartment(SqlDataReader dr) { department Department = new department(); while (dr.Read()) { Department.id = Int32.Parse(dr["id"].ToString()); Department.departname = dr["departname"].ToString(); Department.description = dr["description"].ToString(); } dr.Dispose(); return Department; } }

DAL层我们就设计完了,接下来我们就开始设计BLL层了,欢迎拍砖.

你的持续关注,就是我不断前进的最好动力.

转载地址:http://hthzo.baihongyu.com/

你可能感兴趣的文章
ThoughtWorks雷达上的新奇变化
查看>>
IBM推出实时跨境支付解决方案Blockchain World Wire
查看>>
React服务端渲染Next.js 8发布,新增无服务器功能
查看>>
在Spinnaker中创建微服务部署管道
查看>>
Android Studio 3.3发布:官方支持导航编辑器
查看>>
腾讯组织架构整改引思考:中小团队要怎样搭建架构?
查看>>
GitHub Checks API帮助应用实现进一步的持续集成
查看>>
大前端时代,如何做好C 端业务下的React SSR?\n
查看>>
图解Go内存分配器
查看>>
语义鸿沟、异构鸿沟、数据缺失,多模态技术如何跨过这些坎?
查看>>
Ruby 2.5.0概览
查看>>
Payara基金会发布全面支持MicroProfile 2.0的5.183版Payara Server和Payara Micro
查看>>
JavaScript_Html5_LocalStorage项目demo
查看>>
英特尔中国研究院院长宋继强:摩尔定律的经济效益仍在继续
查看>>
在容器化环境中扩展分布式流式处理器\n
查看>>
Java EE重命名为Jakarta EE:Java EE Guardians与Oracle的分歧
查看>>
JavaScript面向对象核心知识归纳
查看>>
Leetcode刷题神器,妈妈再也不担心我刷题后Solution同步到Github的问题了
查看>>
avalon2.2 发布
查看>>
浏览器唤起qq进行聊天的一些坑和解决方案 - 小裂变
查看>>