span.sup { vertical-align:text-top; }

Toolbox

Serializing objects, Scott Allen's blog, Site Performance, and more

Scott Mitchell

Contents

Serialize and Deserialize Fixed-Length and Delimited Files
Blogs of Note
Build Faster Web Pages with YSlow and Firebug
From the Bookshelf

Serialize and Deserialize Fixed-Length and Delimited Files

I was recently working on a project where we needed to import several thousand records stored in a comma-delimited file into our Microsoft® .NET Framework application. Not only did our application need to import this data, but it also needed the ability to export data in the same format for use by external programs that still rely on the data in a delimited format.

If you've ever used the .NET Framework built-in XML or binary serialization capabilities, you know how easy it is to save an object's state to disk as an XML or binary file, as well as how easy it is to reload the object from disk back into memory. In a nutshell, you decorate a class with attributes, indicating how the class's properties are to be serialized. Once the class has been decorated, serializing or deserializing an object requires just one line of code.

While the .NET Framework includes serialization formatter classes for serializing objects to XML and binary formats, it does not include formatters for working with comma-delimited files. The good news is that serializing objects to fixed-length and delimited text files is as easy as XML and binary serialization thanks to FileHelpers version 2.0 by Marcos Meli.

To use FileHelpers to serialize an array of class instances to a fixed-length or delimited text file, start by decorating the class's properties with the applicable attributes. When serializing to fixed-length files, there is an attribute to indicate the length of each field; when serializing to delimited files, there's an attribute to indicate the delimiter.

There are also attributes that you can use to indicate the data type and format of the data, whether white space should be trimmed, and whether the field is optional. An appropriately decorated class is referred to as a record class. (In addition to using attributes to specify serialization specifics, FileHelpers also includes an API for programmatically defining the serialization and deserialization rules at run time.)

As you may know, creating the record class can be a tedious process. To help streamline the process and reduce the chance of a syntax error or typo, FileHelpers ships with a Record Class Wizard program. The wizard is a standalone GUI application that prompts for the precise fields in the fixed-length or delimited file and builds the corresponding record class with the appropriate properties and attributes.

As with XML and binary serialization in .NET, once you've created a record class—either by decorating its properties with attributes or by programmatically defining the record class—persisting a collection of such objects to disk or populating a collection of such objects from an existing text file is as easy as calling the library's WriteFile or ReadFile methods.

As the FileHelpers library serializes or deserializes the object or text file, it raises an event immediately before and after each record is processed. These events give you the opportunity to inspect or modify the data or cancel the operation prior to each read or write. FileHelpers includes asynchronous versions of the WriteFile and ReadFile methods, which are useful when working with especially large amounts of data. There's also a file transform engine that assists in transforming the structure of a fixed-length or delimited file.

In addition to classes for serializing the record class to disk, there are also classes for reading and writing records to and from Microsoft Access®, SQL Server®, and Microsoft Excel® data stores. Using these classes requires a bit more code than when serializing to disk. For example, when importing a record class to a SQL Server database you must specify the server's connection information and provide the INSERT statement and values. However, using these classes can shave off a lot of boilerplate data access code.

Price: Free, open source.

filehelpers.com

filehelpers.gif

Serialize and Deserialize Fixed-Length and Delimited Files

Blogs of Note

A common challenge developers face when building data-driven apps centers around database versioning. After the app's initial deployment, and as new feature enhancements are being added, developers will need to make changes to the data model. What's the best way to implement these changes and propagate them through the development, testing, and deploying phases? If you're looking for guidance to project-management questions such as this one, check out the blog by author, speaker, and consultant Scott Allen.

The advice Scott dispenses comes directly from the trenches. Whether it's an entry about unit testing, shrink wrapping an ASP.NET Web application, or getting the most out of Visual Studio®, Scott shares his experiences, what he tried, what worked, what didn't, and traps to avoid. Alongside architecture and project management-focused blog entries, you'll find more nitty-gritty technical posts about LINQ, JavaScript and JavaScript Object Notation (JSON), Windows® Workflow, and Silverlight™.

My favorite aspect of Scott's blog is his ongoing "What's Wrong With This Code" series. In each installment, Scott shares a snippet of code with some faulty logic and invites his readers to try to find and fix the problem. It's a great way to hone your debugging and general problem-solving skills, as well as a way to see the techniques and thought processes used by other developers as they tackle the same problem.

