2017-07-31 81 views
0

我正在努力尋找一個適當的解決方案來生成一個平面文件。建立一個多記錄平面文件

這裏有一些標準,我需要照顧: 文件有其以下記錄 有可能是其包含不同類型的多個記錄多個批頭記錄多個採集頭記錄總結的標題。

批處理中的所有記錄都具有必須添加到批校驗和的校驗和。這一個必須被添加到集合Header校驗和中,然後再添加到文件校驗和中。此外,文件中的每個條目都有一個計數器值。

所以我的計劃是爲每個記錄創建一個類。但現在呢?我有記錄和「簡要記錄」,下一步就是將它們全部整理好,計算總和,然後設置計數器。

我應該如何從這裏開始,我應該把所有東西放在一個大的SortedList中?如果是這樣,我怎麼知道在哪裏添加最新記錄(它必須添加到其代表批處理摘要中)?

我的第一個想法是做這樣的事情:

SortedList<HeaderSummary, SortedList<BatchSummary, SortedList<string, object>>>(); 

但是,很難通過HeaderSummariesBatchSummaries導航到內部排序列表中添加一個對象,記住,我可能要軸承創建並添加一個HeaderSummary/BachtSummary

有幾個不同的ArrayList,如一個用於Header,一個用於批處理,一個用於剩餘的時候,由於順序和 - 還未設置 - 計數器,將它們合併到平面文件時給我帶來問題,同時保持順序等。

你對這樣一個平面文件有什麼聰明的解決方案嗎?

+0

我不認爲你想要一個'SortedList' - 一個普通的'List'保存順序。既然你基本上有一個樹形結構,我認爲你需要顛倒你的想法,讓類中包含'List'。 – NetMage

回答

0

考慮使用類來表示樹結構的級別。

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類更幹。