我正在努力寻找一个合适的解决方案来生成一个平面文件。
这里有一些我需要注意的标准:文件有一个标题,其中包含以下记录的摘要,可能有多个集合标题记录和多个批次标题记录,其中包含多个不同类型的记录。
批次中的所有记录都有一个必须添加到批次校验和中的校验和。必须将这个添加到收集头校验和中,然后再添加到文件校验和中。此外,文件中的每个条目都有一个计数器值。
所以我的计划是为每条记录创建一个类。但是现在呢?我有记录和“总结记录”,下一步是把它们都整理好,计算总数,然后设置计数器。
我应该从这里开始,我应该把所有的东西都放在一个大的SortedList中吗?如果是这样,我如何知道在哪里添加最新的记录(必须将其添加到代表批次摘要的位置)?
我的第一个想法是这样做:
SortedList<HeaderSummary, SortedList<BatchSummary, SortedList<string, object>>>();但是很难通过HeaderSummaries和BatchSummaries在内部排序列表中添加对象,记住我可能需要创建和添加HeaderSummary / BachtSummary。
有几个不同的ArrayLists,一个用于标题,一个用于批处理,一个用于其余的,这给我在将它们组合到一个平面文件时带来了问题,因为它们的顺序和尚未设置的计数器,同时保持顺序等。
对于这样的平面文件,您有什么聪明的解决方案吗?
发布于 2017-08-01 03:37:17
考虑使用类来表示树结构的级别。
interface iBatch {
public int checksum { get; set; }
}
class BatchSummary {
int batchChecksum;
List<iBatch> records;
public void WriteBatch() {
WriteBatchHeader();
foreach (var record in records)
batch.WriteRecord();
}
public void Add(iBatch rec) {
records.Add(rec); // or however you find the appropriate batch
}
}
class CollectionSummary {
int collectionChecksum;
List<BatchSummary> batches;
public void WriteCollection() {
WriteCollectionHeader();
foreach (var batch in batches)
batch.WriteBatch();
}
public void Add(int WhichBatch, iBatch rec) {
batches[whichBatch].Add(rec); // or however you find the appropriate batch
}
}
class FileSummary {
// ... file summary info
int fileChecksum;
List<CollectionSummary> collections;
public void WriteFile() {
WriteFileHeader();
foreach (var collection in collections)
collection.WriteCollection();
}
public void Add(int whichCollection, int WhichBatch, iBatch rec) {
collections[whichCollection].Add(whichBatch, rec); // or however you find the appropriate collection
}
}当然,您可以使用一个通用的Summary类来使其更加枯燥,即使不一定更加清晰。
https://stackoverflow.com/questions/45412840
复制相似问题