This way you may choose to have the code within the console application to stay on your app side code (ASP.NET or Windows forms etc.,)Īll the other stuff could be placed on a service, which has public method (of course you got to add Webmethod attribs) to be triggered by the clients. Please refer to the code snippet I have put in for your reference.ġ) MyFile is the File object (product) being handled.Ģ) MyFileProcessor will return the FileProcessor to IFileProcessor (Delimeted or a FixedWidth) based on the File Format you choose (on the UI?)ģ) IFileProcessor.ProcessFile() will process and return the data (as a DataTable - assumed for simplicity). This way your app becomes more resilient to change requirements and will add extensibility as well. In case if the File format changes for Field Names, length, lookup formatting etc., all that you need to do is to change the XML and not the code. Processing a Delimited File is fairly straight forward and processing Fixed Width needs extra effort.įor Fixed Width I would recommend to have a xml based File Schema, which could be loaded and parsed against the data file. įile formats could normally vary like Delimited and Fixed Width. Hi for your scenario, I could think of Factory pattern. If anyone is interested, feel free to ping me and you can send email to tony at blognstuff dot comĪlways interested in someone looking at my solution and showing me a better way! In my case if I could not convert some piece of data I would throw an exception and then I could figure out why and handle that, then just rerun the process. Once you have the data for the field then you could easily set the value of the objects field I also had to add code in this method to check for field types like Time or Date or both also for handling boolean conversion of data and decimal types) (here is where I ran into coding trouble - I had a huge switch based on RecordType and a method to extract the data based on record types. Get the specifications for this row - in my case the first 3 characters of each row are the row type identifierĬreate Collection to store decomposed objects (ie whatever each row holds the data for)įor each specification in row specifications determine the specifications required to decompose this file It gets trickier when you have to know what the previous line was in order to apply the correct specification to the current line. Using this data I can create a collection of detail specifications that defines a row, and either parse it or build it against that specification. The detail rows contain the following fields, Id, RecordTypeId, FieldName, StartPosition, FieldLength, Description, IsFieldPadded, PaddingCharacter, FieldFormattingString, PaddingDirection, HasConstantValue, ConstantValue, DataTypeId. This allows me to define any specification including versions of the specifications. I created a few tables in a database to store the data for the specifications of the file. Its been a while and I developed my own solution. The other trick is that I use the field name to match the object field name to reflect back and set the field formatting and the field data, so I have to make sure those items match or decorate them somehow or build a mapping of some sort.Īnyway, any ideas would be appreciated! Also, I need to go both ways, in other words, I need to use the rowspecs to build a file or to parse the file. Does anyone have a good suggestion for how to apply a design pattern for this type of situation? Multiple patterns? I have coded it to make it work, but it seems very clunky and riddled with possible errors. Some times there is some conversion of the data, or a lookup that needs to occur on a piece of data, and sometimes there are some special rules like formatting etc. I iterate through the specific specs for that row and assign the data to that field in the appropriate element. I open the file and I read each row and based on the first three characters I pull the specifications for that row. I have created a data table that contains the specifications, and I am able to get the specifications into an object List no problem. Say the first row specification level 100 a header row and all 200s are data rows and level 199 is a file summary. I have a flat file where each row has a specification.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |