FlatFile
FlatFile copied to clipboard
Example with multiple fixed record types
You mention attributes in this example but it's not clear how to use it:
// If using attribute mapping, pass an array of record types // rather than layout instances
Could you clarify specifically using delimited attributes what this might look like?
Yes I would like an example as well. The documentation is rather lacking.
I'd like to see this as well. I'm working to leverage this library to parse NACHA files. In their simplest form they are structured as such:
- FileHeaderRow (13 fields)
- BatchHeaderRow (13 fields, different from FileHeader)
- DetailRow (11 fields)
- BatchControlRow (11 fields)
- FileControlRow (8 fields)
It follows that a file can only have one FileHeaderRow and one FileControlRow and they must be the first and last rows. A file can have multiple BatchHeaderRows but after one is added, it must be followed by a BatchControlRow before another BatchHeaderRow can be added. A file can also have multiple DetailRows but they must be nested between a BatchHeaderRow and BatchControlRow.
For simplicity's sake I am okay with assuming that the file to be built will only have one BatchHeaderRow and one BatchControlRow, with the only variable number of rows being the DetailRow. I have defined 5 FixedLengthFile classes that leverage your FixedLengthField attributes to define each class property (this is very nice for handling NACHA file field rules!) but as such, am left needing to instantiate the Engine to handle these 5 different row types. Thanks!
I'll test how this handles having two levels of nesting, but here is how you would instantiate the engine:
VB.NET:
Dim mFactory As FixedLengthFileEngineFactory = New FixedLengthFileEngineFactory()
Dim mRecordTypes As List(Of Type) = New List(Of Type) From {
GetType(NACHAFileHeaderRow),
GetType(NACHABatchHeaderRow),
GetType(NACHADetailRow),
GetType(NACHABatchControlRow),
GetType(NACHAFileControlRow)
}
Dim mNACHAFile = mFactory.GetEngine(mRecordTypes)
C#:
FixedLengthFileEngineFactory mFactory = new FixedLengthFileEngineFactory();
List<Type> mRecordTypes = new List<Type>()
{
typeof(NACHAFileHeaderRow),
typeof(NACHABatchHeaderRow),
typeof(NACHADetailRow),
typeof(NACHABatchControlRow),
typeof(NACHAFileControlRow)
};
var mNACHAFile = mFactory.GetEngine(mRecordTypes);
Updated this to also pass in a function for line type logic and was able to get the Engine; however, attempts to Write yielded a "method or operation is not implemented" response. Unfortunately due to time constraints I'm going to switch to using FileHelpers but I wanted to leave this here in case it helps someone else down the road.
Dim mFactory As FixedLengthFileEngineFactory = New FixedLengthFileEngineFactory()
Dim mRecordTypes As List(Of Type) = New List(Of Type) From {
GetType(NACHAFileHeaderRow),
GetType(NACHABatchHeaderRow),
GetType(NACHADetailRow),
GetType(NACHABatchControlRow),
GetType(NACHAFileControlRow)
}
Dim mLineFunc As Func(Of String, Integer, Type) = Function(line, number)
If String.IsNullOrEmpty(line) OrElse line.Length < 1 Then Return Nothing
Select Case line(0)
Case "1"
Return GetType(NACHAFileHeaderRow)
Case "5"
Return GetType(NACHABatchHeaderRow)
Case "6"
Return GetType(NACHADetailRow)
Case "8"
Return GetType(NACHABatchControlRow)
Case "9"
Return GetType(NACHAFileControlRow)
Case Else
Exit Select
End Select
Return Nothing
End Function
Dim mNACHAFile = mFactory.GetEngine(mRecordTypes, mLineFunc)
...
mNACHAFile.Write(Of NACHAFileHeaderRow)(mMemoryStream, mFileHeaderRows)