Dr. Dobb's Journal May 2004
There's no end to developer "cookbooks" these days, as I discovered when a couple more landed on my desk. The C# Programmer's Cookbook, by Allen Jones, is a hefty tome, coming in at over 600 pages. Its range of topics is broad, covering C# language features and .NET implementation details for all sorts of .NET Framework programs, including Windows Forms (WinForms) and Web Forms (ASP.NET applications).
The book begins by covering general development issues such as creating various types of applications, using code modules, and accessing command-line arguments. The "Working with Data" chapter is next, but doesn't cover exactly what you would expect. The data involved in this chapter is data internal to your programstrings, arrays, and the like. There are also chapters on application domains and reflection, threads and processes, XML processing, Windows Forms, and Web Forms. Notably missing is a chapter on Windows Services, which is actually discussed in two recipes in the "Windows Integration" chapter.
The "Graphics and Multimedia" chapter contains recipes to do everything from perform a screen capture to playing WAV and MP3 files. "Files and Directories" includes recipes on file operations, including moving, deleting, and copying files, calculating the size of a directory and using isolated storage. The chapter on database access is a bit short, although there are fairly complete chapters on networking, XML web services, runtime security, cryptography, and unmanaged code interoperability. Especially useful is a chapter that has recipes showing how to implement commonly used interfaces and patterns. It took me a while to realize how the author was actually disposing of and closing connections in the database access chapter (most examples use the using keyword). In the end, I examined exactly what the code was doing and realized that, in many respects, it was superior to what I commonly do. While I may not go back and rewrite my existing code, I may change the pattern for future database access code I write.
While I generally liked the C# Programmer's Cookbook, I still had some issues with it. For instance, the example of creating stateful member variables was more complex than simply encapsulating member variables in a property, where the getter and setter do all the interactions with ViewStatewith error handling, of course. Although admittedly subtle, other problems include a common pattern that checks to see if an item is still in the cache, then retrieves the item from cache, using it as if it will always still be there. This exposes a subtle bug that may never bite, but if it does, will be difficult to figure out.
The ASP.NET Developer's Cookbook, by Steven Smith and Rob Howard (and members of the ASP Alliance) begins with a chapter on Web Forms basics and includes recipes as simple as displaying a calendar, and as complex as submitting data to a different page. Next are chapters on user controls and custom controls. Controls are the secret to getting ASP.NET to work the way it was designed to, and these chapters contain all the recipes you will need to get started.
It was in the chapter on using the cache where I discovered that examples in this book suffered from the same potential problem as those in the C# Programmer's Cookbook. That is, a value in the cache is checked, but rather than using an instance stored locally as the cache was checked, later code retrieved it from the cache again, presuming it is still available in the cache. In many cases, it is there; but in those cases when it is not, it is difficult to debug.
Since I've been working on some mobile ASP.NET apps of late, I was happy to see a chapter with a few recipes on mobile controls. Likewise, the next chapters on configuration, state management, and security were generally strong, though I was disappointed there wasn't a more complete example of real-world ASP.NET Forms authentication. Many of the questions I see on the newsgroups involve implementing such systems.
XML and traditional database access are related and are, therefore, covered in a number of chapters. I was especially pleased with the coverage of DataReaders and DataSets. I was less happy that the authors specifically mentioned the importance of using error handling when reading from a DataReader, then did not actually include the try/finally pattern in their examples. I find DataReaders amazingly useful, and virtually all problems associated with their use is related to not properly handling or closing them, especially when they are returned from helper functions.
So, is there anything I do not like about the ASP.NET Developer's Cookbook? Well, all the examples are in Visual Basic .NET. True, most of what is being conveyed is related to using the .NET Framework, and even though that is relatively easy to convert from one language to another, some C# programmers might be put off. Also, I would have preferred that both books use more real-world exception handling, understanding, of course, that many examples in books aren't designed to handle errors.
DDJ