I am using Filehelpers to import my database from files. I am now working on the reverse process and am having trouble seeing how to create the records from my data objects.
All the examples I can find show going from file -> table -> file I am using interfaces with generics to convert. I use this one on the inbound conversion:
public interface IConvertCSVRecordToType<T> where T : SimpleBase
{
T ConvertCSVRecordToType();
}
and would like to use something like this for the outbound:
public interface IConvertTypeToCSVRecord<T> where T : SimpleBase, new()
{
void ConvertTypeToCSVRecord(T type);
}
I use this class to represent the CSV Records:
[DelimitedRecord(";"), IgnoreEmptyLines]
开发者_StackOverflow社区public class CSVRecordFormat : IConvertCSVRecordToType<Material>,
IConvertTypeToCSVRecord<Material>
I came across TransformToRecordAttribute in the Filehelpers documentation
TransformToRecordAttribute Class
With this attribute you can mark a method in the RecordClass that is the responsable of convert it to the specified.
Does anyone have an example that uses this attribute or an example of how to create a record set to get me in the right direction?
(rough) Code that worked with some details elided:
public class CSVTableExportProvider<TTable, TRecord>
where TTable : SimpleBase, new()
where TRecord : IConvertTypeToCSVRecord<TTable>, new()
{
public void ExportTable(string path, string filename, bool continueOnError)
{
var rows = _service.GetList<TTable>();
var records = new List<TRecord>();
var csvfile = Path.Combine(path, filename);
var csvFile = new CSVFile<TRecord>(csvfile, continueOnError);
foreach (var row in rows)
{
var rec = new TRecord();
rec.ConvertTypeToCSVRecord(row);
records.Add(rec);
}
csvFile.ConvertRecordsToFile(records.ToArray());
}
}
Which I call from my unit test:
var provider = new CSVTableExportProvider<Material, MaterialCSVRecordFormat>();
provider.ExportTable(foldername, filename, true);
精彩评论