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

正确使用类实例

  •  1
  • Smandoli  · 技术社区  · 16 年前

    我正在使用Access 2003 VBA处理制造BOM的递归数据。我构建了一个类模块,让我的主表具有静态作用域。这似乎简化了递归——它让我在遍历BOM时免于反复计算级别。我不会冗余地打开同一个记录集;我过滤了很多。

    在开始这项工作后,我阅读了《对象关系建模》,并决定不实例化记录集,而是实例化记录。然后,该记录的字段可以是属性。经过大量的工作和许多胜利的刺激,我意识到这种方法没有任何好处,因为Access是基于表的。

    有什么理由选择其中一个而不是另一个吗?此外,请注意末尾的斜体行:如果我不需要(因为我不需要更多关于父级的信息),这会改变答案吗?有人能帮我思考一下吗?

        VERSION 1 
        Property Sub ReviewPart ( Parent_Part_ID )
    
        Get Parent_Part_ID
        Create instance of Class --> Get recordset
        Filter Class recordset ( Parent_Part_ID )
        Exploit Class recordset
        See if Parent_Part_ID has Childs
        If it does:
           Open recordset of Childs
           For each Child
             Get Child_Part_ID
             Create instance of Class --> Get recordset
             Filter Class recordset ( Child_Part_ID )
             Exploit Class recordset
             See if Child_Part_ID has Childs
             If it does:  
               Instance New ReviewPart ( Child_Part_ID )
             Otherwise:
             Nothing; Move On
           Next Child
           Close recordset of Childs
        Otherwise:
           Move On
        Exploit Class recordset ( still points to parent )
     
    VERSION 2 Property Sub ReviewPart ( Parent_Part_ID ) Get Parent_Part_ID Create instance of Class --> Get recordset Filter Class recordset ( Parent_Part_ID ) Exploit Class recordset See if Parent_Part_ID has Childs If it does: Open recordset of Childs For each Child Get Child_Part_ID Create instance of Class --> Get recordset Filter Class recordset ( Child_Part_ID ) Exploit Class recordset See if Child_Part_ID has Childs If it does: Instance New ReviewPart ( Child_Part_ID ) Otherwise: Nothing; Move On Next Child Close recordset of Childs Otherwise: Move On Filter Class recordset ( Parent_Part_ID ) Exploit Class recordset ( still points to parent )
    2 回复  |  直到 16 年前
        1
  •  1
  •   Sean McMains    16 年前

    从程序员的角度来看,前者似乎让你更容易使用,因为人们可以简单地从你感兴趣的记录开始,只需访问你开始的记录的属性,就可以轻松地根入相关记录。

    另一方面,后者似乎更有效,因为它不会悲观地加载可能与当前记录相关的每条记录。

    • 仅在需要时加载子记录。使用获取/设置访问器将允许您及时加载这些内容,而不是在从数据库中提取父记录时一次性加载所有内容。
    • 或者,使用JOIN作为单个查询的一部分一次检索所有子数据。这仍然会为您预加载所有数据,但会大大减少您必须运行的查询数量。

    希望这能有所帮助。

        2
  •  1
  •   onedaywhen    16 年前

    您是否考虑过使用msdatashape提供程序和SHAPE语法来生成 hierarchical ADO recordsets ?