What is the best design in this scenario?

Hi,

Can somebody with experience tell me what would be the best way to accomplish following in c#.

I have a requirement to convert Access table to flat file, I have specification for text file like that

Field

Field

Type

Length

Format

Note

13

Province or State

string

2

Free format

Length of field for Canada =2, US=3, International =20

8

Company Name

string

44

Free format

19

Weight

long

9

Grams, integer

I need to make sure that converted CSV file follows rules. What is the best way to accomplish that Enumaration Class Just procedure to go check for all conditions I need this conversion to work differently depending on destination country (as you can see in the notes). Does it mean I need to create base class and derive from it with overriding some base class methods

I want this to be done in good design manner rather then sloppy code I would probably write with a lot of "if (){}else{}" statements

Really simple example would be greatly appreciated



Answer this question

What is the best design in this scenario?

  • cRz

    The most practical would be to create a class with all those fields in it, a constructor which has all those fields as parameters. This way you can make a list of these objects pretty fast.

    As for the validation of the values can be done in the property setters. This is the easiest approach if you don't have too much to validate (only one or two fields in one class). If you would have more fields but still that one class, it might be beneficial to create a method that validates all the fields at once. If on the other hand you have many classes which need their instances validated, a generic validation framework might be the best thing (using attributes).

    As for the saving to CSV. Depending on the format of the CSV, you could make a method in your dataholder class that generates a CSV string for that record. Then itterate through all objects in a list concatenating all CSV strings for all the records (using StringBuilder). If for some reason this can't be done (strange CSV format), you can make a static method in your dataholder class or a helper class wich writes the entire list to a CSV file in the appropriate format.

    I hope this helps...


  • What is the best design in this scenario?