Saturday, September 11, 2010

Functional Construction Pattern: Parsing a csv file.

The Functional Construction Pattern is used to construct a tree or graph of objects, with a code structure similar to what is used in functional programming languages. It is also called the Transform Pattern because it's used to create a new object graph based on a given one. Using the functional construction pattern you write code that is a lot more declarative (the structure of the generated object graph is visible in the code). One drawback is that code is not as easy to debug. In order to understand the pattern let's take the example of parsing a csv file into a collection of objects.

Sample csv file

Imperative approach for parsing a csv file

Declarative approach for parsing a csv file

Declarative approach for parsing a csv file, a more concise style

Streaming approach to save resources

When dealling with large files, better stream than read to end in order to reduce the memory used by your application.

Lines extension method

The following extension method (used previously) returns an IEnumerable reader.

0 comments:

Post a Comment

Post a comment