about C#三级架构,实例说明

来源:互联网 发布:如何使用管家婆软件 编辑:程序博客网 时间:2024/04/30 00:27

先看看三级架构的基本知识点:

  1. 用户界面表示层(USL)
  2. 业务逻辑层(BLL)
  3. 数据访问层(DAL)

  • 各层的作用
  • 1:数据数据访问层:主要是对原始数据(数据库或者文本文件等存放数据的形式)的操作层,而不是指原始数据,也就是说,是对数据的操作,而不是数据库,具体为业务逻辑层或表示层提供数据服务.

    2:业务逻辑层:主要是针对具体的问题的操作,也可以理解成对数据层的操作,对数据业务逻辑处理,如果说数据层是积木,那逻辑层就是对这些积木的搭建。

    3:表示层:主要表示WEB方式,也可以表示成WINFORM方式,WEB方式也可以表现成:aspx, 如果逻辑层相当强大和完善,无论表现层如何定义和更改,逻辑层都能完善地提供服务。
  • 具体的区分方法

    1:数据数据访问层:主要看你的数据层里面有没有包含逻辑处理,实际上他的各个函数主要完成各个对数据文件的操作。而不必管其他操作。

    2:业务逻辑层:主要负责对数据层的操作。也就是说把一些数据层的操作进行组合。

    3:表示层:主要对用户的请求接受,以及数据的返回,为客户端提供应用程序的访问。
具体请详见点击打开链接,这里就不做多余工作了。下面主要说说我个人对C#三级架构的应用。

在.net环境中,三级架构主要在这里。

即Brl、Dal、Model三个文件夹中。

先说说Model。

以Model中的Student.cs文件为例。

using System;namespace XIS.Model{/// <summary>/// Student的摘要说明/// </summary>public class Student{public Int64 Id;public Int64 Code;public String Name;public String Sex;        public Int64 CardNum;public String CardType;        public Int64 BankAcount;        public Int64 CollegeId;        public Int64 SpecialtyId;public String GradeId;public String nation;public String EducationaSystem;        public String StudentKind;public String TrainingLevel;public String nationality;public String TrainingWay;public String StudentSouce;public String Remark;public Student(){Id = 0;Code = 0;Name = "";Sex = "";CardNum = 0;CardType = "";BankAcount = 0;CollegeId = 0;SpecialtyId = 0;GradeId = "";nation = "";EducationaSystem = "";StudentKind = "";TrainingLevel = "";nationality = "";TrainingWay = "";StudentSouce = "";Remark = "";}}}

我们再看看数据库中的内容:

我们可以清晰的看到,Model中是对数据库中列名的声明及初始化。

接下来说说Dal文件夹中的StudentDa.cs中的代码

using System.Data;using System.Collections.Generic;using XIS.Model;namespace XIS.Dal{    /// <summary>    /// StudentDa 的摘要说明    /// </summary>    public class StudentDa    {        private const string tableName = "Student";        private readonly string insertSqlString;        private readonly string updateSqlString;        public StudentDa()        {            List<string> fields = Mapper.GetFieldNames(typeof(Student));            insertSqlString = DaHelper.MakeInsertSqlString(fields, tableName, 1);            updateSqlString = DaHelper.MakeUpdateSqlString(fields, tableName, 1);        }        private Dictionary<string, object> getParameters(Student student)        {            Dictionary<string, object> dictionary = new Dictionary<string, object>();            Mapper.Map(student, dictionary);            return dictionary;        }        private Student fromReader(IDataReader reader)        {            Student student = new Student();            Mapper.Map(reader, student);            return student;        }        public bool Add(Student student)        {            Dictionary<string, object> parms = getParameters(student);            int iRet = DaHelper.ExecuteSql(insertSqlString, parms);            bool bAdded = false;            if (iRet >= 0)            {                bAdded = true;            }            return bAdded;        }        public bool Update(Student student)        {            Dictionary<string, object> parms = getParameters(student);            int iRet = DaHelper.ExecuteSql(updateSqlString, parms);            bool bUpdated = false;            if (iRet >= 0)            {                bUpdated = true;            }            return bUpdated;        }        public bool Delete(int id)        {            string sqlString = "DELETE FROM " + tableName +                " WHERE ( Id = " + id.ToString() + ")";            int iRet = DaHelper.ExecuteSql(sqlString);            bool bDeleted = false;            if (iRet >= 0)            {                bDeleted = true;            }            return bDeleted;        }        public Student Get(int id)        {            string sqlString = "SELECT * FROM " + tableName +                " WHERE ( Id = " + id.ToString() + ")";            IDataReader reader = DaHelper.StartReader(sqlString);            Student student = null;            if (reader.Read())            {                student = fromReader(reader);            }            DaHelper.EndReader(reader);            return student;        }        /*        public List<Student> Get()        {            List<Student> list = new List<Student>();            string sqlString = "SELECT * FROM " + tableName +                " WHERE Flag = 0";            IDataReader reader = DaHelper.StartReader(sqlString);            Student student = null;            while (reader.Read())            {                student = fromReader(reader);                list.Add(student);            }            DaHelper.EndReader(reader);            return list;        }        public List<Student> GetAll()        {            List<Student> list = new List<Student>();            string sqlString = "SELECT * FROM " + tableName;            IDataReader reader = DaHelper.StartReader(sqlString);            Student student = null;            while (reader.Read())            {                student = fromReader(reader);                list.Add(student);            }            DaHelper.EndReader(reader);            return list;        }        public List<Student> Query()        {            List<Student> list = new List<Student>();            string sqlString = "SELECT * FROM " + tableName +                " WHERE Flag = 0";            IDataReader reader = DaHelper.StartReader(sqlString);            Student student = null;            while (reader.Read())            {                student = fromReader(reader);                list.Add(student);            }            DaHelper.EndReader(reader);            return list;        }        */    }}

这里主要就是数据库访问层了。是对原始数据(数据库或者文本文件等存放数据的形式)的操作层,而不是指原始数据,也就是说,是对数据的操作,而不是数据库,具体为业务逻辑层或表示层提供数据服务。

最后是Brl中的StudentBr.cs了。

using System;using System.Data;using System.Collections .Generic;using XIS.Dal;using XIS.Model;namespace XIS.Brl{    /// <summary>    /// StudentBr 的摘要说明    /// </summary>    public class StudentBr      {        private StudentDa studentDa;         public StudentBr()          {            studentDa = new StudentDa();          }        public bool Add(Student student)          {            return studentDa.Add(student);          }        public bool Update(Student student)          {            return studentDa.Update(student);          }        public bool Delete(int id)        {            return studentDa.Delete(id);         }        public Student Get(int id)         {            return studentDa.Get(id);         }        /*        public List<Student> Get()        {            return studentDa.Get();        }                public List<Student> GetAll()        {            return studentDa.GetAll();        }        public List<Student> Query()        {            return studentDa.Query();        }        //*/    }}

可以看到,这里主要是对Dal的封装。