WCF Child Fetch Question

WCF Child Fetch Question

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


jfreeman posted on Tuesday, August 12, 2008

This is probably pretty simple but I am stuck and need some help.  I have a parent child relationship and I want to lazy load the children when I select a parent record in a list.  I am able to get this to work.  The problem comes when I set up the aplication to use the WCF Data Portal.  The application works fine when I call a ReadOnlyBase object and get data but I get errors when I run Child_Fetch in my ReadOnlyListBase object.  Here is an example of the Child_Fetch code:

private void Child_Fetch(int censusID)

{

this.RaiseListChangedEvents = false;

using (var mgr = ContextManager<CensusDataContext>.GetManager("HotelDB"))

{

var data = (from p in mgr.DataContext.up_Census_Destinations(censusID) select p);

IsReadOnly = false;

foreach (var item in data)

   Add(new Census_DestinationInfo(item.RouteOriginCode, item.RouteTypeCode, item.Destination));

IsReadOnly = true;

}

this.RaiseListChangedEvents = true;

}

It looks like it is not using the WCF data portal for the child fetch.  Is that possible?  Thanks.

Jonathan

 

RockfordLhotka replied on Tuesday, August 12, 2008

To lazy load a child, that child needs to have a factory method like a root object, and a DataPortal_Fetch() method to go with it. You have to manually call MarkAsChild() in that DataPortal_Fetch() method.

[Serializable]
public class MyChild
{
  // ...

  internal static MyChild GetChild(...)
  {
    return DataPortal.Fetch<MyChild>(...);
  }

  private void DataPortal_Fetch(...)
  {
    MarkAsChild();
    // load data here
  }
}

You'll implement the insert/update/delete methods as Child_XYZ, but the fetch has to act like a root so the data portal can interact with it directly.

jfreeman replied on Wednesday, August 13, 2008

Do I need to call the MarkAsChild even thought I'm using version 3.5?  Also, my Fetch logic is currently in a ReadOnlyList object (ChildList).  Does it need to be in the ReadOnlyBase (ChildInfo)?  When I tried to add the MarkAsChild to the DataPortal_Fetch in the ChildList, it could not find the method.  Which namespace is the method in?  Thanks.

Jonathan

RockfordLhotka replied on Wednesday, August 13, 2008

The problem is that there are several scenarios, and you need to understand the principles to know what to do in each scenario.

 

1.       The data portal can retrieve ANY object as long as that object has a DataPortal_Fetch() method (this even includes non-CSLA objects!).

2.       When the data portal invokes DataPortal_Fetch() it DOES NOT automatically call MarkAsChild(), because it assumes you are retrieving a root object.

3.       If your DataPortal_Fetch() method retrieves OTHER objects by using the FetchChild() method, THOSE objects will be loaded through Child_Fetch(), and they will be automatically marked as child objects.

4.       Read-only objects don’t have MarkAsChild(), because they are never updated (so there’s no difference between a root and child).

 

So if you are lazy loading a read-only list, that list will have an internal/Friend factory that calls DataPortal.Fetch(), which calls DataPortal_Fetch(). That DataPortal_Fetch() method will typically use DataPortal.FetchChild() to load the child objects into the list. Because these are read-only objects, no MarkAsChild() is involved.

 

If you are lazy loading an editable list, you’d follow the same procedure, but you must manually call MarkAsChild() in the DataPortal_Fetch() of the list - but DataPortal.FetchChild() will make that automatic for the objects inside the list.

 

Rocky

 

jfreeman replied on Wednesday, August 13, 2008

I think we are on the same page.  I added the DataPortal calls to my List object late last night and got things working but was confused when I saw mention of the MarkAsChild method.  Thanks for explaining how things work in detail.  I really appreciate your time.  Thanks.

Jonathan

Copyright (c) Marimer LLC