tailieunhanh - Building XML Web Services for the Microsoft .NET Platform phần 3

tận dụng các hạn chế mô hình trong định nghĩa kiểu đơn giản của bạn, bạn có thể làm giảm đáng kể số lượng mã xác nhận, bạn cần phải viết cho dịch vụ Web của bạn. Chúng ta hãy xem xét một ví dụ về nơi mà một hạn chế mô hình có thể hữu ích. | leveraging pattern constraints within your simple type definition you can significantly reduce the amount of validation code you need to write for your Web service. Let s take a look at an example of where a pattern restriction can be helpful. Recall that the Orderitem method exposed by the Commerce Web service accepts a parameter called Item. In the previous example I defined a type called Productid for defining the type of data that can be contained within the Item element. In addition to length restrictions suppose that an instance of a Productid cannot contain the following characters . If I were to use the Productid type as defined previously I would have to write code to ensure that no illegal characters were included within the item element. Instead I will add a pattern constraint to the Productiddefinition that restricts the type of characters that values of that type can contain. xml version schema xmlns http 2001 xmlns tns urn Commerce targetNamespace urn Commerce simpleType name ProductId restriction base string minLength value 1 maxLength value 20 pattern value A x5B x5D gt lt restriction simpleType -- Request Message work-in-progress -- element name Item type tns ProductId -- Respons e Message work-in-progress -- element name Amount type double nillable true schema Another useful constraint is the enumeration. The value of an enumeration can contain one of a fixed set of possible values. For example suppose I want to restrict the value of the item attribute to one of a set of possible values. The following example creates a datatype called items that can contain the possible values of the item element xml version schema xmlns http 2001 xmlns tns urn Commerce targetNamespace urn Commerce -- Removed the ProductId type definition for clarity simpleType name Items restriction base ProductId 78 enumeration value Apple enumeration value Banana enumeration value Orange restriction simpleType -- Request