一個設計我有課項目,資源和文件。 其中添加資源,項目
一個項目包含資源列表。
每個資源包含特定類型的文件的列表。
這被映射爲XML:
<Project>
<Resource id=1>
<File id="1" path="" type="A" />
<File id="2" path="" type="B" />
<File id="3" path="" type="B" />
<File id="4" path="" type="B" />
</Resource>
<Resource id=2>
<File id="1" path="" type="A" />
<File id="2" path="" type="B" />
<File id="3" path="" type="B" />
<File id="4" path="" type="B" />
</Resource>
</Project>
所以基本上每個資源都必須有在最型「A」和任意數量的「B」型的文件中的一個文件。文件類型由用戶從他選擇文件並添加到資源的對話框中選擇。
的問題是「A」型的每一個文件,我需要創建一個XML格式的新的資源,因此,新的節點。(這我當前的代碼是無法做到的)
起初,我來了用下面的(廣義爲了簡潔)
Project p =new Project("Untitled project"); //Will happen once per project
Resource res = p.CreateProjectResource("resource1");
//various params to create resource
p.AddResource(res);
//now lets add files to a resource
AddFileHelper(res,"C:\myfile1.bin","A",guid.toString());
AddFileHelper(res,"C:\myfile32.bin","B",guid.toString());
AddFileHelper(res,"C:\myfile56.bin","B",guid.toString());
//The next statement should create a new resource and add the file to
//the new created design
AddFileHelper(res,"C:\myfile4.bin","A",guid.toString()); //
//some helper class :
//Adds a file of type "type" to a resource "res" with file ID as "id"
private AddFileHelper(Resource res,string path,FileType type,string id)
{
// path is user defined file path from OpenFile dialog,
//type is selected from a Dropdown (of Enum values "A","B",...)
//id is GUID
res.AddFile(path,type,id);
//************ OR it could be also written as *******
//ResFile file =new ResFile(path,type,id);
//res.AddFile(file);
//Update XML file here..
}
的主要問題是用戶不創建資源「明確」(除了第一資源)和新資源的創建取決於文件的類型被用戶添加。
也由於這種設計,很難找出給定文件ID的資源。 唯一的方法是使用每個資源類中的文件集合。
任何幫助?
謝謝大家。
這是參考一個問題,我之前post
任何讀取「我的代碼不好,讓它變得更好」的問題通常是一個可怕的問題。這是證明規則的例外。 – Beska 2010-07-06 13:46:28
我推薦刪除「refactor-my-code」標籤,添加一個「設計模式」標籤並重新命名爲「爲項目添加資源的設計」或類似的問題。 – Amichai 2010-07-06 15:04:55
現在編輯...標記+標題 – Amitd 2010-07-06 15:18:14