CodeSmith Community
Your Code. Your Way. Faster!

Nested Objects Best Practice

Latest post 12-06-2007 8:58 PM by shailensukul. 2 replies.
  • 06-11-2007 11:30 PM

    • shailensukul
    • Top 500 Contributor
    • Joined on 10-30-2006
    • Melbourne, Australia
    • Posts 11
    • Points 140

    Nested Objects Best Practice

    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

    Now 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

    With Regards Shailen Sukul (Software Architect/Developer) BSc Mcpd Mcts (Web, Win, Dis Apps) Mcsd.Net Mcsd Mcad http://www.ashlen.com.au
    • Post Points: 35
  • 07-09-2007 7:07 AM In reply to

    • rdrunner6
    • Not Ranked
    • Joined on 07-09-2007
    • Posts 1
    • Points 35

    Re: Nested Objects Best Practice

    Hello...

     I think you are doing something wrong here...

    If you have a Tree of Root-Childs-Child-GChilds-GChild, then you should not load all "Grandchilds" into the first child object. The problem with a level-3 or deeper hierarcy (spelling?) is that it cannot be populated propperly with a datareader.

    You can fill the object hierarcy quite easy with either a temporary dataset, or with DTO's.

     If you populate the dataset with all 3 tabels at once, and define the correct relations between them you can just pass the current datarow from the parent to the child. The childcollection can then call the getChildRows from the row it retrieved and populate itself by passing each child it creates a child-datarow the child can then popuate the grandchild collection by passing in the row that was used to populate the child. Inside the grandchild-collection you can loop over the getChildRows of the child row, which will give you your grandchild rows, for that speciffic child.

    So and since English is only my 2nd language, ill point you to a webcast explaining this, and some other options you have, for populationg your BO's

     http://www.dnrtv.com/default.aspx?showID=60

     

    Hope this helps...

     

    P.s.: Yes, that means you have to work on your data access code...But since you have a code generator, I suggest the DTO strategy ;)

    Filed under: , ,
    • Post Points: 35
  • 12-06-2007 8:58 PM In reply to

    • shailensukul
    • Top 500 Contributor
    • Joined on 10-30-2006
    • Melbourne, Australia
    • Posts 11
    • Points 140

    Re: Nested Objects Best Practice

    Thanks for the reply.

    After much thought, I have decided to abondon the Parent -> Child -> Grand Child hiearachy.

    Instead, 90% of my objects now EditableSwitchable and I only use 1 or 2 tier objects only.

    I have delivered several successful projects using this strategy and am now after some templates that can

    build DTO objects, DAL, and optionally have a pluggable DAL provider for a concrete DAL (using MS Enterprise Library DAAB maybe?).

    I want to concentrate on building the higher level BLL and UI only.

    Having the DAL and DTO auto-generated is also great for refactoring as my database model undergoes quite rapid change in the first few weeks of development and then it stabilizes.

    To have a template like that would shave close to 40-60% of my project effort!

     

    With Regards Shailen Sukul (Software Architect/Developer) BSc Mcpd Mcts (Web, Win, Dis Apps) Mcsd.Net Mcsd Mcad http://www.ashlen.com.au
    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems