2010-07-06 87 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