Hackfoofery

Alson Kemp

Archive for the ‘view’ tag

ASP.NET vs Rails: Controls and Controllers

without comments

Since I’ve used Ruby on Rails quite a bit in the past and since I’m now using ASP.NET, I often find myself doing comparisons between the two frameworks.  Recently, I found myself comparing Rails’ controllers/views and ASP.NET’s controls.  The following is an example of where ASP.NET’s declarative instantiation/configuration of controls worked well.  I’m not sure how I could do the same in Rails.

We had to insert Google Ad Manager code into a number of our web pages with varying numbers of ads on each page.  We really wanted to be able to declaratively configure the controls, so that inserting a <UserControl:GoogleAdManager /> tag or two or three into a page would correctly generate the Javascript to place the ads.  The Javascript for two slots looks something like this:

<script src="http://partner.googleadservices.com/gampad/google_service.js" type="text/javascript"></script>
<script type="text/javascript">
GS_googleAddAdSenseService("publisher-code");
GS_googleEnableAllServices();
</script>
<script type="text/javascript">
GA_googleAddSlot('publisher-code', 'slot1-code');
GA_googleAddSlot('publisher-code', 'slot2-code');
</script>
<script type="text/javascript">
GA_googleFetchAds();
</script>
 
<!-- Goes in the proper location -->
<script type="text/javascript">
GA_googleFillSlot("slot1-code");
</script>
 
<!-- Goes in the proper location -->
<script type="text/javascript">
GA_googleFillSlot("slot1-code");
</script>

Optimally, I’d like to generate that code by placing two controls in my markup:

  <UserControls:GoogleAdManager Slot="slot1" runat="server" />
  <... lots of HTML ... >
  <UserControls:GoogleAdManager Slot="slot2" runat="server" />

Doing this in Rails would be a bit difficult: we only know which ads are being included after we parse the page, but we only parse the page in order to render the page.  We’d really like to know which ads are on the page before we render the page.  In Rails, I think that we’d have to do this by creating an array of ads up in the controller and then rendering them in the View using a helper.  But now the controller is responsible for knowing which HTML elements should be on a page…  So maybe we’d drop all the logic down into Javascript and use Javascript’s implicit page load events to fire the right bits of code at the right time.  Neither of these is pretty.

The key to doing this in ASP.NET is ASP.NET’s page event model in which pages and control go through 3 or so phases before they’re actually rendered.  We wound up breaking the Javascript generation as follows.

In the Page_Load event for each control, we try to insert the following code. Of course, once one control has inserted (“this.Page.ClientScript.RegisterClientScriptInclude”) the javascript, the other control won’t insert it.

<script src="http://partner.googleadservices.com/gampad/google_service.js" type="text/javascript"></script>
<script type="text/javascript">
GS_googleAddAdSenseService("publisher-code");
GS_googleEnableAllServices();
</script>
<script type="text/javascript">

Then on PageLoad each control add its slot code. Control 1:

GA_googleAddSlot('publisher-code', 'slot1-code');

Control 2:

GA_googleAddSlot('publisher-code', 'slot2-code');

On PreRender, each control tries to add the following code to close off the previous script tag and to fetch the ads:

</script>
<script type="text/javascript">
GA_googleFetchAds();
</script>

Finally, each control renders its code into the div. Control 1:

<script type="text/javascript">
GA_googleFillSlot("slot2-code");
</script>

Control 2:

<script type="text/javascript">
GA_googleFillSlot("slot2-code");
</script>

Gotta dip down into C# a bit for the ASP.NET, but the pay-off is a simple declarative configuration of Google’s Ad Manager. I didn’t implement the functionality in Rails, but I suspect that it would be rather more complicated… ASP.NET wins this round… but I still miss Rails something fierce…

Note: this may have been what Rails’ components did. But they got canned.

Written by alson

August 20th, 2008 at 12:52 pm

Posted in ASP.NET,Ruby On rails

Tagged with , ,