odetocode.com/blogs/scott

scottallenblog.gif

Hone Your Debugging Skills and See Other Techniques on Scott Allen's Blog

Build Faster Web Pages with YSlow and Firebug

In the March 2008 Toolbox column I reviewed Firebug, a free, open source browser extension for Mozilla Firefox that displays information about the visited page's HTML, CSS, JavaScript, and images. Firebug makes it easy for a Web developer to visually inspect and modify the various elements of a Web page. It also displays the size and time required to download any external resources such as images or JavaScript files.

For a more detailed analysis of your page's load time, check out YSlow version 0.9.5, a Mozilla Firefox browser add-on created by Yahoo! that integrates with Firebug. Once installed, YSlow appears as a tab in the Firebug user interface. From the YSlow tab you can view detailed information about the external resources (just as you can from Firebug), statistical information about the page's components, and tips for improving the page's performance.

YSlow's Stats view breaks down the bandwidth consumed by the page's HTML, IFrame content, Flash content, JavaScript files, CSS files, and images, and sums the total size and total number of HTTP requests needed. Furthermore, it details this breakdown when the data has been cached by the browser. The cookies sent to the page, along with the sizes and contents, also are enumerated here.

The most indispensable feature of YSlow is the Performance view, which lists 13 performance tips, such as "Make fewer HTTP requests" and "gzip components." The loaded Web page is evaluated against these 13 metrics and assigned a letter grade for each. If there are specific changes that can improve the page's performance, YSlow lists them here. You can click any of these tips to be whisked to a page with a more detailed explanation of the impact on performance and information on how to implement the performance enhancement.

In addition to its Stats and Performance views, YSlow includes a number of helpful tools. From the YSlow menu you can run the page's JavaScript through Douglas Crockford's JSLint program, which parses the script for common problems. There are also tools to view the page's entire JavaScript or CSS content on a single page, regardless of how many external files into which the JavaScript and CSS are split. And there's a tool for displaying YSlow's Performance, Stats, and external resources views in a single, formatted Web page, which makes it easy to print or e-mail the results.

Price: Free.

YSlow: developer.yahoo.com/yslow

Firebug: getfirebug.com

yslow2.gif

YSlow's Detailed Analysis of Web Pages

From the Bookshelf

As a freelance developer and consultant, I work with clients who have existing apps but need help in adding new features. Some applications are written in C#, others in Visual Basic®, so I need to have a mastery of both languages. Visual Basic is easy to pick up; its prose-like syntax and straightforward semantics have helped make it one of the most popular programming languages in the world. But there's a wide gulf between knowing Visual Basic and mastering it.

To help bolster my Visual Basic skills, I picked up Accelerated VB 2008 (Apress, 2008) by Guy Fouché and Trey Nash. This book dispenses with the typical page-filling content found in most computer trade books and replaces it with distilled information and code examples that get right to the point. Because of this terse style, however, Accelerated VB 2008 is not for beginners who are interested in learning Visual Basic, but it's ideal for intermediate developers who are either looking to bolster their understanding of Visual Basic or who are in need of a good desk reference.

The first several chapters focus on core Visual Basic language concepts, including types and typing rules; classes, methods, properties, and fields; interfaces; delegates and events; and exception handling. Subsequent chapters look at working with higher-level constructs, such as strings, arrays, collections, and generics. There's also chapters on threading and LINQ.

I especially liked the Visual Basic 2008 Best Practices chapter, which offers guidance for developers creating new classes. The chapter includes discussions on designing classes that can be cloned, converted to other types, and ordered.

If you' are an intermediate developer looking to hone your Visual Basic skills, or are in need of a Visual Basic language desk reference, pick up Accelerated VB 2008.

Price: $39.99.

apress.com

fouche_acceleratedvb.gif

Send your questions and comments for Scott to toolsmm@microsoft.com.

Scott Mitchell, author of numerous books and founder of 4GuysFromRolla.com, is an MVP who has been working with Microsoft Web technologies since 1998. Scott is an independent consultant, trainer, and writer. Reach him at Mitchell@4guysfromrolla.com or via his blog at ScottOnWriting.NET.