一个设计我有课项目,资源和文件。 其中添加资源,项目
一个项目包含资源列表。
每个资源包含特定类型的文件的列表。
这被映射为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