简单的三层结构(C#实现)

      这篇文章讨论如何在c#中实现3层架构,使用MS Access数据库 存储 数据。在此,我在3层架构中实现一个小型的可复用的组件保存客户数据。并提供添加,更新,查找客户数据的功能。

背景

首先,我介绍一些3层架构的理论知识。简单说明:什么是3层架构?3层架构的优点是什么?

什么是3层架构?

3层架构是一种“客户端-服务器”架构,在此架构中用户接口,商业逻辑,数据保存以及数据访问被设计为独立的模块。主要有3个层面:

1.第一层(表现层,GUI层);

2.第二层(业务对象,业务逻辑层);

3.第三层(数据访问层)。这些层可以单独开发,单独测试

为什么要把程序代码分为3层。把用户接口层,商业逻辑层,数据访问层分离有许多的优点。

在快速开发中重用商业逻辑组件,我们已经在系统中实现添加,更新,删除,查找客户数据的组件。这个组件已经开发并且测试通过,我们可以在其他要保存客户数据的项目中使用这个组件。

系统比较容易迁移,商业逻辑层与数据访问层是分离的,修改数据访问层不会影响到商业逻辑层。系统如果从用SQL server存储数据迁移到用oracle存储数据,并不需要修改商业逻辑层组件和GUI组件

系统容易修改,假如在商业层有一个小小的修改,我们不需要在用户的机器上重装整个系统。我们只需要更新商业逻辑组件就可以了。

应用程序开发人员可以并行,独立的开发单独的层。

代码

这个组件有3层,第一个层或者称为GUI层用form实现,叫做FrmGUI。第二层或者称为业务逻辑层,叫做BOCustomer,是Bussniess Object Customer的缩写。最后是第三层或者称为数据层,叫做DACustomer,是Data Access Customer的缩写。为了方便,我把三个层编译到一个项目中。

用户接口层

下面是用户接口成的一段代码,我只选取了调用商业逻辑层的一部分代码。

  1. //This function get the details from the user via GUI 
  2. //tier and calls the Add method of business logic layer. 
  3. private void cmdAdd_Click(object sender, system.EventArgs e) 
  4.     try 
  5.     { 
  6.         cus = new BOCustomer(); 
  7.         cus.cusID=txtID.Text.ToString(); 
  8.         cus.LName = txtLName.Text.ToString(); 
  9.         cus.FName = txtFName.Text.ToString(); 
  10.         cus.Tel= txtTel.Text.ToString(); 
  11.         cus.Address = txtAddress.Text.ToString(); 
  12.         cus.Add(); 
  13.     } 
  14.     catch(Exception err) 
  15.     { 
  16.         MessageBox.Show(err.Message.ToString()); 
  17.     } 
  18. //This function gets the ID from the user and finds the 
  19. //customer details and return the details in the form of 
  20. //a dataset via busniss object layer. Then it loops through
  21. //the content of the dataset and fills the controls. 
  22. private void cmdFind_Click(object sender, system.EventArgs e) 
  23.     try 
  24.     { 
  25.         String cusID = txtID.Text.ToString(); 
  26.         BOCustomer thisCus = new BOCustomer(); 
  27.         DataSet ds = thisCus.Find(cusID); 
  28.         DataRow row; 
  29.         row = ds.Tables[0].Rows[0]; 
  30.         //via looping 
  31.         foreach(DataRow rows in ds.Tables[0].Rows ) 
  32.         { 
  33.             txtFName.Text = rows["CUS_F_NAME"].ToString(); 
  34.             txtLName.Text = rows["CUS_L_NAME"].ToString(); 
  35.             txtAddress.Text = rows["CUS_ADDRESS"].ToString(); 
  36.             txtTel.Text = rows["CUS_TEL"].ToString(); 
  37.         } 
  38.     } 
  39.     catch (Exception err) 
  40.     { 
  41.         MessageBox.Show(err.Message.ToString()); 
  42.     } 
  43. //this function used to update the customer details.
  44. private void cmdUpdate_Click(object sender, system.EventArgs e) 
  45.     try 
  46.     { 
  47.         cus = new BOCustomer(); 
  48.         cus.cusID=txtID.Text.ToString(); 
  49.         cus.LName = txtLName.Text.ToString(); 
  50.         cus.FName = txtFName.Text.ToString(); 
  51.         cus.Tel= txtTel.Text.ToString(); 
  52.         cus.Address = txtAddress.Text.ToString(); 
  53.         cus.Update(); 
  54.     } 
  55.     catch(Exception err) 
  56.     { 
  57.         MessageBox.Show(err.Message.ToString()); 
  58.     } 
  59. }  

业务逻辑层

下面是业务逻辑层的所有代码,主要包括定义customer对象的属性。但这仅仅是个虚构的customer对象,如果需要可以加入其他的属性。商业逻辑层还包括添加,更新,查找,等方法。

