在c#中实现3层架构_第1页
在c#中实现3层架构_第2页
在c#中实现3层架构_第3页
在c#中实现3层架构_第4页
在c#中实现3层架构_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、在c#中实现3层架构在c#中实现3层架构作者:佚名 文章来源:本站原创 点击数: 37 更新时间:2006-5-24   原版英文文章地址: 介绍这篇文章讨论如何在c中实现3层架构,使用MS Access数据库存储数据。在此,我在3层架构中实现一个小型的可复用的组件保存客户数据。并提供添加,更新,查找客户数据的功能。背景首先,我介绍一些3层架构的理论知识。简单说明:什么是3层架构?3层架构的优点是什么?什么是3层架构?3层架构是一种“客户端服务器”架构,在此架构中用户接口,商业逻辑,数据保存以及数据访问被设计为独立的模块。主要有3个层面,第一层(表现层,GUI层),第二层(商业对象,

2、商业逻辑层),第三层(数据访问层)。这些层可以单独开发,单独测试。为什么要把程序代码分为3层,把用户接口层,商业逻辑层,数据访问层分离有许多的优点。 在快速开发中重用商业逻辑组件,我们已经在系统中实现添加,更新,删除,查找客户数据的组件。这个组件已经开发并且测试通过,我们可以在其他要保存客户数据的项目中使用这个组件。 系统比较容易迁移,商业逻辑层与数据访问层是分离的,修改数据访问层不会影响到商业逻辑层。系统如果从用SQL Server存储数据迁移到用Oracle存储数据,并不需要修改商业逻辑层组件和GUI组件 系统容易修改,假如在商业层有一个小小的修改,我们不需要

3、在用户的机器上重装整个系统。我们只需要更新商业逻辑组件就可以了。 应用程序开发人员可以并行,独立的开发单独的层。 代码 这个组件有3层,第一个层或者称为GUI层用form实现,叫做FrmGUI。第二层或者称为商业逻辑层,叫做BOCustomer,是Bussniess Object Customer的缩写。最后是第三层或者称为数据层,叫做DACustomer,是Data Access Customer的缩写。为了方便我把三个层编译到一个项目中。 用户接口层   下面是用户接口成的一段代码,我只选取了调用商业逻辑层的一部分代码。/This

4、 function get the details from the user via GUI /tier and calls the Add method of business logic layer.private void cmdAdd_Click(object sender, System.EventArgs e)      try                  cus =

5、 new BOCustomer();            cus.cusID=txtID.Text.ToString();            cus.LName = txtLName.Text.ToString();            cus.FName =

6、txtFName.Text.ToString();            cus.Tel= txtTel.Text.ToString();            cus.Address = txtAddress.Text.ToString();            c

7、us.Add();            catch(Exception err)                  MessageBox.Show(err.Message.ToString();       /This function gets the ID from the

8、 user and finds the /customer details and return the details in the form of/a dataset via busniss object layer. Then it loops through /the content of the dataset and fills the controls. private void cmdFind_Click(object sender, System.EventArgs e)      try  &#

9、160;               String cusID = txtID.Text.ToString();                              BOCustome

10、r thisCus = new BOCustomer();                              DataSet ds = thisCus.Find(cusID);           &#

11、160; DataRow row;            row = ds.Tables0.Rows0;             /via looping            foreach(DataRow rows in ds.Tables0.Rows )

12、                           txtFName.Text = rows"CUS_F_NAME".ToString();               txtLName.

13、Text = rows"CUS_L_NAME".ToString();               txtAddress.Text = rows"CUS_ADDRESS".ToString();               txtTel.Text = rows"CUS_

14、TEL".ToString();                       catch (Exception err)                  MessageBox.Show(err.Message.ToString(

15、);        /this function used to update the customer details. private void cmdUpdate_Click(object sender,                           

16、60;      System.EventArgs e)      try                  cus = new BOCustomer();            cus.cusID=txtID.Text.ToSt

17、ring();            cus.LName = txtLName.Text.ToString();            cus.FName = txtFName.Text.ToString();            cus.Tel= txtTel.Te

18、xt.ToString();            cus.Address = txtAddress.Text.ToString();             cus.Update();            catch(Exception err) 

