代码之家  ›  专栏  ›  技术社区  ›  Amitd

为项目添加资源的设计

  •  2
  • Amitd  · 技术社区  · 16 年前

    项目 , 资源 和 文件 .

    A .
    资源 包含的列表 文件夹 特殊类型的。

    <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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Amichai    16 年前

    我理解的问题是:

    到现在为止,你的 AddFileHelper 仅将文件添加到项目资源中 ''resource1'' ''resource2'' )再加上它。

    有一个非常简单的方法可以做到这一点。内 添加文件帮助程序 FileType 并确定是否需要将新资源添加到项目中。如果类型不是,则调用现有代码并添加文件:

    res.AddFile(path, type, id);
    

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

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

    resourceName 是字符串:

    string resourceName = ''resource'' + resourceCounter;
    

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

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

    这个 AddFile 方法,以及 这个方法听起来很相似,但做了两件截然不同的事情。这个 增加文件 方法属于资源类,因为它作用于定义良好的资源对象。然而 增加文件 添加文件帮助程序 方法是确定哪个资源将调用 方法。因此 添加文件帮助程序 方法属于项目类和 增加文件 方法创建资源类。

    添加文件帮助程序 如果将方法重命名为 FileResourceAssignment 或者类似的。

    推荐文章