Nested Object Best Practice

Nested Object Best Practice

Old forum URL: forums.lhotka.net/forums/t/3023.aspx


shailensukul posted on Monday, June 11, 2007

Hi,

I am creating a group of nested objects that relate to one parent object and am a bit stuck in the process (I am using CSLA.NET CodeSmith templates from the CSLAContrib project).

Ok so I have one root object ROOTOBJECT,

It has 1 child object CHILD1 and a collection of child1 is a member of ROOTOBJECT

ie CHILD1COLLECTION is a member of ROOTOBJECT

The Child objects have a collection of sub-child objects

SUBCHILD1_COLLECTION is CHILD1's collection

Ok ROOTOBJECT has a ROOT_SelectProcedure that selects the root object and then calls CHILD1_SelectByRootId that selects all Child1 objects that have a foreign key to the ROOTID field. The CHILD1_SelectByRootId also calls SUBCHILD1_SelectByChild1ID to get all sub-child objects for Child1.

Ok now in the code for ROOTOBJECT1, it calls FetchObject which gets a datareader, reads its own fields, calls GetnextResultSet and passes the datareader to CHILD1COLLECTION, which loops to read from the datareader and then calls the constructor of the CHILD1 object, passing in the datareader. CHILD1 builds its fields from the datareader and then calls GetNextResultSet to build its CHILD1COLLECTION.

Here is where the problem is because CHILD1COLLECTION is still reading from the datareader in a while loop and as soon as GetNextResultSet is called, the loop breaks and I only get 1 item for CHILD1COLLECTION.

Surely, there must be a better way to do this. I want to work with the CSLA.NET templates and not change its core pattern by much. Does anyone have a best practice on how nested child collections can be fetched?

 

Many Thanks,

Shailen Sukul

 

 

McManus replied on Tuesday, June 12, 2007

Hi Shailen,

Rocky wrote a demo called DeepData that shows an example of loading grandchild objects using a DataReader. The code can be downloaded from http://www.lhotka.net/Article.aspx?area=11&id=65c98c98-f0e0-477d-b45b-7a1e7d5768ee 

Cheers,

Herman

JoeFallon1 replied on Tuesday, June 12, 2007

This topic has been discussed many times.

Try searching the FAQ and the forum for "grandchild".

 

shailensukul replied on Tuesday, June 12, 2007

Thank you McManus and JoeFallon for your replies.

I shall investigate both options and update this thread with my findings.

:)

Shailen

shailensukul replied on Thursday, July 26, 2007

I have decided to create a variant of CSLA Contrib that would allow:

1. Multiple nested levels

2. Use of DAAB

 

See http://shailensukul.blogspot.com/2007/07/start-new-branch-of-cslanet-template.html

 

Thanks

Shailen Sukul

Copyright (c) Marimer LLC