19、0;                MessageBox.Show(err.Message.ToString();      商业逻辑层下面是商业逻辑层的所有代码,主要包括定义customer对象的属性。但这仅仅是个虚构的customer对象,如果需要可以加入其他的属性。商业逻辑层还包括添加,更新,查找,等方法。商业逻辑层是一个中间层,处于GUI层和数据访问层中间。他有一个指向数据访问层的引用cusData

20、 = new DACustomer().而且还引用了System.Data名字空间。商业逻辑层使用DataSet返回数据给GUI层。 using System;using System.Data; namespace _3tierarchitecture      / <SUMMARY>      / Summary description for BOCustomer.      / </SUMMARY&g

21、t;            public class BOCustomer                  /Customer properties            private String fName; &

22、#160;          private String lName;            private String cusId;            private String address;       

23、;     private String tel;             private DACustomer cusData;        public BOCustomer()                &#

24、160;  /An instance of the Data access layer!             cusData = new DACustomer();                           / &l

25、t;SUMMARY>            / Property FirstName (String)            / </SUMMARY>            public String FName    &

26、#160;                                get                    

27、                      return this.fName;                          

28、0;         set                                          try&

29、#160;                                                  

30、   this.fName = value;                               if (this.fName = "")         

31、60;                                                  &#

32、160;     throw new Exception(                                      "Please provide first n

33、ame .");                                                

34、0;                             catch(Exception e)                   &#

35、160;                                  throw new Exception(e.Message.ToString();          &

36、#160;                                                   

37、0;    / <SUMMARY>            / Property LastName (String)            / </SUMMARY>            public String L

38、Name                              get                     &#

39、160;                    return this.lName;                            

40、        set                                          /could be mo

41、re checkings here eg revmove ' chars                        /change to proper case                

42、60;       /blah blah                        this.lName = value;               &#

43、160;        if (this.LName = "")                                      

44、                throw new Exception("Please provide name .");                          

45、60;                                                    

46、 / <SUMMARY>            / Property Customer ID (String)            / </SUMMARY>            public String cusID  &

47、#160;                           get                        &

48、#160;                 return this.cusId;                               

49、;     set                                          this.cusId = value; 

50、0;                      if (this.cusID = "")                        &#

51、160;                             throw new Exception("Please provide ID .");            &#

52、160;                                                    

53、;   / <SUMMARY>            / Property Address (String)            / </SUMMARY>            public String Address&#

54、160;                             get                      &#

55、160;                   return this.address;                            

56、0;       set                                          this.address = v

57、alue;                         if (this.Address = "")                   

58、0;                                  throw new Exception("Please provide address .");       

59、0;                                                    &

60、#160;      / <SUMMARY>            / Property Telephone (String)            / </SUMMARY>           

61、 public String Tel                              get                   

62、                       return this.tel;                          

63、          set                                         

64、this.tel = value;                        if (this.Tel = "")                   &#

65、160;                                  throw new Exception("Please provide Tel .");        

66、                                                    

67、60;       / <SUMMARY>            / Function Add new customer. Calls             / the function in Data layer.       

68、60;    / </SUMMARY>            public void Add()                              cusDat

69、a.Add(this);                          / <SUMMARY>            / Function Update customer details.    

70、0;        / Calls the function in Data layer.            / </SUMMARY>            public void Update()         

71、;                      cusData.Update(this);                         / <SUMMARY>

72、;            / Function Find customer. Calls the             / function in Data layer.            / It returns the details of the custo

73、mer using             / customer ID via a Dataset to GUI tier.            / </SUMMARY>            public DataSet Find(String str)

74、                               if (str = "")                 

75、0;    throw new Exception("Please provide ID to search");                                     &

76、#160;  DataSet data = null;                   data = cusData.Find(str);                   return data;  &

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

78、60;   / <SUMMARY>    / Summary description for DACustomer.    / </SUMMARY>    public class DACustomer            private OleDbConnection cnn;       

79、 /change connection string as per the         /folder you unzip the files        private const string CnnStr =           "Provider=Microsoft.Jet.OLEDB.4.0;Data " +  

80、        "Source= D:Rahman_BackupProgramming" +              "Csharp3tierarchitecturecustomer.mdb;"         /local variables   

81、0;    private String strTable=""        private String strFields=""        private String strValues=""        private String insertStr=""

82、60;               /this needs to be changed based on customer         /table fields' Name of the database!        private const String thisTable = "tblCusto

83、mer"        private const String cus_ID = "CUS_ID"        private const String cus_LName = "CUS_L_NAME"        private const String cus_FName = "CUS_F_NAME"

84、0;       private const String cus_Tel = "CUS_TEL"        private const String cus_Address = "CUS_ADDRESS"          public DACustomer()      

85、                          public DACustomer(BOCustomer cus)                    / A reference of the

86、business object class                        /standard dataset function that adds a new customer         public void Add(BOCustomer cus)   &

87、#160;                 String str = BuildAddString(cus);                        OpenCnn();    &#

88、160;        /Open command option - cnn parameter is imporant            OleDbCommand cmd = new OleDbCommand(str,cnn);              /execute conne

89、ction            cmd.ExecuteNonQuery();                        / close connection         

90、   CloseCnn();                                    /standard dataset function that updates       

91、0; /details of a customer based on ID        public void Update(BOCustomer cus)                    OpenCnn();           

92、             String selectStr = "UPDATE " + thisTable +                 " set " + cus_LName + " = '" + cus.LName + "'" +&#

93、160;               ", " + cus_FName + " = '" + cus.FName + "'" +                ", " + cus_Address + "

94、 = '" + cus.Address + "'" +                ", " + cus_Tel + " = '" + cus.Tel + "'" +             &#

95、160;  " where cus_ID = '" + cus.cusID + "'"             OleDbCommand cmd = new OleDbCommand(selectStr,cnn);             cmd.ExecuteNonQuery(); 

96、;                       CloseCnn();                        /standard dataset function that

97、 finds and         /return the detail of a customer in a dataset        public DataSet Find(String argStr)                    DataSet ds=nul

98、l;             try                            OpenCnn();         

99、                   String selectStr = "select * from " + thisTable +                       

100、0;       " where cus_ID = '" + argStr + "'"                OleDbDataAdapter da =               &#

101、160;        new OleDbDataAdapter(selectStr,cnn);                ds = new DataSet();                da.Fill(ds,thisT

102、able);                            CloseCnn();                     

103、0;                   catch(Exception e)                            String Str = e.Message;      

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论