Accessing Children BOs from a Parent BO Programmatically


Author
Message
Leonard P.
Leonard P.
StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
Hello,

I have what seems to be a very simple scenario, but for whatever reason can't get it to work correctly.

I defined parent/child relationship between 2 BOs; 1) setup ParentRelationShip property in BO designer by PK_Id to FK_Id and 2) Dropped 2 objects on the form and set ParentObject property.

I want to loop through parent objects and inner loop children of each parent. But I am getting all "Children" objects for each parent, instead of true children as defined by IDs. What am I missing?

Also is there way to get strongly typed Child object from parent instead of: parentBO.ChildBusinessObjects[0];

here's relevant code:

private void PopulateFactSheets()
{
FillFactSheetsObjects();
if (factSheetCategory.MoveFirst())
{
do
{
TreeNode fsTypeNode = trvFactSheets.Nodes.Add(factSheetCategory.FactSheetType);
FactSheetReport fsR = (FactSheetReport)factSheetCategory.ChildBusinessObjects[0];//can I get to strongly typed child?
if (fsR.MoveFirst())
{
do
{
fsTypeNode.Nodes.Add(fsR.IndexName);
} while (fsR.MoveNext());
}
} while (factSheetCategory.MoveNext());
}
}
private void FillFactSheetsObjects()
{
SqlCommand sqlCmnd = new SqlCommand(SqlCommands.SelectAllFactSheets + ";" + SqlCommands.SelectFactSheetTypes);
BusinessLayer.FillMultipleDataTables(sqlCmnd, factSheetReport, factSheetCategory);
// factSheetReport.ParentBusinessObject = factSheetCategory; //it's set in the designer
}

Thank you for your help.


Leonard P.
Leonard P.
StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
Hi,

Ok, I did get it to work, see codesnippet below.

I just have a few questions regarding that:

1. What's the most efficient way of getting children of each parent. I was hopping there would be a way to Fill all BOs at once and loop through parents and get children on each parent, instead of making multiple ".FillByParentPrimaryKey" calls

2. I tried using .FillByParent(parentBo) method, but it didn't work. How does this method work?

Thank you.

private void PopulateFactSheets()
{
// FillFactSheetsObjects();
factSheetCategory.FillDataTable(SqlCommands.SelectFactSheetTypes);
if (factSheetCategory.MoveFirst())
{
do
{
TreeNode fsTypeNode = trvFactSheets.Nodes.Add(factSheetCategory.FactSheetType);
// FactSheetReport fsR = (FactSheetReport)factSheetCategory.ChildBusinessObjects[0];//can I get to strongly typed child?
FactSheetReport fsR = new FactSheetReport();
fsR.FillByParentPrimaryKey(factSheetCategory.FactSheetTypeId);
if (fsR.MoveFirst())
{
do
{
fsTypeNode.Nodes.Add(fsR.IndexName);
} while (fsR.MoveNext());
}
} while (factSheetCategory.MoveNext());
}
}


Dustin Taylor
Dustin Taylor
StrataFrame Team Member (660 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
Glad you got it going!

1) The most efficient way kind of depends on your setup. In most scenarios, doing what you are doing now and filling the child as needed whenever the parent navigates (by way of FillByParentPrimaryKey) is actually the most efficient. BigGrin

The other way, and I believe this is what you are looking for, is to pull all child records in the first query (preferably by using the FillByParent method) and then filter the view down to only show the child records that belong to the current row of the parent BO. This will be a bigger performance hit up front, but will be much faster from that point forward, and should only require one query. The parent child relationship can actually do the filtering for you via the ChildAutoFilterOption property on the parent business object. Just set the property to CurrentRow on the parent business object and it should filter the view of the child down for you whenever you change the current row of the parent BO.

2) The FillByParent method is used exactly the way you described (call it on the child BO and pass it the parent BO), but there may be some misunderstanding as to what it does. Using FillByParent will fill the child BO with all child records that belong to any parent record currently in the parent BO.  From there you would still need to filter the child BO down to only show those records that apply to the current parent BO (which can, again, be done automatically via the ChildAutoFilterOption property on the parent BO.)  Using FillByParent will omit any child records that do not belong to any parent record currently in the parent BO and is therefore more efficient than just doing a "Select * From ChildTable" to fill the child BO.

Hope it helps Smile


Leonard P.
Leonard P.
StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)StrataFrame Novice (83 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
Thank you. I think using .Filter is exactly what I was looking for.
Alex Luyando
Alex Luyando
StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)
Group: StrataFrame Users
Posts: 112, Visits: 1.2K
Dustin Taylor (08/08/2008)
Glad you got it going!




1) The most efficient way kind of depends on your setup. In most scenarios, doing what you are doing now and filling the child as needed whenever the parent navigates (by way of FillByParentPrimaryKey) is actually the most efficient. BigGrin







Hi all -



A question concerning the above, which may be" Not Applicable" to StrataFrame but something I've become accustomed to using the Visual FoxExpress framework. Would it be incorrect to expect StrataFrame to automatically handle the calls to FillByParentPrimaryKey() based on the pre-established registering of child Business Objects with their parent Business Object? I would envision tying into the ChildAutoFilterOption property (e.g., CurrentRow) on the parent business object, and having a property on the child BOs that would specify the fill method to use (though perhaps that would just be the FillByParentPrimaryKey()).



Am I missing a reason why this should not be behavior inherent in the framework to handle the countless PC BO relationships we all work with?



My apologies in advance if this is a dumb question... still working on the VFP/VFE-to-dotNET/SF transfer...



Thanks!

________________

_____/ Regards,

____/ al




________________
_____/ Regards,
____/ al
Dustin Taylor
Dustin Taylor
StrataFrame Team Member (660 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
Kind of Wink. One of the major differences between the .NET mindset and the VFP mindset is that you are working in disconnected data, and you want to be very conscious of when you are making trips to the server to retrieve data, and what data you are retrieving. For that reason, we won't automatically go and grab the child data from the server for you in the way you are describing. 

You need to have the flexibility to pull back all pertinent child data at once and then filter off of the selected parent record (one big trip to the server), or just pull back the child data relating to current record every time a different parent is selected (many small trips to the server.) As outlined in my earlier post above, either one could be more efficient or desirable in different situations.

Alex Luyando
Alex Luyando
StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)StrataFrame User (210 reputation)
Group: StrataFrame Users
Posts: 112, Visits: 1.2K
Hi Dustin -



I will re-read the post shortly. I think my question was more along the lines of given that at times it is more efficient to make many, smaller trips to the back end, it seems odd that with all of the properties already set between the parent and child business objects that the framework doesn't provide an option for having the child BOs requery on a change to the parent's row (or at least to the related FK value) without the developer handling the fill. Just seems like the hooks are all in place to make this a framework-provided feature and eliminate the need for the developer to roll similar code many times.



My question is probably more a result of my work in the Visual FoxExpress framework, rather than VFP specifically. That said I do find both frameworks similar in many respects, so that will drive additional questions of this nature. Smile



Thanks!

________________
_____/ Regards,
____/ al
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search