locked
Lazyloading child list with in a Business Object RRS feed

  • Question

  • User197122282 posted

    I am using a 3-layer (UI, BLL, DAL) architecture and use Business Objects (aka Data Transfer Objects - DTO) between these layers to transfer data. Suppose I have the following DTOs:

    public class EmployeeDTO
    {
        public int EmpID { get; set; }
        public string Name { get; set; }
        public ICollection<EmailDTO> EmailList { get; set; }
    }
    
    public class EmailDTO
    {
        public int EmailID { get; set; }
        public string Message { get; set; }
    }

    I want to lazyload the EmailList collection when I use the EmployeeDTO since it can be alot of data. So basically the EmployeeDTO will be created at the DAL level (without Email collection) and sent back to the UI via BLL. At this point how can I lazyload the EmailList at the UI Level. My code (omitting details) is similar to the following:


    UI
    ---

    {
        EmployeeDTO empDTO = new EmployeeBLL().GetEmployeeByID (id);
    }


    BLL
    ---

    public EmployeeDTO GetEmployeeByID (id)
    {
        return new EmployeeDAL().GetEmployeeByID (id);
    }


    DAL
    ---

    public EmployeeDTO GetEmployeeByID (id)
    {
        reader = get employee data by id...
    
        EmployeeDTO empDTO = new EmployeeDTO ();
        empDTO.EmpID = reader [1];
        empDTO.Name  = reader [2];
     // I can load the email list but i dont
        return empDTO;
    }

    Now at the DAL level when I use empDTO.EmailList (), it should lazy load the data.  How can i do so. I need a clean solution please. Thanks.

    Monday, July 29, 2013 12:23 PM

All replies

  • User1424224880 posted

    Change the email list's property so that upon "get" it checks to see if null.  If null it calls and populates itself and returns your object.

    Edit.  In your parent object you would have at least the FK stored... so that it knows what to look up the email list by.

    Kevin Silverthorne

    Monday, July 29, 2013 1:18 PM
  • User197122282 posted

    @cornball, thanks for replying.

    cornball76

    Change the email list's property so that upon "get" it checks to see if null.  If null it calls and populates itself and returns your object.

    Doesn't it look odd to make a call to the DAL from the DTO. For me, a DTO should only hold values and not data access code.

    cornball76

    In your parent object you would have at least the FK stored... so that it knows what to look up the email list by

    Do you mean that each EmailDTO should have FK field to the parent. Obviously the parent cannot store a FK of all children emails. Could you show an example if possible. Thanks once agin.

    Monday, July 29, 2013 2:46 PM