揭秘ADO.NET批注在編程中的重大意義
隨著時代的發(fā)展,我們要學(xué)的東西越來越多,這里我們就共同學(xué)習(xí)學(xué)習(xí)ADO.NET批注類型化數(shù)據(jù)集。ADO.NET批注使您能夠在不修改基礎(chǔ)架構(gòu)的情況下修改類型化 DataSet 中元素的名稱。如果修改基礎(chǔ)架構(gòu)中元素的名稱,則會使類型化 DataSet 引用不存在于數(shù)據(jù)源中的對象,并且會丟失對存在于數(shù)據(jù)源中的對象的引用。
利用批注,您可以使用更有意義的名稱來自定義類型化 DataSet 中對象的名稱,從而使代碼更易于閱讀,類型化 DataSet 更易于為客戶端使用,同時保持基礎(chǔ)架構(gòu)不變。例如,Northwind 數(shù)據(jù)庫中 Customers 表的以下架構(gòu)元素會生成 CustomersRow 這一 DataRow 對象名稱和一個名為 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 則會導(dǎo)致誤解,因為它是單個對象。此外,在通常情況下,將不使用 Row 標識符來引用該對象,而僅將該對象當作 Customer 對象來引用。解決方案是為架構(gòu)添加ADO.NET批注并標識 DataRow 和 DataRowCollection 對象的新名稱。下面是上一架構(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 對象名稱 Customer。將 typedPlural 的值指定為 Customers 則會保留 DataRowCollection 名稱 Customers。
若要使用類型化 DataSet 批注,則必須在 XML 架構(gòu)定義語言 (XSD) 架構(gòu)中包含以下 xmlns 引用。
- xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"
下面是一個ADO.NET批注架構(gòu)示例,它公開 Northwind 數(shù)據(jù)庫的 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>