Eager Loading and Lazy Loading in ADO.NET Data Services

December 5, 2008

Eager Loading and Lazy Loading in ADO.NET Data Services


In today’s post I’m goingEager Loading and Delay Loading in ADO.NET Data Services
to explain how to achieve
eager loading and lazy loading
functionality with ADO.NET
data services
.NET clients.


What are Eager Loading and Lazy Loading?


Lazy loading is a design pattern that is commonly used to defer initialization of an
object up until it is needed by the program. The gains of using the pattern include
efficiency (if it’s used right) and sometime performance. Eager loading is the opposite
pattern of lazy loading. In this pattern we initialize the object before hand and don’t
wait to the second we really need it.


Lazy Loading in Data Service Client


The default behavior of a data service’s .NET client is not to load the entities’ associated
objects . When we request an entity we will get it from the service but its associated
objects will not load up at all. Lets say that I have two entities in my program – a course
and a department. The associations between the entities are that a department can have
a lot of courses and a course belongs to one department. When I load a department
it’s list of courses will be empty. trying to iterate the list of courses will give nothing
because the courses will not load until we tell them to be loaded explicitly. This is done by
the LoadProperty method of the data service context. Lets look at an example:



protected void Page_Load(object sender, EventArgs e)


{


    var proxy = new SchoolEntities(


        new Uri(ConfigurationManager.AppSettings["SchoolProxy"]));


    proxy.MergeOption = MergeOption.AppendOnly;


    PrintDepartmentsToResponse(proxy);


}


 


private void PrintDepartmentsToResponse(SchoolEntities proxy)


{


    foreach (var department in proxy.Department)


    {


        Response.Write(string.Format(


            “Department ID: {0}, Department Name: {1}, Department Budget: {2}”,


            department.DepartmentID,


            department.Name,


            department.Budget));


        proxy.LoadProperty(department, “Course”);


        foreach (var course in department.Course)


        {


            Response.Write(string.Format(


                “Course ID: {0}, Course Title: {1}”,


                course.CourseID,


                course.Title));


            Response.Write(“<br/>”);


        }


        Response.Write(“<br/>”);


    }


    Response.Write(“<br/>”);


    Response.Write(“<br/>”);


}


In the example I’m using a built SchoolEntities proxy. I iterate the list of departments
That will arrive to the client once I loop them in the foreach loop. As you can see
I’m using the proxy.LoadProperty method that gets two parameters – the object to
work with and the property to lazy load. After loading the property I can iterate and
write the courses to the response output. The result of running this code will be
something like:
Running result


Eager Loading in Data Service Client


The lazy loading scenario that I showed in the previous example will send a HTTP request
to the data service’s server for each course list. This lazy load behavior can cause
sometime a decrease in the performance because of extra unnecessary round-trips. In the
previous example we could fetch the courses with the department when we requested the
department. The way to achieve the eager loading behavior is by using the $exapnd Uri
syntax. The $exapnd is used to indicate that we want inline expansion for our objects.
Lets look at the same example from the previous example but in eager loading scenario:



protected void Page_Load(object sender, EventArgs e)


{


    var proxy = new SchoolEntities(


        new Uri(ConfigurationManager.AppSettings["SchoolProxy"]));


    proxy.MergeOption = MergeOption.AppendOnly;


    PrintDepartmentsToResponse(proxy);


}


 


private void PrintDepartmentsToResponse(SchoolEntities proxy)


{


    var departments =


        proxy.Execute<Department>(new Uri(“Department?$expand=Course”,


            UriKind.Relative));


    foreach (var department in departments)


    {


        Response.Write(string.Format(


            “Department ID: {0}, Department Name: {1}, Department Budget: {2}”,


            department.DepartmentID,


            department.Name,


            department.Budget));


        foreach (var course in department.Course)


        {


            Response.Write(string.Format(


                “Course ID: {0}, Course Title: {1}”,


                course.CourseID,


                course.Title));


            Response.Write(“<br/>”);


        }


        Response.Write(“<br/>”);


    }


    Response.Write(“<br/>”);


    Response.Write(“<br/>”);


}


The same result will be printed to the response’s output.


Summary


Lets sum up, in today’s post I explained how to use eager loading and how
to use lazy loading in an ADO.NET data service’s .NET client. The use of these patterns
can have an impact on the program performance and therefore should be use wisely.


DotNetKicks Image

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 comments

  1. Chris DeweeseJanuary 28, 2009 ב 4:57

    Thanks for this short article. Seeing both the lazy and eager options was just what I needed for a demo I am building to show our developers ADO.NET Data Services.

    Reply