<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alson Kemp &#187; control</title>
	<atom:link href="http://www.alsonkemp.com/tag/control/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alsonkemp.com</link>
	<description>Hackfoofery</description>
	<lastBuildDate>Thu, 05 Jan 2012 03:53:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.NET vs Rails: Controls and Controllers</title>
		<link>http://www.alsonkemp.com/aspnet/aspnet-vs-rails-controls-and-controllers/</link>
		<comments>http://www.alsonkemp.com/aspnet/aspnet-vs-rails-controls-and-controllers/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 16:52:45 +0000</pubDate>
		<dc:creator>alson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Ruby On rails]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.alsonkemp.com/?p=18</guid>
		<description><![CDATA[Since I&#8217;ve used Ruby on Rails quite a bit in the past and since I&#8217;m now using ASP.NET, I often find myself doing comparisons between the two frameworks.  Recently, I found myself comparing Rails&#8217; controllers/views and ASP.NET&#8217;s controls.  The following is an example of where ASP.NET&#8217;s declarative instantiation/configuration of controls worked well.  I&#8217;m not sure [...]]]></description>
			<content:encoded><![CDATA[<p>Since I&#8217;ve used Ruby on Rails quite a bit in the past and since I&#8217;m now using ASP.NET, I often find myself doing comparisons between the two frameworks.  Recently, I found myself comparing Rails&#8217; controllers/views and ASP.NET&#8217;s controls.  The following is an example of where ASP.NET&#8217;s declarative instantiation/configuration of controls worked well.  I&#8217;m not sure how I could do the same in Rails.</p>

<p>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 &lt;UserControl:GoogleAdManager /&gt; 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:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script src=&quot;http://partner.googleadservices.com/gampad/google_service.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GS_googleAddAdSenseService<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;publisher-code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
GS_googleEnableAllServices<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleAddSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'publisher-code'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slot1-code'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
GA_googleAddSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'publisher-code'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slot2-code'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleFetchAds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
&lt;!-- Goes in the proper location --&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleFillSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slot1-code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
&lt;!-- Goes in the proper location --&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleFillSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slot1-code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>


<hr />

<p>Optimally, I&#8217;d like to generate that code by placing two controls in my markup:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #339933;">&lt;</span>UserControls<span style="color: #339933;">:</span>GoogleAdManager Slot<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;slot1&quot;</span> runat<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;server&quot;</span> <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>... <span style="color: #660066;">lots</span> of HTML ... <span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>UserControls<span style="color: #339933;">:</span>GoogleAdManager Slot<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;slot2&quot;</span> runat<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;server&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div>


<p>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&#8217;d really like to know which ads are on the page <em>before</em> we render the page.  In Rails, I think that we&#8217;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&#8230;  So maybe we&#8217;d drop all the logic down into Javascript and use Javascript&#8217;s implicit page load events to fire the right bits of code at the right time.  Neither of these is pretty.</p>

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

<p>In the Page_Load event for each control, we try to insert the following code.  Of course, once one control has inserted (&#8220;this.Page.ClientScript.RegisterClientScriptInclude&#8221;) the javascript, the other control won&#8217;t insert it.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script src=&quot;http://partner.googleadservices.com/gampad/google_service.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GS_googleAddAdSenseService<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;publisher-code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
GS_googleEnableAllServices<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span></pre></div></div>


<p>Then on PageLoad each control add its slot code.  Control 1:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">GA_googleAddSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'publisher-code'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slot1-code'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>Control 2:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">GA_googleAddSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'publisher-code'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'slot2-code'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


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


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleFetchAds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>


<p>Finally, each control renders its code into the div.  Control 1:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleFillSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slot2-code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>


<p>Control 2:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
GA_googleFillSlot<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slot2-code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>


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

<p>Note: this may have been what Rails&#8217; components did.  But they got <a href="http://6brand.com/rails-components-abandoned">canned</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alsonkemp.com/aspnet/aspnet-vs-rails-controls-and-controllers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

