views:

66

answers:

1

I'm trying to reduce the code duplication that exists throughout my asp.net web forms. This is what an example object loaded from the database looks like.

Apartment
  int id
  decimal rent
  bool callForPricing
  int squareFeet
  int beds
  int baths

Now I create views from this object in several different asp.net pages (ie. a list with several apartments, a detail view, etc). In the past what I have done is create another class that wraps the Apartment class. Something like this...

ApartmentView

  Apartment apt

  public virtual String Rent
  {
     get 
     { 
        if (apt.CallForPricing)
        {
           return "Call For Pricing"; 
        }else{
           return apt.Rent.ToString("C") + "/Month";
        }
     }
  }

  public virtual String BedsBathsSqFt
  {
     get 
     { 
        if (apt.squareFeet > 0)
        {
           return apt.beds + " Beds|" + apt.beds + " Beds|" + apt.SquareFeet + " sqft"; 
        }else{
           return apt.beds + " Beds|" + apt.beds + " Beds";
        }
     }
  }

As you can see I'm usually just creating string representations of the data. I have considered having the ApartmentView class extend the Apartment class, but haven't because of overlapping properties like 'Rent'. I'm just wondering how people normally deal with this situation. Is this the correct naming convention to use?

+1  A: 

This is a tough dilemma to work through on any webforms project. You have two choices for the conditional formatting logic - in the page itself (now you have to duplicate it all over the site), or in some code behind like you're doing. Neither are great options in terms of separation of concerns, it's a well known drawback of the ASP.Net webforms model.

Personally I'd turn your view into an .ascx web user control that contains a FormView control and databind it to your apartment wrapper object. Something like :

<asp:FormView ID="FormView1"
  DataSourceID="ObjectDataSource1"
  RunAt="server">

  <ItemTemplate>
    <table>
      <tr>
        <td align="right"><b>Rent:</b></td>       
        <td><%# Eval("Rent") %></td>
      </tr>
      <tr>
        <td align="right"><b>BedsBathsSqFt:</b></td>     
        <td><%# Eval("BedsBathsSqFt") %></td>
      </tr>
    </table>                 
  </ItemTemplate>                 
</asp:FormView>

Have the .ascx expose the DataSource property so it could be set by the page that's using it.

If Databinding was more flexible with conditional expressions, you could do away with the apartment wrapper object and embed the conditions directly in the user control. With the complexity of your properties though, that will probably be a big headache. However, you could look at something like this, where people have attempted to get around those limitations.

womp