Howto implement Lazy load with DAAB

Howto implement Lazy load with DAAB

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


NickyLarson posted on Saturday, September 12, 2009

Hello everyone.

I don´t want to use Linq or EF yet.

The projectTracker example

  private void DataPortal_Fetch(SingleCriteria<Project, Guid> criteria)
    {
      using (var ctx = ContextManager<ProjectTracker.DalLinq.PTrackerDataContext>.GetManager(ProjectTracker.DalLinq.Database.PTracker))
      {
        // get project data
        var data = (from p in ctx.DataContext.Projects
                    where p.Id == criteria.Value
                    select p).Single();

        LoadProperty(IdProperty, data.Id);
        LoadProperty(NameProperty, data.Name);
        LoadPropertyConvert<SmartDate, System.DateTime?>(StartedProperty, data.Started);
        LoadPropertyConvert<SmartDate, System.DateTime?>(EndedProperty, data.Ended);
        LoadProperty(DescriptionProperty, data.Description);
        _timestamp = data.LastChanged.ToArray();

        // get child data
        LoadProperty(
        ResourcesProperty,
        ProjectResources.GetProjectResources(data.Assignments.ToArray())); <--- Linq Entity Set
      }
    }

How can I  do the same thing but with ADO.Net (DAAB)

Thanks

Nicky



JonnyBee replied on Saturday, September 12, 2009

Hi,

When using pure ADO.NET  you will have use the DataReader /SafeDataReader to get your data.

Alt 1: Use stored procedures that return multiple recordsets and pass the DataReader as parameter.
Alt 2: Use direct sql/command to get to each table and read data from the datareader.

Alt1 is shown in ProjectTracker sample for Csla 3.0.5.

/jonnybee

NickyLarson replied on Saturday, September 12, 2009



Hi jonnybee

Thanks for the answer..

I will check this example ProjectTracker for Csla 3.0.5.

Nicky

JonnyBee replied on Saturday, September 12, 2009

Hi Nick,

Your question was about "Lazy loading" - but the code sample you provided does not show LazyLoading .

LazyLoading is when you do not fetch the child list upon initial fetch of the root - but does a fetch of values when the property is accessed.

Conceptually the lazy loaded list is a:
/jonnybee

NickyLarson replied on Sunday, September 13, 2009



Hi jonnybee

The code I post, in the book of Rocky he says
"It is important to realize that the data.Assignments.ToArray() call causes LINQ to SQL to make
a second query to the database to retrieve the list of assignments. LINQ to SQL uses a lazy loading
scheme and only retrieves data when necessary.
"

That´s why I talk about lazy load.

About the example you tell me to  see in ProjectTracker sample for Csla 3.0.5.

   private void DataPortal_Fetch(Criteria criteria)
    {
      using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
      {
        cn.Open();
        using (SqlCommand cm = cn.CreateCommand())
        {
          cm.CommandType = CommandType.StoredProcedure;
          cm.CommandText = "getProject";
          cm.Parameters.AddWithValue("@id", criteria.Id);

          using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
          {
            dr.Read();
            _id = dr.GetGuid("Id");
            _name = dr.GetString("Name");
            _started = dr.GetSmartDate("Started", _started.EmptyIsMin);
            _ended = dr.GetSmartDate("Ended", _ended.EmptyIsMin);
            _description = dr.GetString("Description");
            dr.GetBytes("LastChanged", 0, _timestamp, 0, 8);

            // load child objects
            dr.NextResult();
            _resources.ListChanged -= new System.ComponentModel.ListChangedEventHandler(_resources_ListChanged); <--- Do I need this ? I use CSLA 3.7.0
            _resources = ProjectResources.GetProjectResources(dr);
            _resources.ListChanged += new System.ComponentModel.ListChangedEventHandler(_resources_ListChanged); <--- Do I need this ? I use CSLA 3.7.0
          }
        }
      }
    }


One more question if I may. Howto to implement using pure ADO.NET lazy load child?
I have to do all the work, I have to call the MarkAsChild method ?

Thanks

Nicky

rfcdejong replied on Sunday, September 13, 2009

data.Assignments.ToArray() does make the call for lazy loading in L2SQL, but u do it in the fetch method so it becomes a eager fetch for the business object the way u do it. Even with L2S u should do data.Assignments.ToArray() in a property using the following code (for sync lazy loading)

public static PropertyInfo resourcesProperty = .. register theproperty
public ResourcesList Resources
{
get
{
if (!FieldManager.FieldExists(fieldname))
{
int resourceId = ReadProperty(ResourceIdProperty);
LoadProperty(resourcesProperty, ProjectResources.GetProjectResource(resourceId));
}
return GetProperty(resourcesProperty);
}
set
{
...
}
}

ofcourse there only a reason to do lazy loading if u know that the UI won't access the property often

NickyLarson replied on Monday, September 14, 2009


Hi rfcdejong

Yes you are right..

Thanks for open my eyes.

Nicky




rfcdejong replied on Tuesday, September 15, 2009

It's nice to have an eye opener hehe

Copyright (c) Marimer LLC