Lazy loading?

Lazy loading?

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


jrnail23 posted on Tuesday, May 09, 2006

I've seen where Rocky has used "lazy loading" in the past (specifically in the VB6 business objects book) to reduce unnecessary overhead from populating contained objects... is there anything like this in the .NET versions of CSLA, or perhaps a better way to implement this?

glenntoy replied on Tuesday, May 09, 2006

There was an errata in Rocky's site in version 1.x http://www.lhotka.net/Articles.aspx?id=3a47a231-1254-410c-babd-d9fc711ca58c you might what to check it.

In your property that holds the child list, I implemented this way,

If Me.IsNew() Then
  Return ChildList.NewChildList('')
Else
  mChildList = ChildList.GetChildList(Me.ID)
  'or mChildList = ChildList.GetChildList(Me)
  Return mChildList
End If

ajj3085 replied on Tuesday, May 09, 2006

Gleen,

From your code it looks like everytime you get the child collection for a new object you'll get an empty one; what happens when you try to add new children to a new object?  Looks like you wouldn't be able to.

Same thing with the get; you'd reload the collection every access (losing any new children).

Perhaps thats correct for your app, but a better idea would probably be:

public ChildrenCol Children {
    get {
          if ( childCol == null ) {
              if ( IsNew ) {
                   childCol = ChildrenCol.New();
              }
              else {
                   childCol = ChildrenCol.Get( this );
              }
          }
          return childCol;
     }
}

glenntoy replied on Tuesday, May 09, 2006

Hi ajj30385,

Thanks for the correction.. I didn't noticed that one. ;-)

I hope jrnail23 can do the lazy loading now

Cheers,

Glenn

 

Jav replied on Tuesday, May 09, 2006

I tried lazy-loading in Csla (It's the same whether .Net 1.0 or 2.0), and found it frustrating in the very situations where you are most likely to use it, i.e., large hierarchies. 
I do use it but selectively, being very deterministic about it. In other words, I try to be fully aware of what branch of a hierarchy can be loaded optionally and then program everything with that awareness, instead of just depending on If Not Null Then Load ...

Jav

glenntoy replied on Tuesday, May 09, 2006

If your doing in winforms, what I did in the CSLA 1.1 and VS.NET 2003 is to use BindingContext() to make it simplier to traverse from parent to child objects - grandchild objects.

I'm quite not sure if I got it right in the syntax its like this

Dim custContacts = Ctype(me.bindingcontext(mCustomers).Current, Customers).Contacts

The above line directs me to the current customer and gain access to Contacts (its child object)

 

Copyright (c) Marimer LLC