【问题标题】:A design for adding resources to a project为项目添加资源的设计
【发布时间】:2010-07-06 13:34:56
【问题描述】:

我有 ProjectResourceFile 类。 在哪里

项目包含资源列表。
每个资源都包含特定类型的文件列表。

这被映射到一个 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


【解决方案1】:

我理解的问题:

到目前为止,您的 AddFileHelper 仅将文件添加到标记为 ''resource1'' 的项目资源中,这是一个问题,因为每次向您的 AddFileHelper 传递文件类型“A”时,您都会为您的项目创建一个新资源(@ 987654324@) 并将其添加到其中。

有一个非常简单的方法可以做到这一点。在AddFileHelper 中测试添加文件的FileType 并确定是否需要将新资源添加到项目中。如果类型不是“A”,您将调用您现在拥有的代码并添加文件:

res.AddFile(path, type, id);

如果要添加的类型是“A”并且您需要一个新资源,只需重新定义 res 并增加一个计数器变量来表示您的项目中有多少资源:

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

其中resourceName 是字符串:

string resourceName = ''resource'' + resourceCounter;

所有这些都应该作为您的 AddFileHelper 方法来实现。

关于代码的整体结构,AddFileHelper 应该是一个项目类方法。原因如下:

AddFile 方法和AddFileHelper 方法听起来很相似,但做的是两件截然不同的事情。 AddFile 方法属于资源类,因为它作用于定义良好的资源对象。但是,AddFile 方法不足以满足您的目的,因为对于拥有文件并希望将其添加到项目中的客户来说,要附加到的资源文件不会立即显而易见。 B在调用AddFile方法之前,需要确定目标资源。 AddFileHelper 方法的工作是确定哪个资源将调用AddFile 方法。因此,AddFileHelper 方法属于项目类,AddFile 方法属于资源类。

如果您将方法重命名为FileResourceAssignment 或类似名称,AddFileHelper 方法和项目类之间的逻辑联系可能会更加明显。

【讨论】:

  • 在哪里实现 AddFileHelper..在 "Project" 类内或外?
  • 我认为 AddFileHelper 应该是一个 Project 类的方法。另一种方法是创建一个 FileFactory 类 (en.wikipedia.org/wiki/Factory_design_pattern) 并让 Project 类实现一个 AddFile 方法,但在您的情况下似乎没有必要。
  • 我也是这么想的,但问题是文件属于资源而不是项目。那么resource.AddFile(file) 不会比Project.AddFileHelper(file) 在逻辑上更正确吗?这是让我感到困惑的部分......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
相关资源
最近更新 更多