业务逻辑层是一个中间层,处于GUI层和数据访问层中间。他有一个指向数据访问层的引用cusData = new DACustomer().而且还引用了System.Data名字空间。业务逻辑层使用DataSet返回数据给GUI层。

  1. using system; 
  2. using system.Data; 
  3. namespace _3tierarchitecture 
  4.     /// 
  5.     /// Summary description for BOCustomer. 
  6.     /// 
  7.     public class BOCustomer 
  8.     {     
  9.         //Customer properties 
  10.         private String fName; 
  11.         private String lName; 
  12.         private String cusId; 
  13.         private String address; 
  14.         private String tel; 
  15.         private DACustomer cusData; 
  16.         public BOCustomer() 
  17.         { 
  18.             //An instance of the Data access layer! 
  19.             cusData = new DACustomer(); 
  20.         } 
  21.         /// 
  22.         /// Property FirstName (String) 
  23.         /// 
  24.         public String FName 
  25.         { 
  26.             get 
  27.             { 
  28.                 return this.fName; 
  29.             } 
  30.             set 
  31.             { 
  32.                 try 
  33.                 { 
  34.                     this.fName = value; 
  35.                     if (this.fName == ""
  36.                     { 
  37.                         throw new Exception( "Please provide first name ..."); 
  38.                     } 
  39.                 } 
  40.                 catch(Exception e) 
  41.                 { 
  42.                     throw new Exception(e.Message.ToString()); 
  43.                 } 
  44.             } 
  45.         } 
  46.         /// 
  47.         /// Property LastName (String) 
  48.         /// 
  49.         public String LName 
  50.         { 
  51.             get 
  52.             { 
  53.                 return this.lName; 
  54.             } 
  55.             set 
  56.             { 
  57.                 //could be more checkings here eg revmove ' chars 
  58.                 //change to proper case 
  59.                 //blah blah 
  60.                 this.lName = value; 
  61.                 if (this.LName == ""
  62.                 { 
  63.                     throw new Exception("Please provide name ..."); 
  64.                 } 
  65.             } 
  66.         } 
  67.         /// 
  68.         /// Property Customer ID (String) 
  69.         /// 
  70.         public String cusID 
  71.         { 
  72.             get 
  73.             { 
  74.                 return this.cusId; 
  75.             } 
  76.             set 
  77.             { 
  78.                 this.cusId = value; 
  79.                 if (this.cusID == ""
  80.                 { 
  81.                     throw new Exception("Please provide ID ..."); 
  82.                 } 
  83.             } 
  84.         } 
  85.         /// 
  86.         /// Property Address (String) 
  87.         /// 
  88.         public String Address 
  89.         { 
  90.             get 
  91.             { 
  92.                 return this.address; 
  93.             } 
  94.             set 
  95.             { 
  96.                 this.address = value; 
  97.                 if (this.Address == ""
  98.                 { 
  99.                     throw new Exception("Please provide address ..."); 
  100.                 } 
  101.             } 
  102.         } 
  103.         /// 
  104.         /// Property Telephone (String) 
  105.         /// 
  106.         public String Tel 
  107.         { 
  108.             get 
  109.             { 
  110.                 return this.tel; 
  111.             } 
  112.             set 
  113.             { 
  114.                 this.tel = value; 
  115.                 if (this.Tel == ""
  116.                 { 
  117.                     throw new Exception("Please provide Tel ..."); 
  118.                 } 
  119.             } 
  120.         } 
  121.         /// 
  122.         /// Function Add new customer. Calls 
  123.         /// the function in Data layer. 
  124.         /// 
  125.         public void Add() 
  126.         { 
  127.             cusData.Add(this); 
  128.         } 
  129.         /// 
  130.         /// Function Update customer details. 
  131.         /// Calls the function in Data layer. 
  132.         /// 
  133.         public void Update() 
  134.         { 
  135.             cusData.Update(this); 
  136.         } 
  137.         /// 
  138.         /// Function Find customer. Calls the 
  139.         /// function in Data layer. 
  140.         /// It returns the details of the customer using 
  141.         /// customer ID via a Dataset to GUI tier. 
  142.         /// 
  143.         public DataSet Find(String str) 
  144.         { 
  145.             if (str == ""
  146.                 throw new Exception("Please provide ID to search"); 
  147.             DataSet data = null
  148.             data = cusData.Find(str); 
  149.             return data; 
  150.         } 
  151.     } 

数据访问层

数据层包括处理MS Access数据库的细节。所有这些细节都是透明的,不会影响到商业逻辑层。数据访问层有个指向商业逻辑层的引用BOCustomer cus。为了应用方便并且支持其他数据库。

  1. using system; 
  2. using system.Data.OleDb; 
  3. using system.Data; 
  4. namespace _3tierarchitecture 
  5.     /// 
  6.     /// Summary description for DACustomer. 
  7.     /// 
  8.     public class DACustomer 
  9.     { 
  10.         private OleDbConnection cnn; 
  11.         //change connection string as per the 
  12.         //folder you unzip the files 
  13.         private const string CnnStr = 
  14.                     "Provider=Microsoft.Jet.OLEDB.4.0;Data " + 
  15.                     "Source= D://Rahman_Backup//Programming//" + 
  16.                     "Csharp//3tierarchitecture//customer.mdb;"
  17.         //local variables 
  18.         private String strTable=""
  19.         private String strFields=""
  20.         private String strValues=""
  21.         private String insertStr=""
  22.         //this needs to be changed based on customer 
  23.        //table fields' Name of the database! 
  24.         private const String thisTable = "tblCustomer"
  25.         private const String cus_ID = "CUS_ID"
  26.         private const String cus_LName = "CUS_L_NAME"
  27.         private const String cus_FName = "CUS_F_NAME"
  28.         private const String cus_Tel = "CUS_TEL"
  29.         private const String cus_Address = "CUS_ADDRESS"
  30.         public DACustomer() 
  31.         { 
  32.         } 
  33.         public DACustomer(BOCustomer cus) 
  34.         { 
  35.             // A reference of the business object class 
  36.         } 
  37.         //standard dataset function that adds a new customer 
  38.         public void Add(BOCustomer cus) 
  39.         { 
  40.             String str = BuildAddString(cus); 
  41.             OpenCnn(); 
  42.             //Open command option - cnn parameter is imporant 
  43.             OleDbCommand cmd = new OleDbCommand(str,cnn); 
  44.             //execute connection 
  45.             cmd.ExecuteNonQuery(); 
  46.             // close connection 
  47.             CloseCnn(); 
  48.         } 
  49.         //standard dataset function that updates 
  50.         //details of a customer based on ID 
  51.         public void Update(BOCustomer cus) 
  52.         { 
  53.             OpenCnn(); 
  54.             String selectStr = "UPDATE " + thisTable + 
  55.                     " set " + cus_LName + " = '" + cus.LName + "'" + 
  56.                     ", " + cus_FName + " = '" + cus.FName + "'" + 
  57.                     ", " + cus_Address + " = '" + cus.Address + "'" + 
  58.                     ", " + cus_Tel + " = '" + cus.Tel + "'" + 
  59.                     " where cus_ID = '" + cus.cusID + "'";
  60.             OleDbCommand cmd = new OleDbCommand(selectStr,cnn); 
  61.             cmd.ExecuteNonQuery(); 
  62.             CloseCnn(); 
  63.         } 
  64.         //standard dataset function that finds and 
  65.         //return the detail of a customer in a dataset 
  66.         public DataSet Find(String argStr) 
  67.         { 
  68.             DataSet ds=null
  69.             try 
  70.             { 
  71.                 OpenCnn(); 
  72.                 String selectStr = "select * from " + thisTable +
  73.                         " where cus_ID = '" + argStr + "'"
  74.                         OleDbDataAdapter da = 
  75.                 new OleDbDataAdapter(selectStr,cnn); 
  76.                 ds = new DataSet(); 
  77.                 da.Fill(ds,thisTable); 
  78.                 CloseCnn(); 
  79.             } 
  80.             catch(Exception e) 
  81.             { 
  82.                 String Str = e.Message; 
  83.             } 
  84.             return ds; 
  85.         }
  86.         private void OpenCnn() 
  87.         { 
  88.             // initialise connection 
  89.             String cnnStr = CnnStr; 
  90.             cnn = new OleDbConnection(cnnStr); 
  91.             // open connection 
  92.             cnn.Open(); 
  93.         } 
  94.         private void CloseCnn() 
  95.         { 
  96.             // 5- step five 
  97.             cnn.Close(); 
  98.         } 
  99.         // just a supporting function that builds 
  100.         // and return the insert string for dataset. 
  101.         private String BuildAddString(BOCustomer cus) 
  102.         { 
  103.             // these are the constants as 
  104.             // set in the top of this module. 
  105.             strTable="Insert into " + thisTable; 
  106.             strFields=" (" + cus_ID + 
  107.                                 "," + cus_LName + 
  108.                                 "," + cus_FName +
  109.                                 "," + cus_Address + 
  110.                                 "," + cus_Tel + ")"
  111.             //these are the attributes of the 
  112.             //customer business object. 
  113.             strValues= " Values ( '" + cus.cusID + 
  114.                                 "' , '" + cus.LName + 
  115.                                 "' , '" + cus.FName + 
  116.                                 "' , '" + cus.Address + 
  117.                                 "' , '" + cus.Tel + "' )"
  118.             insertStr = strTable + strFields + strValues; 
  119.             return insertStr; 
  120.         } 
  121.     } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值