<?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>Gigu's blog &#187; .net</title>
	<atom:link href="http://blog.gigoo.org/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gigoo.org</link>
	<description>wEBbLOG</description>
	<lastBuildDate>Thu, 01 Jul 2010 22:59:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A same cup of Java &#8230;</title>
		<link>http://blog.gigoo.org/2010/07/01/a-same-cup-of-java/</link>
		<comments>http://blog.gigoo.org/2010/07/01/a-same-cup-of-java/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 22:59:21 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[stuff]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[languages]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/2010/07/01/a-same-cup-of-java/</guid>
		<description><![CDATA[… at IKEA.
&#160;
Recently I changed the job. After three years (longest I ever worked for a single company) I left ThoughtWorks. Majority of my consultant career oscillated in .NET space. It was almost three years since last I was doing Java. New job … is Java (packed full of Java technologies). 
I had to do [...]]]></description>
			<content:encoded><![CDATA[<p>… at IKEA.</p>
<p>&#160;</p>
<p><img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; float: left; border-top-width: 0px; border-left-width: 0px" border="0" align="left" src="http://stacyprowell.com/blog/wp-content/uploads/2009/04/java_logo_21.png" width="123" height="163" />Recently I changed the job. After three years (longest I ever worked for a single company) I left ThoughtWorks. Majority of my consultant career oscillated in .NET space. It was almost three years since last I was doing Java. New job … is Java (packed full of Java technologies). </p>
<p>I had to do some IKEA shopping one late afternoon after work (something for the house). As my journey turned out to be a waste of time (items out of stock) I was sitting in front of the IKEA building consuming a horrible £1.40 Hot Dog Menu 2. Watching the sun set I had a little epiphany! I started new job, I got dropped into source code and I was able to work without relearning Java, libraries and tools. </p>
<p>Then I realised that through three years of work at TW, C# and .NET changed it’s shape a lot. In a mean time Java did nothing,well … almost nothing. C# introduced new language syntax, features, influenced by dynamic and functional languages. Java had got annotations and generics (somehow weird generics).</p>
<p>Is that a bed thing? It gives stable platform, and more consistent syntax in legacy code. On the other hand it lacks cool features, and I do have to admit, I like new features. It’s like every time when Apple releases new iPhone instead of all cool new things on it all you get is better performance and stability <img src='http://blog.gigoo.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>There is also the explosion of languages that run on JVM. I like <a href="http://groovy.codehaus.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/groovy.codehaus.org');" target="_blank">Groovy</a> and <a href="http://jruby.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/jruby.org');" target="_blank">JRuby</a>, I need to check out <a href="http://clojure.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/clojure.org');" target="_blank">Clojure</a> and <a href="http://www.scala-lang.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.scala-lang.org');" target="_blank">Scala</a>. </p>
<p>There was a lot of fear when Oracle purchased Sun. Next new release of Java will have closures. If it is caused by Oracle’s influence I hope for more to come.</p>
<p>It is about time for Java to make some tuff decisions and stop being 100% backwards compatible.</p>
<p>What you think? Comment me your thoughts.</p>
<p>Cheers, Greg</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2010/07/01/a-same-cup-of-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to use simulators during web development</title>
		<link>http://blog.gigoo.org/2010/03/23/how-to-use-simulators-during-web-development/</link>
		<comments>http://blog.gigoo.org/2010/03/23/how-to-use-simulators-during-web-development/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 07:33:36 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[stuff]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[practices]]></category>
		<category><![CDATA[simulators]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/?p=4927</guid>
		<description><![CDATA[This is probably two topics that I would like to combine into one, as they are related in the context I will be writing about.
In one of the previous articles I wrote about the story wall that we evolved on our project. Christian in his article, described the way we worked not in pairs but [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-thumbnail wp-image-4929" title="Ski Simulator" src="http://blog.gigoo.org/wp-content/uploads/2010/03/ski-simulator-01-02-09-150x150.jpg" alt="Ski Simulator" width="150" height="150" />This is probably two topics that I would like to combine into one, as they are related in the context I will be writing about.</p>
<p>In one <a title="Revolution-evolution of a story wall" href="http://blog.gigoo.org/2010/02/15/revolution-evolution-of-a-story-wall/"  target="_self">of the previous articles</a> I wrote about the story wall that we evolved on our project. <a title="Threesomes" href="http://christianralph.blogspot.com/2010/02/menage-trois-in-kinky-teams.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/christianralph.blogspot.com');" target="_blank">Christian in his article</a>, described the way we worked not in pairs but in <strong>threesomes</strong>. Two developers and a QA. This was the way that involved iterative, small-steps, story delivery with constant showcasing to QA. QA was able to instantly check the correctness of a functionality, business logic, even a site layout and provide feedback to devs about it.</p>
<p>As any application (at least majority of them) our application has multiple points of integration to other systems. Database, web services, file system etc. One of the integration points was delivered some time ago and never tested, we were ready for a problems and bugs.</p>
<p>It would be very unwise to stop development because the part of the system needs fixing or some rework carried. We decided that we will shield our selfs with the layer of wrappers around third party systems , that we called <strong>Anti Corruption Layer</strong>.</p>
<p>The Layer gives as a constant API controlled by ourselves but it doesn’t mean that we can continue our way of working and constantly showcase all the acceptance criteria to our QAs and BAs.</p>
<p>We decided to bring on <strong>simulators</strong> on a board and hooking them into our anti corruption layer. This is how we achieved it:</p>
<h3>Control</h3>
<p>It would be very painful to switch simulator on or off using configuration in one of the file. Knowledge about the environment was not sufficient enough as we don’t want simulators to be on or off all the time. What we decided was to create a class called <em><strong>SimulatorDecider</strong></em> that will use two variables to determine if the Simulator should be on or off: current environment and a browser cookie.</p>
<p>The environment variable allowed us to switch simulators off regardless of the cookie, in any environment other than DEV or TEST.</p>
<p>Cookie in web browser is very simple to set and to remove. We created a little page called <em><strong>Cookie Monster</strong></em> that has a simple on/off buttons for setting and removing the Simulator cookie.<br />
The approach gives a possibility to control and switch on/off different parts of the system by using different cookies for each parts.</p>
<h3>Simulator</h3>
<p>We have a bunch of wrappers around the integration points. The one that we are interested in, the one that we would like to simulate, we decorate with Configurable object and inject SimulatorDecider into it. This is how it works:</p>
<pre><code>
interface IFoo
{
  ReturnType DoStuff(ParameterType type);
}

public class Foo : IFoo
{
  public ReturnType DoStuff(ParameterType type)
  {
    // Doing some real stuff that is very important
  }
}

public class SimulatedFoo : IFoo
{
  public ReturnType DoStuff(ParameterType type)
  {
    // Doing some other stuff that is only SIMULATED
  }
}

public class ConfigurableFoo : IFoo
{
  private SimulatorDecider _simulatorDecider;

  public SimulatedFoo(SimulatorDecider simulatorDecider, IFoo realFoo, IFoo simulatedFoo)
  {
    _simulatorDecider = simulatorDecider;
  }
  public ReturnType DoStuff(ParameterType type)
  {
    if (_simulatorDecider.ShouldSimulate())
    {
      return _simulatedFoo.DoStuff(type);
    }
    return _realFoo.DoStuff(type);
  }
}
</code></pre>
<p>Because we are using <strong>dependency injection container</strong> <a title="Yadic" href="http://code.google.com/p/yadic/" onclick="javascript:pageTracker._trackPageview('/outbound/article/code.google.com');" target="_blank">(Yadic)</a> we don’t need to worry about dependencies.<br />
It is also possible to not code <em><strong>SimulatedFoo</strong></em> as separate type and just inline simulated behavior within the configurable type. We made this decision on a base of how complex the simulated behavior should be.</p>
<p>Hope you find this useful when you stuck on integration pice that you don’t know how to carry on <img src='http://blog.gigoo.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Comments are welcome as always <img src='http://blog.gigoo.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Greg</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2010/03/23/how-to-use-simulators-during-web-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to deal with nulls</title>
		<link>http://blog.gigoo.org/2010/03/18/how-to-deal-with-nulls/</link>
		<comments>http://blog.gigoo.org/2010/03/18/how-to-deal-with-nulls/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 07:30:08 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[stuff]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/?p=4912</guid>
		<description><![CDATA[This week I’ve been refactoring and improving some code in a .Net world. In last few weeks I also had some close encounters with Java as well. When looking at the code in both environments I realized that there is a lot of checking for null values on in a code. Where it is not [...]]]></description>
			<content:encoded><![CDATA[<p>This week I’ve been refactoring and improving some code in a <strong>.Net</strong> world. In last few weeks I also had some close encounters with <strong>Java</strong> as well. When looking at the code in both environments I realized that there is a lot of checking for <strong>null</strong> values on in a code. Where it is not a bad practice to check for null it actually presents another problem.</p>
<p>For someone like me when I was new to a code I couldn’t guess, what is the goal of that check. When you dealing with <strong>null</strong> the very important thing is to know where they came from and why it happened that null values occurred. When dealing with integration points it is even more important.</p>
<p>As you can guess code:</p>
<pre>if (serverResponse == null)
{
    DoSomeStuff();
}</pre>
<p>is not very descriptive. What actually happend when I got null, does it mean that there was a communication problem, error occurred or perhaps a null means that the operation was successful.</p>
<p>I think the best approach will be to avoid nulls if it is possible at all. Ways we could try to avoid it:<br />
•   <strong> use Maybe pattern<br />
•   create Empty object values<br />
•   use Null Object pattern</strong></p>
<h3>Maybe pattern</h3>
<p>I’m not sure if that is it’s appropriate name but I’ve used it on few projects and it seems like people are referring to it this way.</p>
<p>Have a Maybe interface and return it instead of the object itself.</p>
<pre>interface Maybe&lt;T&gt;
{
    bool Hasvalue;
    T Value;
}</pre>
<p>Also have two implementation of the interface:</p>
<pre class="csharp">public class Something&lt;T&gt;
{
    private readonly T _value;
    public Something(T value)
    {
        _value = value;
    }

    bool HasValue { get { return true; }  }
    T Value { get { return value; } }
}

public class Nothing&lt;T&gt;
{
    bool HasValue { get { return false; } }
    T Value { get { throw new InvalidOperationException(); } }
}</pre>
<p>When returning value from an object return Maybe. Collaborator that consumes value can check if it is Something or Nothing or just ask if it has value.</p>
<h3>Empty Object value</h3>
<p>Just like string.Empty you can implement Empty or something similar as predefined value on your type. For example:</p>
<pre>class Hen
{
    Egg LayEgg()
    {
        // doing stuff
        return Egg.Empty;
    }
}

class Egg
{
    public static Egg Empty = new Egg(“nothing in it”);
}</pre>
<p>You can always check before the execution if the egg is empty.</p>
<h3>Null object pattern</h3>
<p>Object with no default behavior. For example:</p>
<pre>interface TV
{
void ShowMovie();
}

class LCD : TV
{
    void ShowMovie()
    {
        // showing movie
    }
}

class Plasma : TV
{
    void ShowMovie()
    {
        // showing movie
    }
}

class NullTV : TV
{
    void ShowMovie()
    {
        // do nothing
    }
}</pre>
<p>If something is not working well and we don’t have any of the TVs available we could return NullTV and nothing will happen.<br />
Using the patter we can validate by type if the returned TV is a valid object and act accordingly.</p>
<p>I’m interested what other approaches people are heaving. Also what are the approaches in dynamic languages like Python or Ruby.</p>
<p><span style="text-decoration: underline;"><strong>Please add your comment <img src='http://blog.gigoo.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </strong></span></p>
<p>Greg</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2010/03/18/how-to-deal-with-nulls/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Verify JavaScript with JSLint during build using Nant</title>
		<link>http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/</link>
		<comments>http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 13:39:06 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[nant]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/</guid>
		<description><![CDATA[ Project I’m working on has a lot of JavaScript. We are minimizing JavaScript files during the build and combining them into one file. Problem is when someone checks in some invalid JavaScript into repository that doesn’t minify properly. As a result you can end up with not working JavaScript at all. There is a [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="display: inline; margin: 0px 5px 0px 0px" height="40" alt="image" src="http://blog.gigoo.org/wp-content/uploads/2009/03/image1.png" width="129" align="left" /> Project I’m working on has a lot of JavaScript. We are minimizing JavaScript files during the build and combining them into one file. Problem is when someone checks in some invalid JavaScript into repository that doesn’t minify properly. As a result you can end up with not working JavaScript at all. There is a tool called <a title="JSLint" href="http://www.jslint.com/lint.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank">JSLint</a>. It is created by <strong><em>Douglas Crockford</em></strong> who is also the author of a very good book about JavaScript: “<em><strong><a href="http://www.amazon.com/exec/obidos/ASIN/0596517742/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.amazon.com');" target="_blank">JavaScript: The Good Pars</a></strong></em>”.</p>
<p><a title="JSLint" href="http://www.jslint.com/lint.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank">JSLint</a> is a very strict JavaScript verifier. JS files corrected to a JSLint specification could be minified with confidence. </p>
<p>We made the decision that build should brake when <a title="JSLint" href="http://www.jslint.com/lint.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank">JSLint</a> detects invalid JavaScript file, so developer will need to fix it before the code checkin.</p>
<h2>Prepare the tool base</h2>
<p>As a first step we need to get all the tools that will execute JSLint validation against our codebase. You can grab <a href="http://www.jslint.com/fulljslint.js" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank"><strong><em>fulljslint.js</em></strong></a> from the JSLint home page. There is no executable binary for JSLint, it is just a JavaScript file with set of rules and function to execute verification. Our working environment is Windows we can execute validation using <strong><em>cscript.exe</em></strong> command line tool that Windows has. There is a very useful script created by <strong><em>Jason Diamond </em></strong>that helps to execute JSLint validation. You can get it from from here: <a href="http://jason.diamond.name/downloads/jslint.wsf" onclick="javascript:pageTracker._trackPageview('/outbound/article/jason.diamond.name');" target="_blank"><strong><em>jslint.wsf</em></strong></a><strong><em>. </em></strong>One last thing is a command line batch file that helps execute this tool. You can name it whatever you would like to. I named it <strong><em>jslint.bat. </em></strong>The content of it looks like this:</p>
<blockquote><pre><strong>@cscript //nologo %~dp0\jslint.wsf %*</strong></pre>
</blockquote>
<p>I put all those three files into build tree, in tools folder. You can check if all the files are valid and working properly by running this command from command line:</p>
<blockquote>
<p><strong>jslint.bat c:\path\to\your\js\file.js</strong></p>
</blockquote>
<h2>Create a build target</h2>
<p>Now that we have all the tools we can create a build target. We are using <strong><em><a title="Nant build tool" href="http://nant.sourceforge.net/" onclick="javascript:pageTracker._trackPageview('/outbound/article/nant.sourceforge.net');" target="_blank">Nant</a></em></strong> as a build tool. I created Nant target and called it jslint. It looks like this.</p>
<blockquote>
<p><strong>&lt;target name=&quot;jslint&quot; description=&quot;validates JS files&quot;&gt;<br />
      <br />&#160;&#160;&#160; &lt;foreach item=&quot;File&quot; property=&quot;jsfile&quot;&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;in&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;items&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;include name=&quot;path\to\scripts\folder\**\*.js&quot; /&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/items&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/in&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;do&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;exec program=&quot;jslint.bat&quot; commandline=&quot;${jsfile}&quot; /&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/do&gt; </p>
<p>&#160;&#160;&#160; &lt;/foreach&gt; </p>
<p>&lt;/target&gt;</strong></p>
</blockquote>
<p>There is a lot of stuff that could be turned on/off during validation. If you type <strong><em>jslint.bat </em></strong>without any parameters it will give you a list of possible options. Those could as well be tweaked in the <a href="http://jason.diamond.name/downloads/jslint.wsf" onclick="javascript:pageTracker._trackPageview('/outbound/article/jason.diamond.name');" target="_blank"><strong><em>jslint.wsf</em></strong></a> file.</p>
<p>Hope this helps you as it did for us.</p>
<p>Some says that JSLint might hurt your feelings. I say, bring it on <img src='http://blog.gigoo.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Gregster</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom JavaScript KungFu and management for Microsoft MVC.NET</title>
		<link>http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/</link>
		<comments>http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 13:09:15 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/</guid>
		<description><![CDATA[ Rails community got a lot things right for web application. One of them is the way they manage JavaScript files. I’m working on a web project where I use a lot of JavaScript and MVC.NET as Web Framework. Team is quite big and JavaScript is getting out of control. Files are everywhere with a [...]]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; margin: 0px 5px 0px 0px" src="http://blog.gigoo.org/wp-content/uploads/2009/02/image-thumb.png" align="left" /> Rails community got a lot things right for web application. One of them is the way they manage JavaScript files. I’m working on a web project where I use a lot of JavaScript and MVC.NET as Web Framework. Team is quite big and JavaScript is getting out of control. Files are everywhere with a lot of code. And then when it goes into production it is the best to have it as one compressed and minified file. So, here what we did.</p>
<h3>Where am I, ups, production, I better behave</h3>
<p>The easiest possible way of telling the app where it is running will be in <strong>Web.config</strong>. There is already a bunch of custom setting in there, so why not just have one more. When the time comes based on the <strong>Environment </strong>information we can decide if we will still spit out single un-minified files for a development environment or serve one minified file for lightning fast production performance. </p>
<p> <span id="more-4784"></span><br />
<h3>Help me, help me, Mr Helper</h3>
<p>So, the idea with the helper is: we use it to include every single file that we need on our page. When it knows that we are on Production it will replace imports with one, plus potentially minify it and compress. So I created a class called <strong>JSHelper</strong>.</p>
<p>JSHelper will contain two methods, one will kind of register the file for inclusion, and the other will do the actual job. So, the C# code goes somewhat like this:</p>
<blockquote><p><em>public class <strong>JSHelper</strong>         <br />{         <br />&#160;&#160;&#160;&#160; private readonly <strong>IList</strong>&lt;<strong>string</strong>&gt; listOfFilesToInclude = new <strong>List</strong>&lt;<strong>string</strong>&gt;();         <br />&#160;&#160;&#160;&#160; public const <strong>string</strong> KEY = &quot;JSHelper_Key&quot;;</em></p>
<p><em>&#160;&#160;&#160;&#160; public void IncludeJs(<strong>string</strong> fileName)         <br />&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!listOfFilesToInclude.Contains(fileName))         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; listOfFilesToInclude.Add(fileName);         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br />&#160;&#160;&#160;&#160; }</em></p>
<p><em>&#160;&#160;&#160;&#160; public <strong>string</strong> IncludeAllJs()         <br />&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var scriptRoot = <strong>VirtualPathUtility</strong>.ToAbsolute(&quot;~/Scripts&quot;);         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var scriptFormat = &quot;&lt;script src=\&quot;{0}/{1}\&quot; type=\&quot;text/javascript\&quot;&gt;&lt;/script&gt;\r\n&quot;;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var importedJavaScripts = &quot;&quot;;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach (var fileName in listOfFilesToInclude)         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; importedJavaScripts += <strong>string</strong>.Format(scriptFormat, scriptRoot, fileName);         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return importedJavaScripts;         <br />&#160;&#160;&#160;&#160; }         <br />}</em></p>
</blockquote>
<p>So, as you can see it will also eliminate potential errors of including the same JS file twice.</p>
<p>Now to solve next problem. The <strong>JSHelper </strong>needs to be a single instance per <strong>Web Request</strong>. It needs to be as it holds the state of all the files to be imported. Having a static means that it is shared across all web requests, not good. It has to be an instance for a every server thread. Controllers are save place for our helper to be, plus they can put stuff to a <strong>ViewData. </strong>As you might or not be aware this is accessible in <strong>aspx </strong>View template.</p>
<h3></h3>
<h3>Take over Control</h3>
<p>In MVC.NET there is base class for all controllers that every controller inherits. It is called <strong>Controller. </strong>Because we got some more stuff to setup in all our controller we created our own that extends base framework controller and every controller in our application extends it. This is a great place to put our JSHelper in. Code goes something like that:</p>
<blockquote><p><em>public abstract class <strong>ApplicationController</strong> : </em><em><strong>Controller          <br /></strong>{         <br />&#8230;         <br />&#160;&#160;&#160;&#160; protected override void OnActionExecuting(<strong>ActionExecutingContext</strong> filterContext)         <br />&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ViewData[<strong>JSHelper</strong>.KEY] = new <strong>JSHelper</strong>();         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>base</strong>.OnActionExecuting(filterContext);         <br />&#160;&#160;&#160;&#160; }         <br /></em><em>…        <br /></em><em>}</em></p>
</blockquote>
<p>This code will create new instance of <strong>JSHelper </strong>and put it into your <strong>ViewData</strong> for usage. Because we also extend MVC.NET base class we can have JSHelper accessible in every View we got in the application.</p>
<h3>My VIEW of the world</h3>
<p>So the code goes something like this:</p>
<blockquote><p><em>public class <strong>BaseViewPage</strong>&lt;<strong>TModel</strong>&gt; : <strong>ViewPage</strong>&lt;<strong>TModel</strong>&gt; where <strong>TModel</strong> : class         <br />{         <br />&#160;&#160;&#160; public <strong>JSHelper</strong> JSHelper         <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return (<strong>JSHelper</strong>) ViewData[<strong>JSHelper</strong>.KEY]; }         <br />&#160;&#160;&#160; } </em></p>
<p><em>}</em></p>
</blockquote>
<p>Something similar I done for the <strong>Master Page</strong>. This way we could combine all JavaScripts into one place.</p>
<p>So, the example of a View with JSHelper looks like this.</p>
<h4>Master page</h4>
<blockquote><p><em>…</em></p>
<p><em>&lt;head&gt;        <br />&lt;% JSHelper.IncludeJs(&quot;jquery.js&quot;);%&gt;         <br />&lt;% JSHelper.IncludeJs(&quot;other-awesome-library.js&quot;); %&gt;         <br /></em></p>
<p><em>&lt;asp:ContentPlaceHolder ID=&quot;headContent&quot; runat=&quot;server&quot; /&gt;        <br /></em></p>
<p><em>&lt;%= JSHelper.IncludeAllJs() %&gt;        </p>
<p>&lt;/head&gt;         </p>
<p>&lt;body&gt;</em></p>
<p><em>…</em></p>
</blockquote>
<h4>View</h4>
<blockquote><p>…</p>
<p>&lt;asp:Content ContentPlaceHolderID=”headContent” runat=”server”&gt;      </p>
<p>&lt;% JSHelper.IncludeJs(&quot;blabla.js&quot;);%&gt;      <br />&lt;% JSHelper.IncludeJs(&quot;hm-hm.js&quot;);%&gt;</p>
<p>&lt;/asp:Content&gt;</p>
<p>…</p>
</blockquote>
<h3>Final pieces</h3>
<p>Now we can put into use our <strong>Web.config </strong>file<strong> </strong>and change <strong>IncludeAllJs </strong>method to serve files based on environment we are in. Not posting implementation as it could be achieved in many ways. </p>
<p>So, when we are serving JS on production environment we can serve one single file prepared before as a deployment task or we can dynamically prepare it by pointing web browser to another controller that will combine all the files and minified it on a flight. </p>
<p>You can look at some JavaScript code minification in here: <a title="JavaScript minification" href="http://www.crockford.com/javascript/jsmin.cs" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.crockford.com');" target="_blank">http://www.crockford.com/javascript/jsmin.cs</a></p>
<p>Let me know if you find this ideas useful.</p>
<p>Cheerios, Greg</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
