How to create a new converter

This is a step-by-step guide how to create a new converter for a new source CSV file. If you are familiar with C# and Visual Studio a new converter can be created in ~15min. So let's start:

(1) Create new folder

Open the YnabCsvConverter solution and go to the YnabCsvConverter.Core project. Add a new folder under Converters for the converter and name it for example MyBank

(2) Create a record

Add a new class and name it MyBankRecord. A record represent a single line of the source CSV file.

For example:
   public class MyBankRecord
   {
      [CsvField(Index = 0)]
      public string Date { get; set; }

      [CsvField(Index = 1)]
      public string Text { get; set; }

      [CsvField(Index = 2)]
      public string Amount { get; set; }
   }

The CsvField Attribute with the index specifies the order of your fields. Just add as many properties to the class as your CSV file contains fields per line. Use string to make it as easy as possible.

(3) Create the converter

Add a new class and name it MyBankConverter. This class shall inherit from BaseConverter, implement IConverter and have a ConverterName attribute. The ConverterName is later automatically used within the source type selection on the user interface and can be any string.

Here is a short version of a converter. For a complete and documented example of this converter look at MyBankConverter Example

   [ConverterName("My good Bank")]
   public class MyBankConverter: BaseConverter, IConverter
   {
      public SimpleCsvConverter()
      {
         SourceCulture = CultureInfo.InvariantCulture;
      }

      public ConverterResult Convert(string source, string target, CreationMode mode)
      {
         IEnumerable<MyBankRecord> records= ReadRecords<MyBankRecord>(source, ',', false);
         IEnumerable<YnabRecord> ynabRecords = ConvertRecords(records);
         WriteRecords(ynabRecords, target, mode);

         return new ConverterResult {IsSuccess = true};
      }

      public IEnumerable<YnabRecord> ConvertRecords(IEnumerable<MyBankRecord> records)
      {
         return records.Select(ConvertRecord).ToList();
      }

      public YnabRecord ConvertRecord(MyBankRecord record)
      {
         ...
      }
   }

(4optional) Add a test file

Add a test file with anonymized data like shown on the Converters page. And name the file Sample.MyBank.csv

(5optional) Write unit tests

It's highly recommended to write unit tests for your converter

(6) Try it out

Start the program! On the user interface the drop-down box shall display the MyBank option.


Any problems? Just feel free to ask questions here or start a new discussion or write me an e-mail.

Last edited Feb 5, 2013 at 9:06 PM by chris32, version 10

Comments

No comments yet.