軟考-特殊名詞-數據庫程序在線訪問方式

軟考-特殊名詞-數據庫程序在線訪問方式

軟考當中,可能會遇到一個特殊名詞“程序在線訪問”,題目如最後。很多同學都比較奇怪這個說法,什麼是“程序在線訪問”?這個是微軟.NET平臺中的http://Ado.net技術特有的一種說法。指的使用http://Ado.net技術中的Connection, Command, DataReader, Transaction, ParameterCollection, 和 Parameter類,以只讀的方式,聯機實時查詢數據庫的特定業務數據,而不是把數據庫的數據採用脫機的方式,將數據庫表的數據直接獲取後放入DataSet, DataTable中,在C#或者.NET其他語言中進行下一步的計算。

該軟件企業很快決定基於.NET平臺和SQL Server數據庫進行開發,但在數據庫訪問方式上出現了爭議。王工認爲應該採用程序在線訪問的方式訪問數據庫;而李工認爲本企業內部程序員缺乏數據庫開發經驗,而且應用筒單,應該採用ORM (對象關係映射)方式。

題目意思:

  1. 王工認爲可以採用http://ado.net技術中的Connection, Command, DataReader, Transaction, ParameterCollection, 和 Parameter類,直接聯機的方式,只讀的形式,通過事先寫好的sql語句,傳輸給sql server,讓sql server執行預先定義的sql腳本,獲取相關業務數據。
  2. 李工則認爲應該採用ORM框架,比如Entity Framework, NHibernate等,實現領域對象(Object)與數據庫關係表(Relation)的自動映射,減少sql腳本工作量。

 

在開始設計.NET體系架構時,微軟就決定重新設計數據訪問模型,以便能夠完全的基於XML和離線計算模型。兩者的區別主要有:
(1)ADO以Recordset存儲,而http://ADO.NET則以DataSet存儲。Recordset看起來更像單表,如果讓Recordset以多表的方式表示就必須在SQL中進行多表連接。反之,DataSet可以是多個表的集合。
(2)ADO的運作是一種在線方式,這意味着不論是瀏覽或更新數據都必須是實時的。http://ADO.NET則使用離線方式,在訪問數據的時候http://ADO.NET會利用XML製作數據的一份副本,http://ADO.NET的數據庫連接也只有在這段時間需要在線。
(3)由於ADO使用COM技術,這就要求所使用的數據類型必須符合COM規範,而http://ADO.NET基於XML格式,數據類型更爲豐富並且不需要再做COM編排導致的數據類型轉換,從而提高了整體性能。
圖2描繪了http://ADO.NET體系。圖3給出了http://ADO.NET基於XML進行數據傳遞的原理圖。

 

Connected and Disconnected Data
As mentioned earlier, http://ADO.NET supports two different programming environments: connected and disconnected.
The connected environment provides forward-only, read-only access to data in the data source and the ability to execute commands against the data source. The connected classes provide a common way to work with connected data regardless of the underlying data source. They include 
ConnectionCommandDataReaderTransactionParameterCollection, and Parameter classes.
The disconnected environment allows data retrieved from the data source to be manipulated and later reconciled with the data source. The disconnected classes provide a common way to work with disconnected data regardless of the underlying data source. They include the 
DataSetDataTableDataColumnDataRowConstraintDataRelationship, and DataView classes.
Finally, http://ADO.NET introduces the connected 
DataAdapter class to bridge the data source and disconnected classes by way of the connected classes. The DataAdapter is an abstraction of the connected classes that simplifies filling the disconnected DataSetor DataTable classes with data from the data source and updating the data source to reflect any changes made to the disconnected data. Figure 1-1 shows the relationship between the connected and disconnected classes in ADO.NET.


Figure 1-1. The connected and disconnected http://ADO.NET classes

Connected Classes
The following classes are used by http://ADO.NET to communicate directly with the data source:

