Lazy Loading and the Async Data Portal

Lazy Loading and the Async Data Portal

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


esaulsberry posted on Friday, June 12, 2009

Hello,

I wonder if anyone has done a Silverlight / Async data portal implementation with lazy loaded properties.  The lazy loaded technique seems to be to call DataPortal.Fetch on the child if it doesn't exist, however the async data portal needs a call back handler.  The handler could be cached from the call to the parent factory method I suppose.  Dunno if that would work.  Other than that, I'm wondering if there's a technique that's anyone's used successfully.

sergeyb replied on Friday, June 12, 2009

Here is a thread on the subject.

 

http://forums.lhotka.net/forums/thread/32438.aspx

 

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: esaulsberry [mailto:cslanet@lhotka.net]
Sent: Friday, June 12, 2009 1:58 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Lazy Loading and the Async Data Portal

 

Hello,

I wonder if anyone has done a Silverlight / Async data portal implementation with lazy loaded properties.  The lazy loaded technique seems to be to call DataPortal.Fetch on the child if it doesn't exist, however the async data portal needs a call back handler.  The handler could be cached from the call to the parent factory method I suppose.  Dunno if that would work.  Other than that, I'm wondering if there's a technique that's anyone's used successfully.



esaulsberry replied on Friday, June 12, 2009

sergeyb:

private bool _loadingScore = false;
private static PropertyInfo<Score> ScoreInfoProperty = RegisterProperty(new PropertyInfo<Score>("ScoreInfo", "ScoreInfo"));
public Score ScoreInfo {
     get   {
        
if (!_loadingScore && (!FieldManager.FieldExists(ScoreInfoProperty) || ReadProperty(ScoreInfoProperty) == null))     {

#if SILVERLIGHT
                    _loadingScore = true;
                    Score.GetScore(ReadProperty(ProfileIDProperty), ReadProperty(PersonNameProperty), ReadProperty(NicknameProperty), ReadProperty(TeamIDProperty),((TeamMemberInfoList)this.Parent).ParentTeam.Name, (o, e) =>                    {

                        LoadProperty(ScoreInfoProperty, e.Object);
                        OnPropertyChanged(ScoreInfoProperty.Name);
                        _loadingScore = false;
                    }
                    );

#else

                    LoadProperty(ScoreInfoProperty, Score.GetScore(ReadProperty(ProfileIDProperty), ReadProperty(PersonNameProperty), ReadProperty(NicknameProperty), ReadProperty(TeamIDProperty), ((TeamMemberInfoList)this.Parent).ParentTeam.Name));

#endif
                }

                return GetProperty(ScoreInfoProperty);
            }
        }

OK, If I grock this, you're providing a different silverlight and non-silverlight lazy loaders in the get, which call different factory methods in the child - the silverlight one takes a handler and the regular one doesn't.  In this case you're providing the handler as a lamba, but it could be silverlight only code elsewhere in the object that accomplished the same thing.  The _loading boolean keeps you from starting the process again before the first one finishes.  The return will run before the data comes back so at first the value of the property is null, a property changed event happens when the data comes back so the UI can update itself.  An enigma, wrapped in a mystery... but elegant.

 

RockfordLhotka replied on Friday, June 12, 2009

It is also possible that the LoadPropertyAsync() method can help you in this case.

In any case, technically you don't need a different implementation for SL from Windows - but SL can only have the async implementation, and most .NET developers won't expect the async behavior.

Copyright (c) Marimer LLC