I'm looking at the example of Lazy Loading on "Using CSLA 4 Creating Business Objects" ebook, bottom of page 27. And I have two questions:
1-The code shown there for the AddressListCreator class does not get any parent key. How the child collection is going to know which children to load?
2-The code in the method DataPortal_Fetch (in AddressListCreator) is just this line:
Result = DataPortal.CreateChild<AddressEditList>();
What if the calling code (the parent object) is trying to retrieve an already existent collection of children? Should not this code call DataPortal.FetchChild instead?
Sure you can always go to the database, even from DataPortal_Create, and see if you can find any child object for the calling parent, but when the parent is new, that's one trip to the database for nothing.
Finally I did something, but I would like to know if it's the right approach:
In the parent class (the class name is Contact)
public static readonly PropertyInfo<WebAddressList> WebAddressesProperty = RegisterProperty<WebAddressList>(p => p.WebAddresses, RelationshipTypes.Child | RelationshipTypes.LazyLoad);
public WebAddressList WebAddresses
{
get
{
if (!FieldManager.FieldExists(WebAddressesProperty))
{
var creator = WebAddressListCreator.GetWebAddressListCreator(this);
WebAddresses = creator.Result;
}
return GetProperty(WebAddressesProperty);
}
private set
{
LoadProperty(WebAddressesProperty, value);
OnPropertyChanged(WebAddressesProperty);
}
}
And the Creator class:
using System;
using Csla;
namespace ContactInformation.Library.Contact
{
[Serializable]
public class WebAddressListCreator : ReadOnlyBase<WebAddressListCreator>
{
#region Result
public static readonly PropertyInfo<WebAddressList> ResultProperty = RegisterProperty<WebAddressList>(c => c.Result);
public WebAddressList Result
{
get { return ReadProperty(ResultProperty); }
private set { LoadProperty(ResultProperty, value); }
}
#endregion
public static void GetWebAddressListCreator(Contact contact, EventHandler<DataPortalResult<WebAddressListCreator>> callback)
{
if (contact.IsNew)
DataPortal.BeginCreate<WebAddressListCreator>(callback);
else
DataPortal.BeginFetch<WebAddressListCreator>(contact.Id, callback);
}
#if !SILVERLIGHT
public static WebAddressListCreator GetWebAddressListCreator(Contact contact)
{
if (contact.IsNew)
return DataPortal.Create<WebAddressListCreator>();
else
return DataPortal.Fetch<WebAddressListCreator>(contact.Id);
}
private void DataPortal_Create()
{
Result = DataPortal.CreateChild<WebAddressList>();
}
private void DataPortal_Fetch(Guid contactId)
{
Result = DataPortal.FetchChild<WebAddressList>(contactId);
}
#endif
}
}
Yes, your creator command object can (and should) call FetchChild to retrieve an existing child if that's what your scenario requires.
Copyright (c) Marimer LLC