正确使用类实例

时间:2009-07-14 16:59:04

标签: ms-access vba class access-vba hierarchical-data

我正在使用Access 2003 VBA来处理制造物料清单的递归数据。我构建了一个类模块,让我的主表具有静态范围。这似乎简化了递归 - 当我遍历BOM时,它让我无需再次向下计数水平。我没有冗余地打开相同的记录集;我过滤了很多。

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

我的班级模块仍然像以前一样有用。我的问题是关于下面的两个替代版本。第一个使用两个实例(Parent,Child)。第二个使用一个,然后重用它。显然,第一个受ORM影响。

有没有理由选择其中一个而不是另一个?另外,请注意最后的斜体线:如果我不需要(因为我不需要有关父级的更多信息),这是否会改变答案?一般来说,任何人都可以帮我解决这个问题吗?

(请注意,我关闭记录集但不关闭类实例。我的理解是VBA实例关闭自己并让他们这样做是公认的做法。我似乎已经将伪代码带到了新的伪级别...目标是清晰,希望它有效。)

    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 个答案:

答案 0 :(得分:1)

似乎前者从程序员的角度为您提供了更好的易用性,因为您可以简单地从您感兴趣的记录开始,并通过访问您开始使用的记录的属性轻松地根据关联记录。< / p>

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

第一种方法的一些潜在优化可能有助于它在保持易用性的同时提高第二种方法的效率:

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

希望有所帮助。

答案 1 :(得分:1)

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

相关问题