Home  /  Questions  /  Question



275   99.7
Jul 29, 2010


Design Pattern for importing data

Which Design Pattern I can use to implement the import of data in different formats, such as, XML, CSV, TXT, XLSX etc...



1,364   100.0
Jul 31, 2010
This can be done with a combination of various patterns.  First try to classify what varies in the application.  For e.g. the data-source (it could be xml, csv, excel) anything.  Abstract those variation.   Just to cite an e.g. you can use a "factory" for controlling object creation and a strategy for separating out the parsing of different file stores(or may be a provider pattern can play a role here).  Other patterns may as well be adopted.

To see a live example in action visit http://www.codeproject.com/KB/database/filehelpers.aspx.   Though this article doesn't mention directly anything about design patterns but the code is very well organized and may be a good learning as well.

555   99.9
Jul 30, 2010
First of all, I believe you should ask the other way around: What exactly do I want to achieve? Whether or not there are design patterns that can help you to prevent common problems to arise will be the next question.

For example:
As I understand you have data in different formats available but a common way to work with it in your application (e.g. a DataTable with a given structure). You already know that you don't have only a txt file with a specific format but also xml, csv and other files. At some point you may also have even more file formats or other source structures.

  • This may result in a complex subsystem that is responsible for handling different file formats (like reading, parsing, error handling, transforming, etc.). A Facade might help you to "hide" that complexity from your caller.
  • In case you work with some sort of dependency injection, a Factory might be handy for instantiating specific classes for handling different file formats.
  • The Builder pattern could help you in creating a system for parsing the data returning them in a way you need it.
  • Since you might end up working with "incompatible" interface (needed for each format) but need to work with something common (visible to your caller), you can use the Adapter pattern as Microsoft did e.g. with the SqlDataAdapter.
However, that all depends on what exactly you want to achieve. You should have a look at solutions of others and try getting the whole picture. Also, this entry at StackOverflow might help. One of the replies actually links back to dofactory. ;-)

Regards,
-Chris
 1 comment
 
Thanks alot Jacob I am already going with Factory pattern but need to know how to implement builder pattern on parallel of factory pattern. --- Imran Khan  Jul 30, 2010