2010-07-06 214 views
2

一個設計我有課項目資源文件。 其中添加資源,項目

一個項目包含資源列表。
每個資源包含特定類型的文件的列表。

這被映射爲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

+0

任何讀取「我的代碼不好,讓它變得更好」的問題通常是一個可怕的問題。這是證明規則的例外。 – Beska 2010-07-06 13:46:28

+0

我推薦刪除「refactor-my-code」標籤,添加一個「設計模式」標籤並重新命名爲「爲項目添加資源的設計」或類似的問題。 – Amichai 2010-07-06 15:04:55

+0

現在編輯...標記+標題 – Amitd 2010-07-06 15:18:14

回答

1

的問題問我的理解是:

截至目前,您AddFileHelper僅將文件添加到您的項目資源標記''resource1''這是因爲有問題每當文件類型「A」傳遞給你的AddFileHelper時,你就爲你的項目創建一個新資源(''resource2'')並將其添加到該文件中。

有一個非常簡單的方法來做到這一點。在AddFileHelper內測試添加文件的FileType並確定是否需要將新資源添加到項目中。如果類型不是「A」,你會打電話給你現在的代碼,並添加文件:

res.AddFile(path, type, id); 

如果添加的類型是「A」,你需要一個新的資源,只是重新定義res和增加你有多少資源在項目中的計數器變量:

Resource res = p.CreateProjectResource(resourceName); 
resourceCounter++; 

哪裏resourceName是字符串:

string resourceName = ''resource'' + resourceCounter; 

這一切都應該爲你的A實施ddFileHelper方法。

關於代碼的整體結構,AddFileHelper應該是一個項目類方法。這是爲什麼:

AddFile方法,和AddFileHelper方法聽起來相似,但做了兩件非常不同的事情。 AddFile方法屬於資源類,因爲它作用於定義良好的資源對象。但是,AddFile方法不足以滿足您的需要,因爲要附加到的資源文件對於具有文件並希望將其添加到項目的客戶端來說並不是很明顯。因爲可以調用AddFile方法,所以需要確定目標資源。 AddFileHelper方法的工作是確定哪個資源將調用AddFile方法。因此,AddFileHelper方法屬於資源類的項目類和AddFile方法。

如果將方法重命名爲FileResourceAssignment或類似的東西,AddFileHelper方法和項目類之間的邏輯連接可能會更明顯。

+0

在哪裏實施AddFileHelper..within「Project」類或外部? – Amitd 2010-07-06 16:06:46

+0

我認爲AddFileHelper應該是一個Project類的方法。另一種方法是創建一個FileFactory類(http://en.wikipedia.org/wiki/Factory_design_pattern),並讓Project類實現一個AddFile方法,但在您的情況下似乎並不需要。 – Amichai 2010-07-06 16:40:39

+0

我在想同樣的事情,但文件屬於一個資源比一個項目更多。那麼resource.AddFile(file)在邏輯上是否比Project.AddFileHelper(file)更正確? 這是混淆我的部分.... – Amitd 2010-07-07 05:59:45

相關問題