milani_lucie
asked on
Object Oriented Architecture .... Lazy loading the child objects in VS 2008 using C#
Hi,
I have a "Person" class with "Orders" as the child object. Now "Orders" represents which is of type "List<OrderType>". When page is loading then i am loading ALL the child objects for each parent object. Here is the sample code:
/// <summary>
/// Load all records from database
/// </summary>
/// <returns></returns>
public List<Person> Load()
{
List<Person> listPerson = new List<Person>();
SqlConnection connPerson = new SqlConnection(connStr);
connPerson.Open();
SqlCommand cmdPerson = new SqlCommand("LoadPerson", connPerson);
cmdPerson.CommandType = CommandType.StoredProcedur e;
try
{
SqlDataReader drPerson = cmdPerson.ExecuteReader(Co mmandBehav ior.CloseC onnection) ;
while (drPerson.Read())
{
Person p = new Person();
p.PersonID = Convert.ToInt32(drPerson[" PersonID"] );
p.FirstName = Convert.ToString(drPerson[ "FirstName "]);
p.LastName = Convert.ToString(drPerson[ "LastName" ]);
p.Age = Convert.ToInt32(drPerson[" Age"]);
List<OrderType> listOrder = new List<OrderType>();
SqlConnection connOrder = new SqlConnection(connStr);
connOrder.Open();
SqlCommand cmdOrder = new SqlCommand("LoadOrder", connOrder);
cmdOrder.CommandType = CommandType.StoredProcedur e;
try
{
cmdOrder.Parameters.AddWit hValue("@p ersonID", p.PersonID);
SqlDataReader drOrder = cmdOrder.ExecuteReader(Com mandBehavi or.CloseCo nnection);
while (drOrder.Read())
{
OrderType o = new OrderType();
o.OrderID = Convert.ToInt32(drOrder["O rderID"]);
o.PersonID = Convert.ToInt32(drOrder["P ersonID"]) ;
o.OrderName = Convert.ToString(drOrder[" OrderName" ]);
listOrder.Add(o);
}
}
catch
{
throw;
}
finally
{
cmdOrder.Dispose();
connOrder.Close();
connOrder.Dispose();
}
p.Orders = listOrder;
listPerson.Add(p);
}
}
catch
{
throw;
}
finally
{
cmdPerson.Dispose();
connPerson.Close();
connPerson.Dispose();
}
return listPerson;
}
When there are too many obejcts then i am getting very slow response onto UI as it has to loop through ALL the parent and child objects even though they are not necessary for the first time. For the first time i need ONLY the parent objects. So i want to do LAZY loading of the "Orders" which is of type "List<OrderType>".
Can you please let me know how can i do it ? Some how i need to load the objects when i require them but not at the page load. Otherwise my page will not respond if there are too may objects to load. Can you please provide me the code in C# ?
Thanks
I have a "Person" class with "Orders" as the child object. Now "Orders" represents which is of type "List<OrderType>". When page is loading then i am loading ALL the child objects for each parent object. Here is the sample code:
/// <summary>
/// Load all records from database
/// </summary>
/// <returns></returns>
public List<Person> Load()
{
List<Person> listPerson = new List<Person>();
SqlConnection connPerson = new SqlConnection(connStr);
connPerson.Open();
SqlCommand cmdPerson = new SqlCommand("LoadPerson", connPerson);
cmdPerson.CommandType = CommandType.StoredProcedur
try
{
SqlDataReader drPerson = cmdPerson.ExecuteReader(Co
while (drPerson.Read())
{
Person p = new Person();
p.PersonID = Convert.ToInt32(drPerson["
p.FirstName = Convert.ToString(drPerson[
p.LastName = Convert.ToString(drPerson[
p.Age = Convert.ToInt32(drPerson["
List<OrderType> listOrder = new List<OrderType>();
SqlConnection connOrder = new SqlConnection(connStr);
connOrder.Open();
SqlCommand cmdOrder = new SqlCommand("LoadOrder", connOrder);
cmdOrder.CommandType = CommandType.StoredProcedur
try
{
cmdOrder.Parameters.AddWit
SqlDataReader drOrder = cmdOrder.ExecuteReader(Com
while (drOrder.Read())
{
OrderType o = new OrderType();
o.OrderID = Convert.ToInt32(drOrder["O
o.PersonID = Convert.ToInt32(drOrder["P
o.OrderName = Convert.ToString(drOrder["
listOrder.Add(o);
}
}
catch
{
throw;
}
finally
{
cmdOrder.Dispose();
connOrder.Close();
connOrder.Dispose();
}
p.Orders = listOrder;
listPerson.Add(p);
}
}
catch
{
throw;
}
finally
{
cmdPerson.Dispose();
connPerson.Close();
connPerson.Dispose();
}
return listPerson;
}
When there are too many obejcts then i am getting very slow response onto UI as it has to loop through ALL the parent and child objects even though they are not necessary for the first time. For the first time i need ONLY the parent objects. So i want to do LAZY loading of the "Orders" which is of type "List<OrderType>".
Can you please let me know how can i do it ? Some how i need to load the objects when i require them but not at the page load. Otherwise my page will not respond if there are too may objects to load. Can you please provide me the code in C# ?
Thanks
In the definition of Orders Property in the Person class should look like this
and you need not initialize p.Orders in the above code. The p.Orders will get populated the first time it is called.
Thanks,
Chandra
public class Person
{
private List<OrderType> _orders = null;
public List<OrderType> Orders
{
get
{
if (_orders == null)
{
SqlConnection connOrder = new SqlConnection(connStr);
connOrder.Open();
SqlCommand cmdOrder = new SqlCommand("LoadOrder", connOrder);
cmdOrder.CommandType = CommandType.StoredProcedure;
try
{
cmdOrder.Parameters.AddWithValue("@personID", p.PersonID);
SqlDataReader drOrder = cmdOrder.ExecuteReader(CommandBehavior.CloseConnection);
while (drOrder.Read())
{
OrderType o = new OrderType();
o.OrderID = Convert.ToInt32(drOrder["OrderID"]);
o.PersonID = Convert.ToInt32(drOrder["PersonID"]);
o.OrderName = Convert.ToString(drOrder["OrderName"]);
_orders.Add(o);
}
}
catch
{
throw;
}
finally
{
cmdOrder.Dispose();
connOrder.Close();
connOrder.Dispose();
}
}
else
{
return _orders;
}
}
}
}
and you need not initialize p.Orders in the above code. The p.Orders will get populated the first time it is called.
Thanks,
Chandra
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Missed again :-). Add "_orders = new List<OrderType>();" as first statement in the if condition.
Thanks,
Chandra
Thanks,
Chandra
http://www.asp.net/data-access/tutorials/master-detail-using-a-selectable-master-gridview-with-a-details-detailview-cs