how to process Async FetchChild? No BeginFetchChild method?

how to process Async FetchChild? No BeginFetchChild method?

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


ddredstar posted on Friday, January 21, 2011

As we know, Csla has FetchChild for Child Object.

anyone please tell me how to process Async FetchChild.

Thanks

JonnyBee replied on Friday, January 21, 2011

Create an <Object>Accessor (a ReadOnlyRoot or CommandBase derivate) that implements the DataPortal call (and can run Async) and creates the chuld object on the "logical serverside" and returns it to the client.

This technique is described in the Csla4 EBook Creating Business Objects and I highly recommend buying it.

 

OscarLauroba replied on Tuesday, January 25, 2011

I have the book and I didn't find any sample that shows how to use FetchChild.

My problem is that I have a read-only child list that I want to load in a asynchronous lazy way. The book shows how to use a "child object creator", but doesn't show how this object creator gets a read-only child list from its data access region.(I've gotten 0 matches when searching FetchChild in the pdf).

 It would be very interesting to see how it can be done. Maybe the book is uncomplete yet?

JonnyBee replied on Tuesday, January 25, 2011

So the lazy loaded property would look something like this:

    public static readonly PropertyInfo<AddressEditList> AddressesProperty =
         RegisterProperty<AddressEditList>(c => c.Addresses,
         RelationshipTypes.Child | RelationshipTypes.LazyLoad);
    public AddressEditList Addresses
    {
      get
      {
        if (!FieldManager.FieldExists(AddressesProperty))
        {
          DataPortal.BeginFetch<AddressListCreator>((o, e) =>
          {
            if (e.Error != null)
              throw e.Error;
            else
              Addresses = e.Object.Result;
          });
          return null;
        }
        else
        {
          return GetProperty(AddressesProperty);
        }
      }
      private set
      {
        LoadProperty(AddressesProperty, value);
        OnPropertyChanged(AddressesProperty);
      }
    }

and the AddressListCreator would be like this:

  [Serializable]
  public class AdresslistCreator : ReadOnlyBase<AdresslistCreator>
  {
    public static readonly PropertyInfo<AddressEditList> ResultProperty = RegisterProperty<AddressEditList>(c => c.Result);
    public AddressEditList Result
    {
      get { return GetProperty(ResultProperty); }
      set { LoadProperty(ResultProperty, value); }
    }
  
    private void DataPortal_Fetch(int criteria)
    {
        // get data from database into repository objects or use L2S / EF ....
        
        Result = AddressEditList.GetAddressEditList(addressDataList);
      }
    }
  }

and AddressEditList would be like this:

  [Serializable]
  public class AddressEditList : BusinessListBase<AddressEditListAddressEdit>
  {
    internal static AddressEditList GetAddressEditList(
      object childData)
    {
      return DataPortal.FetchChild<AddressEditList>(childData);
    }

    private void Child_Fetch(object childData)
    {
      RaiseListChangedEvents = false;
      foreach (var child in (IList<object>)childData)
        this.Add(AddressEdit.GetAddressEdit((child)));
      RaiseListChangedEvents = true;
    }
  }

and further AddressEdit like this:

  [Serializable]
  public class AddressEdit : BusinessBase<AddressEdit>
  {
    internal static AddressEdit GetAddressEdit(object childData)
    {
      return DataPortal.FetchChild<AddressEdit>(childData);
    }

    private void Child_Fetch(object childData)
    {
      // TODO: load values
    }
  }

ddredstar replied on Wednesday, February 16, 2011

Why need AdresslistCreator ?

Why not use AddressEditList  directly?

DataPortal.BeginFetch<AddressEditList >((o, e) =>
          {
            if (e.Error != null)
              throw e.Error;
            else
              Addresses = e.Object.Result;
          });

JonnyBee replied on Wednesday, February 16, 2011

AddressEditList in this example is an EditableChildList with EditableChild and would normally NOT have static factory methods nor an implementation of DataPortal_Fetch with criteria object(s).

So the AddressListCreator is "readonly-root" object that accepts parameters, implements DataPortal_Fetch and is responsible for loading a child list.

ddredstar replied on Wednesday, February 16, 2011

it seems too lazy, Object.Addresses return null, how to resolve this?

JonnyBee replied on Thursday, February 17, 2011

Please post your code - impossible to give to good recommendations without a review of your code.

ddredstar replied on Thursday, February 17, 2011

the code is as your code:

i use it like this in my ViewModel

AddressEditList  addresslist=Object.AddressEditList ;

and the addresslist is null.

RockfordLhotka replied on Thursday, February 17, 2011

Do you check e.Error in the client side callback? if your object isn't there, maybe it is because of an error.

RockfordLhotka replied on Wednesday, February 16, 2011

OscarLauroba

Maybe the book is uncomplete yet?

The preview you have of the Data Access ebook is about 50% complete, so there is a lot more content coming.

Copyright (c) Marimer LLC