FileHelpers Library - Append Multiple Records on Transform

932 Views Asked by At

Using the FileHelpers library to do some great things in VB.NET. Parsing files with dynamic classes built from text file templates. One thing I can't find: A way to read in a single record and determine that it should result in the generation of two records.
Current Code:

Dim FromType As Type = Dynamic.ClassBuilder.ClassFromSourceFile(MyFilePath, MyDynamicTypeName, NetLanguage.VbNet)

Dim FromRecords() As Object FromRecords = FileHelpers.CommonEngine.ReadString(FromType, MyStringBuilder.ToString)

'... maybe code here to check for certain values

Dim engine As New FileTransformEngine(Of ITransformable(Of MyDestinationClass), MyDestinationClass)

' Ideally in this next line I would like it to see certain conditions and be able to generate two records from a single source line. Dim PayRecords() As Object = engine.TransformRecords(FromRecords)

Alternately, if there is a way to implement the "ITransformable(Of ..." TransformTo() and have it return multiple records, I could put the logic in the dynamic class definition TransformTo() method.

Thoughts?

Here is a sample of my source dynamic class:

Imports FileHelpers ' Never forget

_ Public NotInheritable Class MyDynamicClass Implements ITransformable(Of MyDestinationClass) _ Public Name As String

<FieldQuoted(""""c, QuoteMode.OptionalForRead, MultilineMode.AllowForRead)> _
Public KeyType As String

Public Hours As Double

Public Function TransformTo() As MyDestinationClass Implements ITransformable(Of MyDestinationClass).TransformTo Dim res As New MyDestinationClass

    res.ContactName = Name
    ' Here is where I would like to say... instead of Return res
    If KeyType="ABCD" Then
        Dim newRes as New MyDestinationClass
        newRes.Contactname = Name + " 2nd contact"

        Dim resArray() as MyDestinationClass
        redim resArray(1)
        resArray(0) = res
        resArray(1) = newRes
    End If
    Return resArray
    ' Or alternately refer to the engine, but it is not in scope for the dynamic record (is it?). Something like...
    engine.AppendToDestination(new MyDestinationClass(...))

End Function End Class

1

There are 1 best solutions below

0
On

I think the main problem you're going to run into is that ITransformable.TransformTo() is spec'ed to return a single T value.

This is called in a loop when you call engine.TransformRecords(), where one output record is added for each input record.

I don't think this would be a big change if you didn't mind doing your own version of FileTransformEngine, but I don't see a clean way to do this as-is.