Implement data access

  • 10/11/2018

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:

  1. Why can’t a program create an instance of the Stream type?

  2. When would a program modify the file pointer in a file?

  3. What is the difference between UT8 Unicode and UTF32 Unicode?

  4. Is there such a thing as a “text file?”

  5. Can a program both read from and write to a single file?

  6. What is the difference between a TextWriter and a StreamWriter?

  7. Why is it useful for a stream class to have a constructor that accepts an object of the stream type?

  8. What is the File class for?

  9. Will you need to re-compile a program for it to be used on a system with a different file system?

  10. What happens to the files in a directory when you delete the directory?

  11. How can you tell if a file path is an absolute path to the file?

  12. Why should you use the Path class to construct file paths in your programs?

  13. What is the difference between HTTP and HTML?

  14. When should you use the HttpWebRequest class to contact a web server?

  15. Can a program make web requests asynchronously?

  16. 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:

  1. Does each user of a database need their own copy of the data?

  2. What would happen if two users of a database updated the same record at the same time?

  3. What would happen if two items in a database had the same ID value?

  4. Why do you need to protect your database connection string?

  5. Do you always have to write SQL queries to interact with a database?

  6. What is an SQL injection attack, and how do you protect against one?

  7. You can see how to read JSON data from a server, but is it possible to store JSON formatted data values on a server?

  8. 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:

  1. What does “Language INtegrated Query” actually mean?

  2. Does LINQ add new features for data manipulation?

  3. Is it more efficient to build and perform our own SQL queries than to use LINQ?

  4. Why would the statement var counter; cause a compilation error?

  5. What is an anonymous type?

  6. What does “deferred execution” of a LINQ query mean?

  7. What does the group behavior do in a LINQ query?

  8. What do the take and skip behaviors do?

  9. What is the difference between query comprehension and method-based LINQ queries?

  10. What is the difference between the XDocument and XElement types?

  11. 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:

  1. When should you use serialization in your program?

  2. Do you need a copy of the type to read a serialized object?

  3. Can any data type be serialized?

  4. Can value types and reference types be stored using serialization?

  5. Does serialization store all of the elements of a class?

  6. When should you use binary serialization and when should you use XML serialization?

  7. Why should you be concerned about security when using binary serialization?

  8. Can you use a custom serializer when serializing to an XML document?

  9. Is it possible to encrypt a serialized class?

  10. 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:

  1. When should you use a collection, and when should you use a database?

  2. Can you create an array of arrays?

  3. Can you create a twenty-dimensional array?

  4. Why does the program keep crashing with an array index error?

  5. The array is one element too small. How do you add a new element on the end of the array?

  6. Why do you use Length to get the length of an array and Count to get the number of items in an ArrayList?

  7. How does a Dictionary actually work?

  8. When would you use a set?

  9. What is the difference between a stack and a queue?

  10. Could you use a List to store every type of data in my program?