WCF Technical Analysis 14: Generic Data contracts and aggregate data contracts (next)

Source: Internet
Author: User

In. NET, all collections implement IEnumerable interfaces, such as array, Hashtable, ArrayList, Stack, queue, and so on. Some sets require elements to have the same type, which is generally defined by generics, and they implement another interface ienumerable<t> (Ienumerable<t> itself inherits from IEnumerable), and such collections have a list <T>, dictionary<tkey,tvalue>, stack<t>, queue<t> and so on. Serialization based on collection type has some special rules and behaviors, in the last chapter we introduce the serialization rules based on the generic data contract, and then we introduce the serialization based on the collection object and the service operation based on the collection type.

First, ienumerable<t>, array and ilist<t>

The premise that a collection object can be serialized is that every element in the collection can be serialized, that is, the type of the element is required to be a data contract (or the Serialiableattribute attribute is applied). Although the collection has a variety of representations, because its essence is a group of objects, DataContractSerializer serialization of them, the use of serialization rules and serialization of the behavior shown in the process is similar. For example, we now need to serialize a collection of customer objects through DataContractSerializer, and the customer type is defined as follows.

   1:namespace Artech.datacontractserializerdemos
2: {
3: [DataContract (namespace= "http://www.artech.com/")]
4: Public class Customer
5: {
6: [DataMember (order = 1)]
7: Public Guid ID
8: {get; set;}
9:
[DataMember (order=2)]
One: public string Name
: {get; set;}
13:
: [DataMember (order = 3)]
: Public String Phone
: {get; set;}
17:
[DataMember (order = 4)]
: Public String companyaddress
{get ; set;}
: }
22:}

Now I serialize the following 3 different types of collection objects through the generic serialize<t> we defined earlier:ienumerable<customer>, ilist<cusomter>, and Customer []。

1:customer Customerfoo = new Customer
2: {
3:id = Guid.NewGuid (),
4:name = "Foo",
5:phone = "8888-88888888",
6:companyaddress = "#9981, West Sichuan Rd, Xian Shanxi Province"
7:};
8:
9:customer Customerbar = new Customer
10: {
11:id = Guid.NewGuid (),
12:name = "Bar",
13:phone = "9999-99999999",
14:companyaddress = "#3721, Taishan Rd, Jinan Shandong Province"
15:};
16:customer[] Customerarray = new customer[] {customerfoo, customerbar};
17:ienumerable<customer> customercollection = Customerarray;
18:ilist<customer> customerlist = customerarray.tolist<customer> ();
19:
20:serialize<customer[]> (Customerarray, @ "E:\Customer.Array.xml");
21:serialize<ienumerable<customer>> (Customercollection, @ "E:\Customer.GenericIEnumerable.xml");
22:serialize<ilist<customer>> (CustomerList, @ "E:\Customer.GenericIList.xml);

We finally found that although the types used to create DataContractSerializer objects were different, the resulting serialization of XML was exactly the same, meaning that datacontractserializer when serializing these 3 types of objects, Use the exact same serialization rules. From the structure and content of the following XML, we can summarize the following 3 rules:

The root node name is prefixed with arrayof, followed by the data contract name for the collection element type;

A collection element object uses the namespace of the data contract as the namespace of the whole set contract;

Each element object is serialized according to its data contract definition.

 1: <arrayofcustomer xmlns:i= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://www.artech.com/"         
2: <customer>
3: <id>8baed181-bcbc-493d-8592-3e08fd5ad1cf</id>
4: <name>foo</name>
5: <phone>8888-88888888</phone>
6: <companyad Dress> #9981, West Sichuan Rd, Xian Shanxi Province</companyaddress>
7: </customer>
8: & Lt Customer>
9: <id>2fca9719-4120-430c-9dc2-3ef9dc7dffb1</id>
: <NAME>BAR&L T;/name>
One: <phone>9999-99999999</phone>
<CompanyAddress> #3721, Taisha N Rd, Jinan Shandong Province</companyaddress>
: </customer>
: </arrayofcustomer>

We can see from the name of the root node that WCF Arrayofcustomer this 3-type Object ienumerable<customer>, Ilist<cusomter>, and customer[] As a customer array. In fact, if you are defining a service contract, the parameter type of a service operation is set to ienumerable<t> or <ILIST>, and the corresponding parameter type is the array type in the service contract generated by the default export. For example, in the same service contract, I have defined the following 3 operations, their parameter types are ienumerable<customer>, ilist<cusomter> and customer[] respectively. When a client exports a service contract by adding a service reference, the parameter types of the 3 operations become customer[].

   1: [ServiceContract]
2:public Interface Icustomermanager
3: {
4: [OperationContract]
5: void Addcustomerarray (customer[] customers);
6: [OperationContract]
7: void Addcustomercollection (ienumerable<customer> customers);
8: [OperationContract]
9: void Addcustomerlist (ilist<customer> customers);
10:}
1: [ServiceContract]
2: [ServiceContract]
3:public Interface Icustomermanager
4: {
5: [OperationContract]
6: void Addcustomerarray (customer[] customers);
7: [OperationContract]
8: void Addcustomercollection (customer[] customers);
9: [OperationContract]
(a ): void Addcustomerlist (customer[] customers);
11:}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.