2008-12-23 198 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

你說

...開始一本書,然後填寫 的產品信息,然後 每個這些產品的填寫 詳細信息...

我會從這本書開始,然後深入研究。這將允許您僅過濾必要的信息。

我希望能回答你的問題。