Analysis of WCF Technology 14: Generic Data contracts and aggregate data contracts (last chapter)

Source: Internet
Author: User
Tags datetime header serialization

Generics were introduced for the first time in the. NET Framework 2.0. We can define generic interfaces, generic types, generic delegates, and generic methods. Serialization relies on real-world types, while generics deliberately blur the concept of specific types. A collection represents a combination of a set of objects that has an iterative (enumerable) attribute that can traverse each element of the collection through an iterative rule. Because the generic type and collection type have some special behavior and rules on serialization and deserialization, I'll explain this in more detail in this article. First of all, the generic data contract.

I. Generics and data contracts

Object-oriented inheritance realizes the reuse of code, while generics realizes "the reuse of algorithm". We define an algorithm, such as sorting, searching, swapping, comparing, or converting, and so on, in order to be as reusable as possible, we do not qualify the specific type of the algorithm to manipulate the object, but are represented by a generic type. Specify its specific type when you really create a generic object or call the method.

In the case of implementations, generics are a special mechanism implemented jointly by the CLR and the programming language (or the compiler of a programming language), and this is the object-oriented category in terms of the concept of generics. The data contract we are introducing now is a service-oriented concept. There are some conflicts between the two, such as the normal service does not inherit, the concept of overloading, service-oriented also does not understand generics.

But the programming language based on WCF is C #, VB. NET, and WCF services are based on service-oriented programming languages. So, in a sense, one of the major roles of WCF is to bridge the differences between object-oriented programming (OOP) and service-oriented architecture (SOA). Let's take a look at what WCF has done to enable us to define data contracts in the form of generic types.

Ii. default serialization rules for generic data contracts

Let's start with a simple example to see how DataContractSerializer serializes a generic object. For this I define a generic type Bill<billheader, Billdetail>, representing a general document, Billheader and billdetail the type of detail that represents the document header. Two property headers and details represent the document header and detail list.

   1:namespace Artech.datacontractserializerdemos
2: {
3: [DataContract (namespace= "http://www.artech.com/")]
4: Public class Bill<billheader, billdetail>
5: {
6: [DataMember (order = 1)]
7: Public Billheader Header
8: {get; set;}
9:
[DataMember (order = 2)]
One: Public billdetail[] Details
: {get; set;}
: }
14:}

We then define the types of headers and details used to describe the order documents: Orderbillheader and Orderbilldetail. Orderbillheader describes the overall information of an order, Orderbilldetail actually represents the ID, unit price, and quantity of each product in the order.

 1:namespace Artech.datacontractserializerdemos 
2: {
3: [DataContract (Namespace= Http://www.artec h.com/")]
4:public class Orderbillheader
5: {
6: [DataMember]
7:public G UID OrderID
8: {get; set;}
9:
: [DataMember]
11:public DateTime Date
: {get; set;}
:
: [DataMember]
15:public string Customer
: {get; set;}
17:}
:
[DataContract (namespace= "http://www.artech.com/")]
20:public class Orderbilldetail
2 1: {
: [DataMember]
23:public Guid ProductID
: {get;, set;}
:
: [DataMember]
27:public int Quantity
: {get; set;}
:
[DataMember]
31:public double UnitPrice
: {get; set;}
33:}
:}

Create a generic type Bill<billheader, a Billdetail> object, and a generic type specified as the Orderbillheader and orderbilldetail defined above, in one of the following ways.

1:private static Bill<orderbillheader, orderbilldetail> Createorderbill ()
2: {
3:orderbillheader Header = new Orderbillheader
4: {
5:orderid = Guid.NewGuid (),
6:date = Datetime.today,
7:customer = "Foo"
8:};
9:
10:ilist<orderbilldetail> details = new list<orderbilldetail> ();
11:orderbilldetail detail = new Orderbilldetail
12: {
13:productid = Guid.NewGuid (),
14:quantity = 20,
15:unitprice = 888
16:};
17:details. ADD (detail);
18:detail = new Orderbilldetail
19: {
20:productid = Guid.NewGuid (),
21:quantity = 10,
22:unitprice = 9999
23:};
24:details. ADD (detail);
25:
26:
27:bill<orderbillheader, orderbilldetail> Orderbill = new Bill<orderbillheader, OrderBillDetail> ()
28: {
29:header = Header,
30:details = Details. Toarray<orderbilldetail> ()
31:};
32:return Orderbill;
33:}

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.