三层架构C/S程序设计实例(C#描述)1.三层之间的关系:三层是指:界面显示层(UI),业务逻辑层(Business),数据操作层(Data Access)文字描述:Clients对UI进行操作,UI调用Business进行相应的运算和处理,Business通过Data Access对Data Base进行操作。
优点:l 增加了代码的重用。
Data Access可在多个项目中公用;Business可在同一项目的不同地方使用(如某个软件B/S和C/S部分可以共用一系列的Business组件)。
l 使得软件的分层更加明晰,便于开发和维护。
美工人员可以很方便地设计UI设计,并在其中调用Business给出的接口,而程序开发人员则可以专注的进行代码的编写和功能的实现。
2.Data Access的具体实现:DataAgent类型中变量和方法的说明:private string m_strConnectionString; //连接字符串private OleDbConnection m_objConnection; //数据库连接public DataAgent(string strConnection) //构造方法,传入的参数为连接字符串private void OpenDataBase() //打开数据库连接private void #region CloseDataBase() //关闭数据库连接public DataView GetDataView(string strSqlStat) //根据传入的连接字符串返回DataView具体实现代码如下:public class DataAgent{private string m_strConnectionString;private OleDbConnection m_objConnection;#region DataAgend///<summary>/// Initial Function///</summary>///<param name="strConnection"></param>public DataAgent(string strConnection){this.m_strConnectionString = strConnection;}#endregion#region OpenDataBase///<summary>/// Open Database///</summary>private void OpenDataBase(){try{this.m_objConnection = new OleDbConnection();this.m_objConnection.ConnectionString = this.m_strConnectionString;if (this.m_objConnection.State != ConnectionState.Open){this.m_objConnection.Open();}}catch (Exception e){throw e;}}#endregion#region CloseDataBase///<summary>/// Close Database///</summary>private void CloseDataBase(){if (this.m_objConnection != null){if (this.m_objConnection.State == ConnectionState.Open){this.m_objConnection.Close();}}}#endregion#region GetDataView///<summary>/// Execute the sql and return the default table view///</summary>///<param name="strSelectString">Select String</param>///<returns>DataView of the DataTable</returns>public DataView GetDataView(string strSqlStat){try{this.OpenDataBase();OleDbDataAdapter objDataAdapter= new OleDbDataAdapter(strSqlStat.Trim(), this.m_objConnection);DataSet objDataSet = new DataSet();objDataAdapter.Fill(objDataSet);return objDataSet.Tables[0].DefaultView;}catch (Exception e){throw e;}finally{this.CloseDataBase();}}#endregion}3.Business的具体实现:建立名为Base的类,此类作为其他事务类的基类,其中定义了一个DataAgent的实例。
其他所有的Business类都从该改类派生。
在该类中添加对DataAgent的引用,使所有的事务类都能使用DataAgent中的方法。
Base.cs源代码:public abstract class Base{protected DataAgent OleDBAgent = new DataAgent("Provider=SQLOLEDB;DataSource=(local);DataBase=test;User ID=sa;PWD=");}准备好了数据操作层和事务层的基类,底下就可以正式地开始业务逻辑类的开发了,如有一个显示新闻的类News,其中包含了一个GetNewsList()的方法,该方法用来获取所有的新闻标题列表,代码如下:public class News : Base{public DataView GetNewsList(){string strSql;strSql = "";strSql += " SELECT Top 10 NewsId,NewsTitle ";strSql += " FROM Tb_News";strSql += " WHERE NewsEnable = 1";strSql += " ORDER BY NewsId ";return OleDBAgent.GetDataView(strSql);}}由于数据库结构比较简单,在此就不再给出详细的表结构。
4.UI层对Business中接口的调用首先,在窗体Form1中添加对News类的引用。
然后,在窗体Form1中添加一个(DataGridView)dgNews用来显示新闻列表。
在窗体的Form1_Load方法中添加如下代码:private void Form1_Load(object sender, EventArgs e){News objNews = new News();this.dgNews.DataSource = objNews.GetNewsList();}一、数据库/*==============================================================*/ /* DBMS name: Microsoft SQL Server 2000 *//*==============================================================*/if exists (select1from sysobjectswhere id =object_id('newsContent')and type ='U')drop table newsContentgo/*==============================================================*/ /* Table: newsContent *//*==============================================================*/ create table newsContent (ID int identity(1,1) primary key,Title nvarchar(50) not null,Content ntext not null,AddDate datetime not null,CategoryID int not null)go复制代码二、项目文件架构实现步骤为:4-3-6-5-2-1实现步骤过程1、创建Model,实现业务实体。
2、创建IDAL,实现接口。
3、创建SQLServerDAL,实现接口里的方法。
4、增加web.config里的配置信息,为SQLServerDAL的程序集。
5、创建DALFactory,返回程序集的指定类的实例。
6、创建BLL,调用DALFactory,得到程序集指定类的实例,完成数据操作方法。
7、创建WEB,调用BLL里的数据操作方法。
注意:1、web.config里的程序集名称必须与SQLServerDAL里的输出程序集名称一致。
2、DALFactory里只需要一个DataAccess类,可以完成创建所有的程序集实例。
3、项目创建后,注意修改各项目的默认命名空间和程序集名称。
4、注意修改解决方案里的项目依赖。
5、注意在解决方案里增加各项目引用。
三、各层间的访问过程1、传入值,将值进行类型转换(为整型)。
2、创建BLL层的content.cs对象c,通过对象c访问BLL层的方法GetContentInfo(ID)调用BLL层。
3、BLL层方法GetContentInfo(ID)中取得数据访问层SQLServerDAL的实例,实例化IDAL层的接口对象dal,这个对象是由工厂层DALFactory创建的,然后返回IDAL层传入值所查找的内容的方法dal.GetContentInfo(id)。
4、数据工厂通过web.config配置文件中给定的webdal字串访问SQLServerDAL层,返回一个完整的调用SQLServerDAL 层的路径给BLL层。