Nested domain-objects and data binding RRS feed

  • Question

  • User197122282 posted

    I am a firm believer of domain object aka DTO, Business Objects. In .NET, we can bind controls to domain objects (a gridview can bind to a list of <Employee>), however, this is limited when it come to nested objects. For example, we have the following DOs (details omitted):

    class Invoice
        int        invID;
        string     title;
        DataTime   datecreated;
        Employee   emp;
    class Employee
        int        empID;
        string     name;
        int        age;
        Address    address;
    class Address
        int         addressID;
        string      street;
        string      phone;
        string      mobile;

    If i try to bind a gridview to a List<Invoice> (with Employee and Address objects populated), it will only display the Invoice attributes while ignoring Employee and Address attributes. In java, it is quite easy to display nested objects using the following syntax:

    <c:forEach var="invoice" ...>
        <a href="InvoiceDetails?invoiceNumber=${invoice.invoiceID}">
          Click to View
      <td>${invoice.emp.name} ${invoice.emp.age}</td> // nested object
      <td>${invoice.emp.address.mobile} ${invoice.emp.address.phone}</td> // nested object

    In the above code, we can clearly see that the nested child objects are accessible from within the parent object and can be easily bound. In .NET, ideally, we should have something similar to the following (given that a gridview is bound to List<Invoice>): 


    Invoice-Date: <%=  Bind ('date')  %> // from the invoice

    Employee-Name: <%=  Bind ('emp.name')  %> // nested employee object in invoice

    Address: <%=  Bind ('emp.address.street')  %> // nested address object in invoice.employee

    Is there a way to achieve the above in .NET? 


    PLEASE NOTE: I am aware of the Entity Framework, however; I would like to solve the above issue through coding.


    Monday, April 15, 2013 2:09 AM


  • User1203810295 posted

    Its <% #Eval("Invoice.emp.name") %>..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 15, 2013 5:03 AM

All replies

  • User1203810295 posted

    Hi..  What you are trying to achieve is possible. If you are assigning the List<Invoice> to the grid then yo have to assign like this to assign Employee Name like this

    <asp:Label id="lbl1" runat="server" text ='<% #Eval("Invoice.Employee.name") %>' %>;

    Monday, April 15, 2013 5:02 AM
  • User1203810295 posted

    Its <% #Eval("Invoice.emp.name") %>..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 15, 2013 5:03 AM
  • User197122282 posted

    nimmi, Excellent point. I tested it and it worked like a charm. This gives .NET developers a lot of power to work with domain objects.

    Tuesday, April 16, 2013 1:45 AM
  • User1203810295 posted

    Thanks  :) As you said with Objects we can play like anything :)

    Tuesday, April 16, 2013 1:50 AM
  • User197122282 posted

    If I am binding a list of Invoice to a gridview, I may like to display a few Employee attributes e.g. Name, Age etc. Usually the BLL will return the list of Invoice. This means, everytime a new Invoice is added to the list, we also have to fetch the Employee data. This will be a very expensive database call. What would be the best way to handle such a situation where we need to display attributes of nested objects.

    (On the other hand, we can just write a single query, fetch all the needed data - combining Invoice and Employee - in a DataTable and bind it to a gridview).

    Wednesday, April 17, 2013 4:41 AM
  • User1203810295 posted

    Hi.. the way is to use a sigle query to fill both invoice and employee so that you can skip hitting the DB again and again, at the same time we should not use so many joins and load much in business objects

    Wednesday, April 17, 2013 7:09 AM