CSLA 4 Async Lazy Load

CSLA 4 Async Lazy Load

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


tgddev posted on Tuesday, April 05, 2011

Hi,

       Comparing the implementations of async lazy-loading available in the forum and the one provided in th e Objects ebook, page 29, I have two doubts:

   1) "if (!FieldManager.FieldExists(FieldXProperty))"

                            or

      "if (!FieldXLoading && !FieldManager.FieldExists(FieldXProperty))" ?

   2) Are the calls to MarkBusy() / MarkIdle() required or the object busy state is handled automatically by the framework?

tiago replied on Tuesday, April 05, 2011

Indirectly answering your question 1), I had different doubts and compiled the code from the 2008 book and Using Csla 4 ebook and got the following sample:

/// <summary>
/// Maintains metadata about child <see cref="Docs"/> property.
/// </summary>
private static readonly PropertyInfo<FolderDocCollDocsProperty = 
    RegisterProperty<FolderDocColl>(p => p.Docs"Docs"RelationshipTypes.Child | RelationshipTypes.LazyLoad);
/// <summary>
/// Gets the Docs (lazy load child property).
/// </summary>
/// <value>The Docs.</value>
public FolderDocColl Docs
{
    get
    {
        if (!FieldManager.FieldExists(DocsProperty))
            if (this.IsNew)
                Docs = FolderDocColl.NewFolderDocColl();
            else
                Docs = FolderDocColl.GetFolderDocColl(ReadProperty(FolderIDProperty));
 
        return GetProperty(DocsProperty);
    }
    private set
    {
        LoadProperty(DocsPropertyvalue);
        OnPropertyChanged(DocsProperty);
    }
}

This also covers the case where you need to load the property when the object is new and need to initialize the lazy loaded property with default values.

tiago replied on Monday, February 11, 2013

The point is

FolderDocColl.GetFolderDocColl(folderID)
 
must call

DataPortal.Fetch(int folderID) // DataPortal_Fetch(int folderID)

to execute on the server, as 

DataPortal.FetchChild(int folderID) // Child_Fetch(int folderID)

willl keep the execution on the client.

JonnyBee replied on Monday, February 11, 2013

Your question is about async lazy load.

Use the LoadPropertyAsync method in BusinessBase / ReadOnlyBase.

This method accepts a Task (async/await DataPortal or Factory call)  and will make sure that only one async method is triggered for this property and mark the property as busy during load.

Copyright (c) Marimer LLC