What is DataContract and DataMember in WCF
DataContract - The flat rate for serializing your objects
In .NET there are many ways to write an object to a file and later load it from it. The data contract offers a simple approach. This has nothing to do with cellular communications, but is a concept introduced by the Windows Communication Foundation (WCF).
So that you can save objects of your class to a file in XML or JSON format you only need the two attributes and. In contrast to other serialization technologies, with data contracts you have to explicitly define which attributes and properties are taken into account during serialization.
You say that your class realizes a data contract by defining the attribute on the class. This attribute as well as other relevant attributes and classes are defined in the namespace. A simple example could look like this:
As you can see, you are using the attribute on all properties and attributes that should be taken into account during serialization. For example, if you have a property on a class that only contains current values from a device or a web service, you don't necessarily want to save it.
You can further influence the serialization via the properties of the attribute. If you do not specify anything else, as with the attribute in the previous example, the serialization uses the name of the attribute within the generated file. If you now want to have a name in the generated file that .NET does not allow as a variable name, you can specify this via.
This also allows you to decouple your class interface from the names in the file. If you want to rename the attribute on the class in the example above, you can do this in the source code without having problems reading an existing file.
The property is also used for versioning. With this you indicate that there will be an error during the deserialization if the corresponding entry does not exist in the file.
Now you already know the most important attributes to define a data contract for your class. Objects of such a class can be written and read with the. You will find out how this works in another LernMoment. In order not to tease you, I have already included it in the example.
First of all, have fun with your flat rate for saving and loading objects
PS: Even if there are still small gaps, Mono supports this functionality almost completely.
- With data contracts () you can effortlessly save objects of your class in different formats and read them in again.
- The required attributes and classes are defined in the namespace.
- is used in the class definition.
- With you define which properties and attributes of your class should be saved and read.
- In particular, you can use the properties and the attribute to influence the versioning of your file format.
Use the following questions to consolidate what you have learned today:
- What do you use the attribute for?
- What is written to a file if a class is defined but none?
- When should you use the property of the attribute?
The best thing to do is look at the previous questions tomorrow and then again in a few days and answer them without having read the text beforehand.
- You can find the complete source text for today's learning moment here.
- An introduction to data contracts can be found at MSDN. However, this is very much designed for WCF. It is less about simply saving and loading objects.
Did you like this learning moment?
Then register and you will be informed about new learning moments:
- Grandparents have favorites among their grandchildren
- Are solutions heterogeneous mixtures
- How is life in an IT company
- What is religion in things falling apart
- Have Chavvy beer
- Who are the most skilled poker professionals
- Why is makeup so ugly for women
- Can blood transfusions also confer immunities
- Is Anakin Skywalker Darth Vader
- Is Text Broker Legit?
- Who designed the Desk com website
- What are some of the landforms of New Brunswick
- Why are Google's margins lower than Microsoft's
- Why is HPCL and not ONGC
- What is the formula of mercury
- What problems does Mauritania face?
- Where is the world's largest bookstore
- Which element has the ordinal number 4
- Is LDH a marker in cancer
- Why is there no quaternary hydrogen?
- Can a doctor tell when you are lying
- What are the origins of bed bugs
- What is a good e-bike motor kit
- City life feels depressed
- Is direct democracy possible in the USA?
- Can I complete btech in one year?
- What do I want to eat
- Where are polymers stored in a diaper
- What is the dipole moment of BF3
- Why are hips important to women
- How many people successfully detox from methadone
- Where are golf umbrellas often used?
- Can you contribute to an IRA rollover?
- Is it true that magnetars crack?