




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、基于.NET的可重用数据访问层的构建作者:王远斌摘要:有人提出一种新的数据访问层的N层架构的设计解决方案。它可以解决多种问题,例如:低效率的开发,移植、更新和重用过程中的困难。该解决方案采用了.NET的反射技术和设计模式。这种解决方案的一种典型应用表明:新的数据访问层的解决方案的性能优于目前的N层架构。更重要的是,应用表明:新的数据访问层的解决方案可以有效地重用。关键词:N层架构;数据访问层;反射技术;重用1 引言在软件开发过程中,软件系统的结构设计一直发挥着关键的作用。对于大型企业级应用来说,系统架构不仅影响系统设计和系统开发,而且与后续系统开发有直接联系,它甚至可以决定整个系统的开发是否成
2、功。目前,由于其明显的优势,N层架构已经成为一种标准的构建企业级应用。然而,当前庞大而复杂的N层体系架构出现了一些问题,例如:低效率的开发,移植、更新和重用过程中的困难。文中提出了基于.NET技术和N层架构的解决方法,即如何建立一种新的利用反射和工厂模式的数据访问层。2 N层架构的采用N层架构是一种基于双层架构的新型软件结构,以便于有效地处理大规模分布应用中的问题。在双层架构中,客户端的数据库和业务逻辑的存储程序是一个抽象和自我管理的应用逻辑层,这种存储程序也纳入了应用服务器中。通过这种方式,N层架构应运而生。在N层架构中,业务逻辑在应用服务器中,应用服务器中包含了业务规则、数据处理等。N层架
3、构具有以下优点:良好的透明性和封装;高内聚、低耦合;易扩展、易更新、重用性好;便于开发者合作;高效率的开发。最常见的N层架构有三个不同的层次,包括用户界面、业务逻辑层和数据访问层。用户界面层仅仅是一种用来处理用户界面应用程序的软件,它的主要任务是实现服务器和客户端的通信。可以是一个网页,或者是传统的命令提示符界面。这一层还负责获取用户的数据输入,并将他们传递给业务逻辑层。业务逻辑层包含了业务规则、数据处理等,这层并不知道任何关于HTML及怎样将其输出的信息。它不关心ADO或SQL,这些任务分配它相应的上层或下层来完成。这层的核心职能是接收用户界面的请求、按照有关的业务逻辑处理请求、发送请求并接
4、收相应的结果,将这些结果返回给用户界面层。数据访问层是整个架构的最底层,它为业务逻辑层提供数据服务,在一些数据存储(关系型数据库和XML等)中存储或检索数据。3 .Net中的反射和工厂模式3.1 .NET中的反射在.NET中,使用CLR(公共语言运行库)提供反射技术,这种技术可以在运行时获得包含在集合中的信息类型。通常,.NET应用程序包括如下几部分:组件,模块和类型。反射提供了一种编程方式,并且允许程序员在运行时检查类型信息并对这些类型调用方法。例如,一个组件是一个可重用和可自我描述的通用语言运行库应用程序的构件。它可以在运行时加载组件,并获得该组件中的所有类型。类型是获取元数据的主要手段,
5、可以作为映像API的门户。它提供了多种方法来获取类型声明的信息,如构造函数、属性、方法和事件。方法信息包括方法的信息,可以发现方法的属性,并提供元数据的获取方法。它给出了方法的名称、参数,返回它的值,甚至还可以调用该方法。通过反射技术,可以实现多种功能,例如:创建一种类型的实例,将这一类型与当前的对象绑定以及获得目前对象的类型。当应用程序在运行中为了执行某一操作需要加载一个特殊类型时,可以使用反射技术。3.2 工厂模式伯特兰·迈耶建议软件实体应该是可扩展的,但是不可修改的。GOF(Gang of Four:四人组)的工厂模式在一定程度上遵守这种原则,并已在软件开发中获得了广泛的应用。
6、工厂模式负责建立有公共接口的类型的实例。通过工厂模式,某些类型的实例可以被动态地创建,而不需要预先了解类型名称的信息。4 数据访问层的设计虽然目前流行的几种多层结构(包括3层、4层和5层)比以往发挥着更为重要的作用,但是,当系统庞大而且数据库复杂时,多层结构的开发似乎仍然很困难。现有的框架直接采用存储过程或程序中的SQL语句。因此,大量的SQL语句需要组建,使得数据访问层的工作量巨大、效率低,甚至导致移植和更新困难。.NET的反射技术与工厂模式的结合产生了一种新的数据访问层的解决方案。在数据库转换时,工厂模式使你可以轻而易举地更新系统。基于OOP(面向对象的程序设计),开发人员从繁重的工作和复
7、杂的SQL语言中解放了出来。所有的SQL语句将由开发人员熟悉的OOP构建,与此同时,新的数据访问层具有较高的重用性和灵活性,从而避免了过去的无意义而又重复的开发。这项设计的的原理包括:1) 使用工厂模式构建数据访问层,以便于克服数据库转换引起的问题;2) 利用反射技术动态构建所需的SQL语句,以避免频繁的、低效的SQL语句编写;3) 引入数据实体层;4) 引入数据访问子层框架子层,并实现再利用。数据访问层的设计如下:1) 引入数据实体层在这个解决方案中,通过ORM(对象关系映射),所有的数据实体作为作为一个单一的层数据实体层取出。通过这种方式,对象将被映射到RMDB(关系映射数据库)中。当对数
8、据库执行操作时,程序员就不再需要处理复杂的SQL语句了,它可以像对象一样被处理。与此同时,数据实体以对象的形式显示数据,并且以消息的形式在层与层之间发送和接收。2) 数据访问层的设计本解决方案中,数据访问层被分为两个子层,DASL子层和框架子层。这种新型架构的结构如图1所示。DASL子层不再直接与数据库进行通信,它只负责封装业务逻辑层的请求并将信息传送给下层。框架子层接收上层的请求,然后自动创建所有的SQL语句。该框架子层独立于应用程序,因此可以高度地重复使用。居中框架子层的结构如图2所示。具体描述如下:1) 查询语句这个类的核心功能是创建SQL语句,以下面的查询语句为例:“SELECT fi
9、eld FROM table name WHERE condition ORDER BY sort”.由于该表已经被映射到实体类中,诸如字段、表名、条件和排序的变量就可以通过使用.NET的反射技术获得了。居中例如,构成SQL语句:Select * from table name where condition的构造函数如下:public Query Statement (object obj) this.dataType = obj.GetType (); this. fields = "*" this. filter = ParseObjectFilter (obj);
10、正如上面的代码所示,首先,输入对象的类型可以通过反射获得,这样更容易获得表名。其次,在”ParseObjectFilter ()”方法中,当前对象的所有字段可以使用反射来构造查询条件字符串。2) 存储管理器这种类型的主要作用是执行SQL语句。根据输入对象的实例,它通过调用查询语句的方法来形成SQL语句,然后使用ADO.NET执行该语句,并将执行结果返回给上层。以查询语句Select * from table name where condition为例:public static void LoadDataSet (QueryStatement query, Datasets dataSet)
11、 cmd.CommandText = query.ToString (); adapter.SelectCommand = cmd; (DbDataAdapter) adapter).Fill (dataSet, query.DataSetTable); 应该注意到,输入的对象”query”已包含了构造的SQL语句。5 通告系统应用程序现在来介绍被称为通告系统的应用程序,它采用了上面提到的数据访问层。这种系统是一种自动通知和信息采集系统,常在突发事件中被用来自动收集和发出信息,例如灾难恢复和保持事务连续性。这一系统的原理如图3所示。该系统的功能包括通知快速行动部分、调动快速行动组、更新管理系统
12、、达到警戒线时预警并发送预警信息,以及发送信息和指令。居中应用程序在.NET平台上开发,开发语言采用C#,数据库操作系统是SQL Server 2000,系统基于B/S构建,用户接口使用ASP.NET开发。图1中所示的N层架构被采用了,即在原来系统中使用的被证明是正确和可靠的架构被直接引入了该系统。由于包含框架子层,数据访问层的采用简化了数据实体层的封装,使开发人员摆脱了数据实体层的封装,允许面向对象的数据库操作,这种操作让开发人员从处理复杂SQL语句中解放了出来。另外,由于这层的高度重用性,开发过程被缩短,开发效率明显提高。6 结论文中介绍了一种新的数据库访问层设计解决方案,该方案采用了反射
13、技术和设计模式。这种解决方案适用于典型的系统并且执行良好。致谢非常感谢重庆大学的王晶、蔡华和徐红的帮助。参考文献1Zhang P, Tang F, Lin G.软件架构设计在XP中的实施J. 计算机工程与应用, 2003, 39 (33): 106-109. 2Simon R, Ollie C. C#高级编程 M.第1版.北京: 清华大学出版社, 2002: 339-348.3Yan H. Java与模式 M. 第1版.北京:电子工业出版社,2004.127-206. 4Xu H, Xu L.基于.NET的信息管理系统J. 武汉工程学院, 2003, 15(2): 39-41. 5Wang Y
14、, Chen P.反射技术与软件的适应性J. 计算机工程与设计, 2003, 24(10): 26-29. 6Xu W, He J, Zhang J, et al.基于反射技术的动态界面的实现J. 计算机工程与设计, 2003, 24 (10): 57-59. 7Hu Y, Peng L, Chi C.基于.NET的三层架构设计 J. 计算机工程与设计, 2003, 29 (8): 173-175.Construction of a reusable data access layer based on .NETWANG Yuan-binAbstract: A new design solut
15、ion of data access layer for N-tier architecture is presented. It can solve the problems such as low efficiency of development and difficulties in transplantation update and reuse. The solution utilizes the reflection technology of .NET and design pattern. A typical application of the solution demon
16、strates that the new solution of data access layer performs better than the current N-tier architecture. More importantly, the application suggests that the new solution of data access layer can be reused effectively. Keywords: N-tier architecture; data access layer; reflection technology; reuse 1 I
17、ntroduction In the course of software development, the architecture design of software system plays a key role all the time. For large enterprise application, the system architecture can influence not only the system design and the system development, but also the follow-up development of the system
18、 directly. It can even determine whether the whole system development is successful. Currently, because of its obvious advantages, N-tier architecture has already become a standard of structuring enterprise application. However, at present, the enormous and complex N-tier architecture has some probl
19、ems, such as low efficiency of development, and difficulties in transplantation, update and reuse. Based on .Net technology and N-tier architecture, this paper puts forward a solution to how to build a new data access layer by utilizing reflection and factory pattern.2 Introduction of N-tier archite
20、ctureN-tier architecture is a new kind of software architecture based on two-tier architecture in order to deal with the problems of large scale distribution applications effectively. The storage procedure of database and business logic of a client in two-tier architecture is an abstracted and self-
21、governed application logical layer, which is incorporated in the application server. In this way, N-tier architecture comes into being. In N-tier architecture, business logic is in application server. Application server contains business rules, data manipulation, etc. This N-tier architecture has th
22、e following advantages: good transparent and encapsulation; high cohesiveness and low coupling; easy to expand, update and reuse; convenient for developers to cooperate; and improved efficiency for development. The most commonly N-tier architecture has three distinct layers, including user interface
23、, business logical layer and data access layer. A user interface layer is only a piece of software that deals with the user interface of an application. This layer is responsible for the communication between the server and client. It can be a web page or a traditional order prompt interface. This l
24、ayer is also responsible for accessing the data a user inputs and transmitting them to the business logical layer. A business Logic Layer contains the business rules, data manipulation, etc. This layer does not know anything about HTML and how to output it. It does not care about ADO or SQL. Those t
25、asks are assigned to each corresponding layer above or below it. The core functions of the layer are accepting request from the users interface, dealing with the request according to relevant business logic, sending out request and receiving corresponding result and returning the result to the user
26、interface layer. The data access layer is the bottom layer of the whole architecture, which provides the business logic layer with data services, and stores or retrieves data in some data store (RDBMS, XML etc.).3 Reflection in .NET and factory pattern3.1 Reflection in .NETIn .NET, CLR (common langu
27、age runtime) provides reflection technology by which information about types contained in an assembly at runtime can be obtained. Usually, applications in .NET have several parts as follows: assemblies, modules and types. Reflection provides a kind of programming way, and allows a programmer to insp
28、ect type information and invoke methods on those types at runtime. For example, an assembly is a reusable and self describing building block of a Common Language Runtime application. It can load assembly at runtime, and get all the types in that assembly. Type is the primary means by which metadata
29、can be accessed and it acts as a gateway to reflection API. It provides methods for obtaining information about a type declaration, such as the constructors, properties, methods and events. Method Information, including the information of a method, discovers the attributes of a method and provides a
30、ccess to method metadata. It provides the name, the parameters of a method, returns its value, and even calls it. Through the reflection technology, many functions can be realized, such as creating instance of a type, binding the type to a current object and obtaining the type from a current object.
31、 Reflection can be used when the application program need to load one special type at runtime in order to implement a task. 3.2 Factory patternBertrand Meyer proposed that software entities should be open for extension, but close for modification. GOF's Factory Pattern complied with the principl
32、e to a certain degree and has already won extensive application in software development. Factory pattern is responsible for building the instance of type which has the common interface. Through factory pattern, the instance of some type can be dynamically created, without information about type name
33、 in advance. 4 Design of data access layerThough several kinds of multi-tier frameworks that prevail at present (including 3-tier, 4-tier, and 5-tier) have played a greater role than ever, the development of multi-tier still seems difficult when a system is huge and the database is complicated. The
34、existing framework adopts the store procedure or SQL sentences in the program directly. Hence, a large number of SQL sentences need to be constructed, which make the work load of data access layer enormous and inefficient, even cause difficulties in transplantation and update. Combining the reflecti
35、on technology of .NET with factory pattern gives rise to a new solution of data access layer. Factory pattern offers easy update to the system when the database is switched. Based on OOP, the developers are liberated from heavy work and complicated SQL language. All of the SQL sentences will be buil
36、t up by OOP with which a developer is familiar. Meanwhile, the new data access layer is of high reuse and flexibility. It avoids meaningless and repeated development in the past. The Philosophy of design involves1) Using the factory pattern to construct the data access layer so as to overcome the pr
37、oblems caused by database switching;2) Utilizing reflection technology to construct required SQL sentences dynamically to avoid the frequent and inefficient write of SQL sentences;3) Importing the data entity layer; and4) Importing the data access sub layer-framework sub layer, and realize the reuse
38、.The data access layer is designed as follows. 1) Import the data entity layer In this solution, through ORM (Object-Relational Mapping), all data entities are taken out as a single layer, data entity layer. In this way, objects will be, mapped to RMDB. When manipulate database, programmers do not n
39、eed to deal with complicated SQL sentences any more. It can be handled like an object. Meanwhile, the data entities display the data in the form of object. They are sent and received as messages between layers. 2) Design of the data access layer In this solution, data access layer is divided into tw
40、o sub layers, DASL sub layer and framework sub layer. The structure of this new architecture is illustrated in Fig.1. Now DASL sub layer no longer communicates with database directly. It only encapsulates the request from BL layer and sends message to a lower layer. The framework sub layer receives
41、the request from upper layer, and then builds all SQL sentences automatically. The framework sub layer is independent from application, so can be highly reused.The structure of the framework sub layer is illustrated in Fig. 2, and described below. 1) Query Statement The core function of this class i
42、s to build SQL sentences. Take a query sentence for example: “SELECT field FROM tablename WHERE condition ORDER BY sort”.Because the tables have been mapped to entity classes, variables such as field, tablename, condition and sort, can be obtained by using the reflection of .NET.For example, the con
43、struction function which constructs the SQL sentence Select * from table name where condition is like this: public Query Statement (object obj) this.dataType = obj.GetType (); this.fields = "*" this.filter = ParseObjectFilter (obj); As the above code shows, firstly, the type of input objec
44、t can be gotten by using the reflection, which makes it easy to get the table name. Secondly, in method ParseObjectFilter (), all fields of current object are obtained by utilizing reflection to construct the query condition string.2) Persistence manager The principal function of this type is to exe
45、cute SQL sentences. According to the instance of input object, it forms SQL sentences by calling the method of query statements, then executes SQL sentences by using ADO.NET and returns the result to the upper layer.Take the query sentence Select * from tablename where condition for example:public s
46、tatic void LoadDataSet (QueryStatement query, DataSet dataSet) cmd.CommandText = query.ToString(); adapter.SelectCommand = cmd; (DbDataAdapter) adapter).Fill (dataSet, query.DataSetTable); It should be noticed that the input object query has included constructed SQL sentence.5 Application to a notif
47、ication system An application, known as notification system, is presented here. It utilizes the data access layer mentioned above. The system is an auto-notification and information collecting system which is used to collect and send information automatically in the cases of sudden events, such as d
48、isaster recovery and keeping transaction continuity. The principle of the system is shown in Fig. 3. The functions of the system includes informing the quick action department, dispatching the quick action group, updating the management system, forewarning and sending forewarning information in the
49、warning line, and sending messages and instructions. The application was developed on the .NET platform. The development language is C#. The DBMS is SQL Server 2000. The system was constructed based on B/S and UI was developed by ASP.NET. The N-tier architecture shown in Fig. 1 was adopted. Framework, which was used in the former system and proved correct and reliable, was directly imported to the system. With the framework sub-layer, the adoption of the data access layer simplifies the encapsulation
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 化学工业中有机化学反应工程与工艺流程题解集
- 现代诗的结构与意象解读:课文白桦林教案
- 一世笑红尘1500字(15篇)
- 前人给我们留下的阴凉400字9篇范文
- 《语文古典诗歌欣赏教案:唐诗三首》
- 《艺术欣赏:西方绘画技法教学教案》
- 智能硬件系统集成服务合同协议
- 开国典礼读写课:初中记叙文教学与写作教案
- 走进自然感受生活记事作文14篇范文
- 八月十五放鞭炮活动方案
- xx县精神病医院建设项目可行性研究报告
- ECMO技术参数要求
- 城市轨道交通供电技术442页完整版教学课件汇总全书电子教案
- 高填深挖路基稳定性监控观测方案
- 安全标准化现场评审所需资料清单(共14页)
- 班组会议运作技巧ppt课件
- 链家房屋买卖合同范本(共10篇)
- 柱上变压器台与设备安装标准
- 技术比武理论复习题(继电保护)
- 锯齿形板式热水冷却器的设计3.
- 科室医疗质量安全管理与持续改进记录本模板.doc
评论
0/150
提交评论