ConnectionMaintains information required to connect to the data source through a connection string. The connection string contains information such as the name of the data source and its location, and authorization credentials and settings. The Connection class has methods to open and close the connection, for transactions to be initiated on the connection, as well as control other properties of the connection.CommandExecutes SQL statements or stored procedures against the data source. The command class has a ParameterCollection object containing Parameterobjects that allow parameterized SQL statements and stored procedures to be used against the data source.DataReaderProvides connected forward-only, read-only access to the data source. It is optimized for speed. The DataReader is instantiated through a Command object.ParameterAllows parameters for both parameterized queries and stored procedures to be defined and set to appropriate values. The Parameter class is accessed through the ParametersCollection object within a Command object. It supports input and output parameters as well as return values from stored procedures.TransactionAllows transactions to be created on a connection so that multiple changes to data in a data source are treated as a single unit of work and either all committed or cancelled.DataAdapterBridges the data source and the disconnected DataSet or DataTable classes. The DataAdapter wraps the connected classes to provide this functionality. It provides a method to retrieve data into a disconnected object and a method to reconcile modified data in the disconnected object with the data source. The CommandBuilder class can generate the logic to reconcile changes in simple situations; custom logic can be supplied to deal with complex situations and optimize performance.
Disconnected Classes
The following http://ADO.NET classes allow data to be retrieved from the data set, examined and modified offline, and reconciled with the data source through the 
DataAdapter:
DataSetProvides a consistent way to deal with disconnected data completely independently of the data source. The DataSet is essentially an in-memory relational database, serving as a container for the DataTableDataColumnDataRowConstraint, and DataRelation objects.
The XML format serializes and transports a 
DataSet. A DataSet can be accessed and manipulated either as XML or through the methods and properties of the DataSetinterchangeably; the XmlDataDocument class represents and synchronizes the relational data within a DataSet object with the XML Document Object Model (DOM).DataTableAllows disconnected data to be examined and modified through a collection of DataColumn and DataRow classes. The DataTable allows constraints such as foreign keys and unique constraints to be defined using the Constraintclass.DataColumnCorresponds to a column in a table. The DataColumn class stores metadata about the structure of the column that, together with constraints, defines the schema of the table. The DataColumn can also create expression columns based on other columns in the table.DataRowCorresponds to a row in a table and can examine and update data in the DataTable. The DataTable exposes DataRow objects through the DataRowCollection object it contains. The DataRow caches changes made to data contained in its columns, storing both original and current values. This allows changes to be cancelled or to be later reconciled with the data source.ConstraintAllows constraints to be placed on data stored within a DataTable. Unique and foreign key constraints can be created to maintain data integrity.DataRelationProvides a way to indicate a relationship between different DataTable objects within a DataSet. The DataRelation relates columns in the parent and child tables allowing navigation between the parent and child tables and referential integrity to be enforced through cascading updates and deletes.DataViewAllows data, once retrieved into a DataSet or DataTable, to be viewed in different ways. It allows data to be sorted based on column values and for a subset of the data to be filtered so that only rows matching specified criteria are displayed.
Chapter 2 examines .NET data providers in more detail and describes the way in which they provide connected functionality and serve as a bridge to disconnected functionality.

 

某製造企業爲拓展網上銷售業務,委託某軟件企業開發一套電子商務網站。初期僅解決基本的網上銷售、訂單等功能需求。該軟件企業很快決定基於.NET平臺和SQL Server數據庫進行開發,但在數據庫訪問方式上出現了爭議。王工認爲應該採用程序在線訪問的方式訪問數據庫;而李工認爲本企業內部程序員缺乏數據庫開發經驗,而且應用筒單,應該採用ORM (對象關係映射)方式。最終經過綜合考慮,該軟件企業採用了李工的建議。
隨着業務的發展,該電子商務網站逐漸發展成一個通用的電子商務平臺,銷售多家制造企業的產品,電子商務平臺的功能也日益複雜。目前急需對該電子商務網站進行改造,以支持對多種異構數據庫平臺的數據訪問,同時滿足複雜的數據管理需求。該軟件企業針對上述需求,對電子商務網站的架構進行了重新設計,新增加了數據訪問層,同時採用工廠設計模式解決異構數據庫訪問的問題。新設計的系統架構如圖4-1所示。

問題:4.1 (9分)
請用300字以內的文字分別說明數據庫程序在線訪問方式和ORM方式的優缺點,說明該軟件企業採用ORM的原因。問題:4.2 (9分)
請用100字以內的文字說明新體系架構中增加數據訪問層的原因。請根據圖4-1所示,填寫圖中空白處(1) - (3)。問題:4.3 (7分)
應用程序設計中,數據庫訪問需要良好的封裝性和可維護性,因此經常使用工廠設計模式來實現對數據庫訪問的封裝。請解釋工廠設計模式,並說明其優點和應用場景;請解釋說明工廠模式在數據訪問層中的應用。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章