design-patternsfileparse

Design pattern for parsing binary file data and storing in a database


Does anybody recommend a design pattern for taking a binary data file, parsing parts of it into objects and storing the resultant data into a database?

I think a similar pattern could be used for taking an XML or tab-delimited file and parse it into their representative objects.

A common data structure would include:

(Header) (DataElement1) (DataElement1SubData1) (DataElement1SubData2)(DataElement2) (DataElement2SubData1) (DataElement2SubData2) (EOF)

I think a good design would include a way to change out the parsing definition based on the file type or some defined metadata included in the header. So a Factory Pattern would be part of the overall design for the Parser part.


Solution

    1. Write your file parser, using whatever techniques come to mind.
    2. Write lots of unit tests to make sure all your edge cases are covered. Once you've done this, you will actually have a reasonable idea of the problem/solution. Right now you just have theories floating around in your head, most of which will turn out to be misguided.
    3. Refactor mercilessly. Your aim should be to delete about half of your code.

    You'll find that your code at the end will either resemble an existing design pattern, or you'll have created a new one. You'll then be qualified to answer this question :-)