2008-12-23 197 views
0

嵌套集合,我有以下3类填充从数据库

Book 
Product 
SpecialOptions 

有许多书籍,并有每本书的许多产品。同样在一个产品中有许多SpecialOptions。有这三个类别中的其他属性,以便每个类都有如下界面

Public Interface IBook 
    Private ProductList() as collection (of Products) 
    Private Somethingelse() as String 
End Interface 

Public Interface IProduct 
    Private SpecialOptionsList() as collection (of SpecialOptions) 
    Private Somethingelse() as String 
End Interface 

Public Interface ISpecialOptions 
    Private SpecialOptionsProperty() as String 
End Interface 

我想创建书籍的集合,其中有每个产品在它之下,并且适用于各个产品我有当我从数据库中提取数据时,需要关联的SpecialOptions。我无法决定哪种方法可以做到这一点。

我有两种方法。我要么从上到下,要么从下往上。意思是,我先从一本书开始,然后填写产品信息,然后为每个产品填写详细信息。或者我可以先获得详细信息,然后将它们添加到适当的产品中,然后再将产品用于书籍。这些都不太吸引人。

另外,由于我在校对时向我自己提出了这个问题,所以我需要捕捉实际关系的结构,因此用不同的结构来重新构建问题是行不通的。

回答

1

有来解决这个问题的几种方法:当创建每个Book对象,加载所有的产品,然后在创建每个产品对象,加载特殊选项

负荷所有的书,然后。这会导致对数据库的大量查询,但很简单,并且可以使DB Sprocs保持简单。 Psudo代码:

foreach bookR in GetBooks 
    Add a new book to the book collection 
    foreach product in GetProductByBook 
     Add a new product to the book's product collection 
     foreach option in GetOptionByProduct 
      Add a new option to the product option collection 

负荷所有的书,本书的所有产品,并在一个存储过程的所有产品的选项,返回3个结果集。然后首先创建所有书籍,然后通过在书籍收藏中找到正确的书籍并将其添加到书籍中来创建您的产品。您的产品也一样。 Psudo代码:

foreach bookR in GetResultSet1 
    Add a new book to the book collection 
foreach productR in GetResultSet2 
    Find the correct book in book collection 
    Add a new product to the book's product collection 
foreach option in GetResultSet3 
    Find the correct book in the book collection 
    Find the correct product in the book's product collection 
    Add a new option to the product option collection 

带回所有的数据在一个结果集(LINQ到SQL确实是这样)。将所有三个表一起加入并返回一个结果集。遍历每一行,检查与该行匹配的书是否存在(如果不创建它),然后检查该书上的产品是否存在(如果不创建它)。 Psudo代码:

results = GetResultSet 
foreach result in results 
    get the book for matching result book id 
    if the book does not exist, create it from result 
    get the product on the book for the matching result product id 
    if the product does not exist, create it from result 
    etc 
+0

我选择了第三个选项。 – 2009-01-06 16:11:39

1

你说

...开始一本书,然后填写 的产品信息,然后 每个这些产品的填写 详细信息...

我会从这本书开始,然后深入研究。这将允许您仅过滤必要的信息。

我希望能回答你的问题。