CSLA Light & Lazy Loading

CSLA Light & Lazy Loading

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


CampbellCM posted on Tuesday, April 07, 2009

Is it possible to do lazy loading of child objects with CSLA Light?  If so, is there an example of how to do that?

RockfordLhotka replied on Tuesday, April 07, 2009

Sergey may be able to point to an example.

But you should know that it is a little tricky due to the async nature of Silverlight. You can't do lazy loading like you would in .NET.

Consider this:

public ListType ChildList
{
  get
  {
    if (!FieldManager.FieldExists(ChildListProperty))
      LazyLoadChildList();
    return GetProperty(ChildListProperty);
  }
}

private void LazyLoadChildList()
{
  ListType.GetList(this.Id, (o, e) =>
    {
      LoadProperty(ChildListProperty, e.Object);
      OnPropertyChanged(ChildListProperty.Name);
    });
}

This is pseudocode from memory, but it should be pretty close. The thing I want you to consider is that the first code that tries to access the ChildList property will almost certainly get a null. Then some time late (seconds?) the actual child list will arrive on the client, and the ChildList property will start returning a non-null value.

So the calling code (probably the UI or View) will need to be able to handle getting a null first, and then getting the real value later.

CampbellCM replied on Tuesday, April 07, 2009

Thanks Rocky, that helps a lot.

I'm hung up on one thing though.  If I'm understanding this correctly the call to OnPropertyChanged is intended to provide a notification that will force a refresh in the UI/View.  Is that correct?  The problem is that there is no OnPropertyChanged method available and I'm at a loss as to what would be the equivalent.

sergeyb replied on Tuesday, April 07, 2009

Here is an example of lazy loaded property in Silverlight.

 

 

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);

            }

        }

 

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: CampbellCM [mailto:cslanet@lhotka.net]
Sent: Tuesday, April 07, 2009 10:40 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] CSLA Light & Lazy Loading

 

Thanks Rocky, that helps a lot.

I'm hung up on one thing though.  If I'm understanding this correctly the call to OnPropertyChanged is intended to provide a notification that will force a refresh in the UI/View.  Is that correct?  The problem is that there is no OnPropertyChanged method available and I'm at a loss as to what would be the equivalent.



CampbellCM replied on Tuesday, April 07, 2009

Thanks Sergey.

I realize now what my problem is.  My BO is RO and that's why I don't have an OnPropertyChanged method.  Aside from changing my BO to RW from RO is there anything else I can do?

RockfordLhotka replied on Tuesday, April 07, 2009

You could copy the INotifyPropertyChanged implementation from BindableBase into your business class.

 

Rocky

CampbellCM replied on Thursday, April 09, 2009

Thanks Rocky.

Andreas replied on Tuesday, May 12, 2009

Hi Sergeby,

your implementation assumes that

1. Returning a "null" value vom ReadProperty indicates a property that has never been loaded before. What if the lazy loaded property value equals "null"?

2. In asynchronous calls you probalby need to lock the code around _loadingScore because it is shared between two threads.

Andreas

 

sergeyb replied on Tuesday, May 12, 2009

I do not need to lock the _loadingScore because it is not static variable.  In my case this property could not be null when loaded, so I did not need an extra check.  If I did, I would add another variable _scoreLoaded and look at it.

 

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: Andreas [mailto:cslanet@lhotka.net]
Sent: Tuesday, May 12, 2009 10:50 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: CSLA Light & Lazy Loading

 

Hi Sergeby,

your implementation assumes that

1. Returning a "null" value vom ReadProperty indicates a property that has never been loaded before. What if the lazy loaded property value equals "null"?

2. In asynchronous calls you probalby need to lock the code around _loadingScore because it is shared between two threads.

Andreas

 



Andreas replied on Tuesday, May 12, 2009

OK...I am still taking a learning curve on parallel programming...but would'nt it be possible that the same thread that called this code calls it again by using the same instance (and so using the same _loadingScore member variable) before the delegate returns...?

RockfordLhotka replied on Tuesday, April 07, 2009

There should be. BusinessBase inherits from BindableBase, which implements a protected method named OnPropertyChanged.

 

That method is marked so it only shows up if you have ‘advanced’ intellisense enabled – so maybe you just aren’t seeing it in intellisense – but it is there.

 

Rocky

 

 

From: CampbellCM [mailto:cslanet@lhotka.net]
Sent: Tuesday, April 07, 2009 9:40 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] CSLA Light & Lazy Loading

 

Thanks Rocky, that helps a lot.

I'm hung up on one thing though.  If I'm understanding this correctly the call to OnPropertyChanged is intended to provide a notification that will force a refresh in the UI/View.  Is that correct?  The problem is that there is no OnPropertyChanged method available and I'm at a loss as to what would be the equivalent.



swegele replied on Tuesday, May 12, 2009

Wow this async lazy loading issue for silverlight just hit me square in the forehead...been reading all the posts.

So do you think it is probably just simpler to remove the lazy loaded properties in silverlight (via directive/partial class) if loading them up front is too expensive?  So in silverlight the UI developer would have to make another call to a factory method passing in the relevant parent?  I think that is what you were getting at in this post http://forums.lhotka.net/forums/permalink/30507/30529/ShowThread.aspx#30529

This async stuff for silverlight is introducing a fair amount of complexity...and that with the TONS of work you and magenic have done to hide most of it.  Maybe I am just an old dog trying to learn new tricks :-)  Maybe complexity isn't the word as much as a need for "exactness" or "adherance".

Anyway I am having fun adapting my architecture to 3.6.2 to play well with silverlight...just a lot to learn!  Thanks.

Sean

Andreas replied on Wednesday, May 13, 2009

Hi Sean,

you made a very good point! It is worth to think about.

Thanks,

Andreas

 

 

bniemyjski replied on Tuesday, August 31, 2010

Hello,

We have also addressed this issue in the soon to be released v3.0.1 of the CodeSmith Templates.

Thanks

-Blake Niemyjski

Copyright (c) Marimer LLC