揭秘ADO.NET批注在編程中的重大意義
隨著時(shí)代的發(fā)展,我們要學(xué)的東西越來(lái)越多,這里我們就共同學(xué)習(xí)學(xué)習(xí)ADO.NET批注類型化數(shù)據(jù)集。ADO.NET批注使您能夠在不修改基礎(chǔ)架構(gòu)的情況下修改類型化 DataSet 中元素的名稱。如果修改基礎(chǔ)架構(gòu)中元素的名稱,則會(huì)使類型化 DataSet 引用不存在于數(shù)據(jù)源中的對(duì)象,并且會(huì)丟失對(duì)存在于數(shù)據(jù)源中的對(duì)象的引用。
利用批注,您可以使用更有意義的名稱來(lái)自定義類型化 DataSet 中對(duì)象的名稱,從而使代碼更易于閱讀,類型化 DataSet 更易于為客戶端使用,同時(shí)保持基礎(chǔ)架構(gòu)不變。例如,Northwind 數(shù)據(jù)庫(kù)中 Customers 表的以下架構(gòu)元素會(huì)生成 CustomersRow 這一 DataRow 對(duì)象名稱和一個(gè)名為 Customers 的 DataRowCollection。
- <xs:element name="Customers">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="CustomerID" type="xs:string" minOccurs="0" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
#T#DataRowCollection 名稱 Customers 在客戶端代碼中是有意義的,但 DataRow 名稱 CustomersRow 則會(huì)導(dǎo)致誤解,因?yàn)樗菃蝹€(gè)對(duì)象。此外,在通常情況下,將不使用 Row 標(biāo)識(shí)符來(lái)引用該對(duì)象,而僅將該對(duì)象當(dāng)作 Customer 對(duì)象來(lái)引用。解決方案是為架構(gòu)添加ADO.NET批注并標(biāo)識(shí) DataRow 和 DataRowCollection 對(duì)象的新名稱。下面是上一架構(gòu)的批注版本。
- <xs:element name="Customers" codegen:typedName="Customer" codegen:typedPlural="Customers">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="CustomerID" type="xs:string" minOccurs="0" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
將 typedName 的值指定為 Customer 將生成 DataRow 對(duì)象名稱 Customer。將 typedPlural 的值指定為 Customers 則會(huì)保留 DataRowCollection 名稱 Customers。
若要使用類型化 DataSet 批注,則必須在 XML 架構(gòu)定義語(yǔ)言 (XSD) 架構(gòu)中包含以下 xmlns 引用。
- xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"
下面是一個(gè)ADO.NET批注架構(gòu)示例,它公開 Northwind 數(shù)據(jù)庫(kù)的 Customers 表并包含與 Orders 表的關(guān)系。
- <?xml version="1.0" encoding="utf-8"?>
- <xs:schema id="CustomerDataSet"
- xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"
- xmlns=""
- xmlns:xs="http://www.w3.org/2001/XMLSchema"
- xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="CustomerDataSet" msdata:IsDataSet="true">
- <xs:complexType>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="Customers" codegen:typedName="Customer"
- codegen:typedPlural="Customers">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="CustomerID"
- codegen:typedName="CustomerID" type="xs:string" minOccurs="0" />
- <xs:element name="CompanyName"
- codegen:typedName="CompanyName" type="xs:string" minOccurs="0" />
- <xs:element name="Phone" codegen:typedName="Phone"
- codegen:nullValue="" type="xs:string" minOccurs="0" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- <xs:element name="Orders" codegen:typedName="Order"
- codegen:typedPlural="Orders">
- <xs:complexType>
- <xs:sequence>
- <xs:element name="OrderID" codegen:typedName="OrderID"
- type="xs:int" minOccurs="0" />
- <xs:element name="CustomerID"
- codegen:typedName="CustomerID"
- codegen:nullValue="" type="xs:string" minOccurs="0" />
- <xs:element name="EmployeeID"
- codegen:typedName="EmployeeID" codegen:nullValue="0"
- type="xs:int" minOccurs="0" />
- <xs:element name="OrderAdapter"
- codegen:typedName="OrderAdapter"
- codegen:nullValue="1980-01-01T00:00:00"
- type="xs:dateTime" minOccurs="0" />
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:choice>
- </xs:complexType>
- <xs:unique name="Constraint1">
- <xs:selector xpath=".//Customers" />
- <xs:field xpath="CustomerID" />
- </xs:unique>
- <xs:keyref name="CustOrders" refer="Constraint1"
- codegen:typedParent="Customer" codegen:typedChildren="GetOrders">
- <xs:selector xpath=".//Orders" />
- <xs:field xpath="CustomerID" />
- </xs:keyref>
- </xs:element>
- </xs:schema>