【发布时间】:2010-07-06 13:34:56
【问题描述】:
我有 Project、Resource 和 File 类。 在哪里
项目包含资源列表。
每个资源都包含特定类型的文件列表。
这被映射到一个 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 的资源。 唯一的跟踪方法是使用每个 Resource 类中的文件集合。
有什么帮助吗??
谢谢大家。
这是参考我在post之前提出的一个问题
【问题讨论】:
-
通常,任何“我的代码不好,让它变得更好”的问题都是一个糟糕的问题。这是证明规则的例外。
-
我建议去掉“refactor-my-code”标签,添加一个“design-pattern”标签并重命名问题:“a design for adding resources to a project”或类似的东西。
-
已编辑...标签+title
标签: c# design-patterns