Niraj Bhatt – Architect's Blog

Ruminations on .NET, Architecture & Design

Displaying Object Associations with ASP.NET

This was a part of one my recent discussion. One of the application developers was working on Web Application project and was stuck with a page. His page was having a grid and that grid required him to display data across tables. A join query was the solution but the problem for him was how to structure his domain model which was object oriented. He was using plain vanilla classes with no relationship across them. E.g. Consider the class Person below (simplified version of his actual data model)

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string AddressID { get; set; } /* Foreign key to Address Table */
}

He was mapping a class with a table and had no associations across entities. While he survived few initial pages which were standalone, he was stuck now. He was thinking of putting additional fields in Person class which would include address table fields like PinCode and Apartment. But that would just result in duplication considering address is also linked with other tables. So we changed the model as below creating a separate class for Address

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}

public class Address
{
public string PINCODE { get; set; }
public string ApartmentName { get; set; }
}

Now he can still use a join query and retrive the result set in his repository but his repository would return a populated object model. Immediate challenge with this though is displaying UI in .ASPX page. Below code would skip the address completely

<asp:GridView ID=”grdDummy” runat=”server” /> <!–Markup–>

//Code-behind
List persons = new List();
persons.Add(
new Person()
{
FirstName = “N”,
LastName = “B”,
Address = new Address() {ApartmentName = “GHS”, PINCODE = “3700037”}
}
);
persons.Add(
new Person()
{
FirstName = “G”,
LastName = “B”,
Address = new Address() { ApartmentName = “SF”, PINCODE = “3700037” }
}
);

grdDummy.DataSource = persons;
grdDummy.DataBind();

This is where we need to start customizing GridView with Bound Fields and Template Fields, and use Eval method to evaluate a data binding expression that contains references to the properties of the object association as shown below

<asp:GridView ID=”grdDummy” runat=”server” AutoGenerateColumns=”False”>
            <Columns>
                <asp:BoundField DataField=”FirstName” HeaderText=”FirstName” />
                <asp:BoundField DataField=”LastName” HeaderText=”LastName” />
                <asp:TemplateField HeaderText=”ApartmentName”>
                    <ItemTemplate>
                        <%# Eval(“Address.ApartmentName”) %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText=”PINCODE”>
                    <ItemTemplate>
                        <%# Eval(“Address.Pincode”) %>                   
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>

This may be an elementary technique and there are better ones too, especially with ASP.NET MVC but I still thought of sharing it thanks to my reduced frequency of blogging 🙂 .

(P.S. I have seen many implementations creating page / UI specific data classes, pulling data from domain entities, for making UI binding easier)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: