Implement data access
- 10/11/2018
- Skill 4.1: Perform I/O operations
- Skill 4.2: Consume data
- Skill 4.3: Query and manipulate data and objects by using LINQ
- Skill 4.4: Serialize and deserialize data by using binary serialization, custom serialization, XML Serializer, JSON Serializer, and Data Contract Serializer
- Skill 4.5: Store data in and retrieve data from collections
- Thought experiments
- Thought experiment answers
- Chapter summary
Thought experiments
In these thought experiments, demonstrate your skills and knowledge of the topics covered in this chapter. You can find the answers to these thought experiments in the next section.
1 Perform I/O operations
Class hierarchies are an important element in the design of the data management mechanisms provided by .NET. The Stream class sets out the fundamental data movement commands and is used as the basis of classes that provide data storage on a variety of platforms. Streams can also be chained together so that character encoding, encryption and compression can be performed on data being moved to or from any stream-based store.
Here are some questions to consider:
Why can’t a program create an instance of the Stream type?
When would a program modify the file pointer in a file?
What is the difference between UT8 Unicode and UTF32 Unicode?
Is there such a thing as a “text file?”
Can a program both read from and write to a single file?
What is the difference between a TextWriter and a StreamWriter?
Why is it useful for a stream class to have a constructor that accepts an object of the stream type?
What is the File class for?
Will you need to re-compile a program for it to be used on a system with a different file system?
What happens to the files in a directory when you delete the directory?
How can you tell if a file path is an absolute path to the file?
Why should you use the Path class to construct file paths in your programs?
What is the difference between HTTP and HTML?
When should you use the HttpWebRequest class to contact a web server?
Can a program make web requests asynchronously?
Why is it a good idea to perform file operations asynchronously?
2 Consume data
Databases are the storage resource that underpins many applications. In this section you’ve seen how a program can use Structured Query Language (SQL) to interact with a database. You’ve also discovered more about the use of databases in Active Server Pages (ASP) and considered how JavaScript Object Notation (JSON) and eXtensible Markup Language (XML) documents allow programs to exchange structured data. Finally, you’ve taken a look at web services; a means by which a server can expose resources in the form of method calls.
Here are some questions to consider:
Does each user of a database need their own copy of the data?
What would happen if two users of a database updated the same record at the same time?
What would happen if two items in a database had the same ID value?
Why do you need to protect your database connection string?
Do you always have to write SQL queries to interact with a database?
What is an SQL injection attack, and how do you protect against one?
You can see how to read JSON data from a server, but is it possible to store JSON formatted data values on a server?
What is the difference between a web service and just downloading JSON document from a server?
3 Query and manipulate data and objects by using LINQ
LINQ allows developers to take their SQL skills in building queries and apply them to software objects. By exposing query elements as C# operators using the query comprehension syntax LINQ allows you to create C# statements that closely resemble SQL queries. These queries can be applied to structured data from a database, but they can also be applied to normal collections and XML documents. LINQ is useful for filtering data and extracting subsets; it also provides the group operator that can be used to summarize data. LINQ queries are actually implemented as method calls onto objects and can be created that way if preferred.
Here are some questions to consider:
What does “Language INtegrated Query” actually mean?
Does LINQ add new features for data manipulation?
Is it more efficient to build and perform our own SQL queries than to use LINQ?
Why would the statement var counter; cause a compilation error?
What is an anonymous type?
What does “deferred execution” of a LINQ query mean?
What does the group behavior do in a LINQ query?
What do the take and skip behaviors do?
What is the difference between query comprehension and method-based LINQ queries?
What is the difference between the XDocument and XElement types?
What is the difference between the XmlDocument and XDocument types?
4 Serialize and deserialize data
Serialization is very useful when you want to store or transfer structured data. It is susceptible to problems if the content or arrangement of data items that have been serialized change in later versions of an application. But, as you have seen, it is possible for these to be addressed with sensible data design.
Here are some questions to consider:
When should you use serialization in your program?
Do you need a copy of the type to read a serialized object?
Can any data type be serialized?
Can value types and reference types be stored using serialization?
Does serialization store all of the elements of a class?
When should you use binary serialization and when should you use XML serialization?
Why should you be concerned about security when using binary serialization?
Can you use a custom serializer when serializing to an XML document?
Is it possible to encrypt a serialized class?
What is the difference between an XML serializer and a DataContract serializer?
5 Store data in and retrieve data from collections
You can think of the collection classes provided by .NET as a set of tools. Each tool is suited for a particular situation. When considering how to store data in a program it is important to consider the whole range of different kinds of collections. Most of the time storage demands tend to be met by the List or Dictionary types, but there have been occasions where you need the FIFO behavior provided by a queue, and a set can save a lot of work because of the behaviors that it provides. There are two things that are important when dealing with collections. The first is that extending a parent collection type is a great way to add custom behaviors. The second is that you should remember that it is possible to perform LINQ queries on collections, which can save you a lot of work writing code to search through them and select items.
Here are some questions to consider:
When should you use a collection, and when should you use a database?
Can you create an array of arrays?
Can you create a twenty-dimensional array?
Why does the program keep crashing with an array index error?
The array is one element too small. How do you add a new element on the end of the array?
Why do you use Length to get the length of an array and Count to get the number of items in an ArrayList?
How does a Dictionary actually work?
When would you use a set?
What is the difference between a stack and a queue?
Could you use a List to store every type of data in my program?