<?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>Phalanx Blogosphere &#187; Mehmetali Shaqiri</title>
	<atom:link href="http://phalanx.spartansoft.org/author/m-shaqiri/feed/" rel="self" type="application/rss+xml" />
	<link>http://phalanx.spartansoft.org</link>
	<description>Whatever happens, SPARTAN&#039;S code must stand ... Or at least crash responsibly.</description>
	<lastBuildDate>Sat, 02 Jul 2011 05:19:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>IIS as Reverse Proxy for CouchDB</title>
		<link>http://phalanx.spartansoft.org/2011/07/02/iis-as-reverse-proxy-for-couchdb/</link>
		<comments>http://phalanx.spartansoft.org/2011/07/02/iis-as-reverse-proxy-for-couchdb/#comments</comments>
		<pubDate>Sat, 02 Jul 2011 05:19:19 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[CouchDB]]></category>
		<category><![CDATA[ExtJs]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[Application Request Routing]]></category>
		<category><![CDATA[Reverse Proxy]]></category>
		<category><![CDATA[URL Rewrite Module]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1289</guid>
		<description><![CDATA[For the last couple of days, I&#8217;ve been using CouchDB and ExtJS on a project I&#8217;m currently working on. For those unfamiliar with CouchDB, it is a free and open source document-oriented database written in the Erlang programming language. It is designed for local replication and to scale vertically along a wide range of devices. Provides [...]]]></description>
			<content:encoded><![CDATA[<p>For the last couple of days, I&#8217;ve been using <a title="CouchDB" href="http://couchdb.apache.org/" target="_blank">CouchDB </a>and <a title="ExtJS" href="http://www.sencha.com/products/extjs" target="_blank">ExtJS </a>on a project I&#8217;m currently working on.</p>
<p>For those unfamiliar with <a title="CouchDB" href="http://couchdb.apache.org/" target="_blank">CouchDB</a>, it is a free and open source document-oriented database written in the Erlang programming language. It is designed for local replication and to scale vertically along a wide range of devices. Provides a RESTful JSON API than can be accessed from any environment that allows HTTP requests.</p>
<p><a title="ExtJS" href="http://www.sencha.com/products/extjs" target="_blank">ExtJS</a> is a cross-browser JavaScript library for building rich internet applications, using techniques such as AJAX, DHTML and DOM scripting.</p>
<p>I wanted to communicate to my CouchDB backend from HTML/Javascript frontend using <a title="Ext.data.Store" href="http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.Store" target="_blank">Ext.data.Store</a> (stores load data via a <a id="ext-gen1480" rel="Ext.data.proxy.Proxy" href="http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.proxy.Proxy">Proxy</a>, and also provide functions for <a id="ext-gen1482" rel="Ext.data.Store-method-sort" href="http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.Store-method-sort">sorting</a>, <a id="ext-gen1483" rel="Ext.data.Store-method-filter" href="http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.Store-method-filter">filtering</a> and querying the <a id="ext-gen1484" rel="Ext.data.Model" href="http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.Model">model</a> instances contained within it.) &#8230; one of the coolest features I love about ExtJs.</p>
<p>Since <a title="CouchDB" href="http://couchdb.apache.org/" target="_blank">CouchDB</a> runs on port 5984 (not on port <em>80) </em>and I was IIS 7 as a webserver (runs on port 80) and because of the <a href="http://en.wikipedia.org/wiki/Same_origin_policy" target="_blank">Same Origin Policy</a>, browsers reject requests to load a document or script unless the <strong>protocol</strong>, <strong>port </strong>and <strong>host </strong>are identical to the source of the current page. Therefore I was unable to talk with CouchDB database. There is one approach using <a href="http://all-docs.info/extjs4/docs/api/Ext.data.ScriptTagProxy.html" target="_blank">Ext.data.ScriptTagProxy</a>(used to load data from a domain other than the one your application is running on) but I didn&#8217;t like this approach because it messes up my code.</p>
<p>I wanted to use IIS web server as a reverse-proxy server that receives web requests and forwards them to CouchDB for processing. Whenever I&#8217;d issue a GET request for  &#8220;/couchdb/[database]/_design/products/_view/list&#8221; reverse-proxy server receives the request and forwards to CouchDB for processing &#8230; the re-written url would look like this: &#8220;http://localhost:5984/[database]/_design/products/_view/list&#8221;</p>
<p>The following figure illustrates a typical configuration for a reverse-proxy scenario:</p>
<p>&nbsp;</p>
<p><center><img title="Application Request Routing" src="http://phalanx.spartansoft.org/wp-content/uploads/2011/07/Application-Request-Routing.jpg" alt="Application Request Routing" width="283" height="312" /></center><br/><br/></p>
<h1><a>Application Request Routing and URL Rewrite to the rescue</a></h1>
<p>To implement the reverse-proxy server you need to install:</p>
<ol>
<li><a href="http://www.iis.net/download/urlrewrite" target="_blank">URL Rewrite  Module</a></li>
<li><a href="http://www.iis.net/download/ApplicationRequestRouting" target="_blank">Application Request Routing</a></li>
</ol>
<p>Then open IIS Manager and:</p>
<ol>
<li>Navigate to &#8220;Application Request Routing&#8221; feature<br/><br />
<center><img title="Application Request Routing Cache" src="http://phalanx.spartansoft.org/wp-content/uploads/2011/07/Application-Request-Routing-Cache.png" alt="" width="472" height="141" /></center></p>
</li>
<li>Check the &#8220;Enable Proxy&#8221;<br/><br />
<center><img  title="Application Request Routing" src="http://phalanx.spartansoft.org/wp-content/uploads/2011/07/Application-Request-Routing.png" alt="" width="437" height="224" /></center>
</li>
<li>
Under <b>system.webServer</b> section in web.config add the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>		
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rewrite<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rule</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Reverse Proxy to CouchDB&quot;</span> <span style="color: #000066;">stopProcessing</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;match</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;^couchdb/(.*)&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Rewrite&quot;</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://localhost:5984/{R:1}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rewrite<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.webServer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
</ol>
<p>So as you can see, we have added a new rule which matches all web requests that begin with <b>couchdb</b> and we rewrite them to http://localhost:5984/[whatever comes after <b>couchdb</b>].</p>
<p>By using IIS as reverse proxy for CouchDB, http://localhost/couchdb is equivalent to http://localhost:5984.</p>
<p>Hope this helps and saves you a couple of hours to figure it out.<br />
<br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2011/07/02/iis-as-reverse-proxy-for-couchdb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Applying where conditions for all select queries behind scenes &#8211; LINQ to SQL</title>
		<link>http://phalanx.spartansoft.org/2011/05/26/applying-where-conditions-for-all-select-queries-behind-scenes-linq-to-sql/</link>
		<comments>http://phalanx.spartansoft.org/2011/05/26/applying-where-conditions-for-all-select-queries-behind-scenes-linq-to-sql/#comments</comments>
		<pubDate>Wed, 25 May 2011 23:13:36 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1249</guid>
		<description><![CDATA[While discussing with a friend of mine, he was telling me about a project he was working on recently. Instead of deleting records from the table, he replaced the concept of delete with update &#8230; on every table he had an additional attribute Active (bit) which indicates whether the record is deleted or is active. Because [...]]]></description>
			<content:encoded><![CDATA[<p>While discussing with a friend of mine, he was telling me about a project he was working on recently. Instead of deleting records from the table, he replaced the concept of delete with update &#8230; on every table he had an additional attribute Active (bit) which indicates whether the record is deleted or is active. Because the application deals only with active records, this leads to a lot of repeated where conditions on repository methods, because for every select query you have to add an additional condition <strong>.Where(p =&gt; p.Active == true) </strong>which filters only active records. He was looking for a way to define this in the data context level (LINQ).</p>
<p>I&#8217;m not sure whether you can achieve this without creating a data context wrapper, because for each query you have to combine two logical expressions, the expression that comes from repository and <strong>p =&gt; p.Active == true</strong>.</p>
<p>I thought it was worth sharing with you this.</p>
<p>Assuming that we have the following State entity:</p>
<p><a href="http://phalanx.spartansoft.org/wp-content/uploads/2011/05/db_context.png"><img title="State" src="http://phalanx.spartansoft.org/wp-content/uploads/2011/05/db_context.png" alt="" width="218" height="164" /></a></p>
<p><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></p>
<p>This is how I would d solve this &#8230; I would simply have a generic class which is responsible for building expressions and issuing queries to the database.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// A generic class that executes queries againts a data context</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;Context&quot;&gt;The Database context&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;The entity&lt;/typeparam&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DataContextWrapper<span style="color: #008000;">&lt;</span>Context<span style="color: #008000;">&gt;</span> where Context <span style="color: #008000;">:</span> DataContext, <span style="color: #008000;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Context DataContext<span style="color: #008000;">;</span>
&nbsp;
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// The connection string</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #FF0000;">string</span> ConnectionString
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//usually this comes from configuration file (web.config / app.config)</span>
                <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;[ConnectionString]&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Class constructor that instantiates a new DataContext object and associates the connection string</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> DataContextWrapper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            DataContext <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Context<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            DataContext.<span style="color: #0000FF;">Connection</span>.<span style="color: #0000FF;">ConnectionString</span> <span style="color: #008000;">=</span> ConnectionString<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetItems<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>Optional<span style="color: #000000;">&#93;</span> Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;</span> query<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//get the entity type</span>
            Type entity <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//get all properties</span>
            PropertyInfo<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> properties <span style="color: #008000;">=</span> entity.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;</span> isRowActive <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">//we are interested in entities that have Active property ==&gt; to distinguish active rows</span>
            PropertyInfo property <span style="color: #008000;">=</span> entity.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>prop <span style="color: #008000;">=&gt;</span> prop.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Active&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SingleOrDefault</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//if the entity has the property</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>property <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//Create a ParameterExpression</span>
                <span style="color: #008080; font-style: italic;">//if the query is specified then we need to use a single ParameterExpression for the whole final expression</span>
                ParameterExpression para <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>query <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> Expression.<span style="color: #0000FF;">Parameter</span><span style="color: #000000;">&#40;</span>entity, property.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> query.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
                var len <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">PropertyOrField</span><span style="color: #000000;">&#40;</span>para, property.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                var body <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">Equal</span><span style="color: #000000;">&#40;</span>len, Expression.<span style="color: #0000FF;">Constant</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                isRowActive <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">Lambda</span><span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span>body, para<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>query <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//combine two expressions</span>
                var combined <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">AndAlso</span><span style="color: #000000;">&#40;</span>isRowActive.<span style="color: #0000FF;">Body</span>, query.<span style="color: #0000FF;">Body</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                var lambda <span style="color: #008000;">=</span> Expression.<span style="color: #0000FF;">Lambda</span><span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span>combined, query.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> DataContext.<span style="color: #0000FF;">GetTable</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>lambda<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isRowActive <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> DataContext.<span style="color: #0000FF;">GetTable</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>isRowActive<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> DataContext.<span style="color: #0000FF;">GetTable</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, we have a method <b>IEnumerable<T> GetItems<T>([Optional] Expression<Func<T, bool>> query)</b> which has an optional argument where you can pass lambda expressions.</p>
<p>And for every query, if the entity has the <b>Active</b> member, we append <b>p => p.Active == true</b> to the query. In this way we only specify it once, and we make sure that every method on our repositories, return only active records.</p>
<p>This is how a simple repository would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// States Repository</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> StatesRepository <span style="color: #008000;">:</span> DataContextWrapper<span style="color: #008000;">&lt;</span>DEMODataContext<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Get all active states</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;All active states&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>State<span style="color: #008000;">&gt;</span> GetStates<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">GetItems</span><span style="color: #008000;">&lt;</span>State<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Get all active states</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;pattern&quot;&gt;State pattern&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;All active states tha contain the given pattern&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>State<span style="color: #008000;">&gt;</span> GetStates<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> pattern<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">GetItems</span><span style="color: #008000;">&lt;</span>State<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>s<span style="color: #008000;">=&gt;</span>s.<span style="color: #0000FF;">Description</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>pattern<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The usage:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">StatesRepository repo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StatesRepository<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var activeStates <span style="color: #008000;">=</span> repo.<span style="color: #0000FF;">GetStates</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>And</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var filtered <span style="color: #008000;">=</span> repo.<span style="color: #0000FF;">GetStates</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Al&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2011/05/26/applying-where-conditions-for-all-select-queries-behind-scenes-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to MEF</title>
		<link>http://phalanx.spartansoft.org/2011/05/14/introduction-to-mef/</link>
		<comments>http://phalanx.spartansoft.org/2011/05/14/introduction-to-mef/#comments</comments>
		<pubDate>Sat, 14 May 2011 01:58:45 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[MEF]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[mef]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1166</guid>
		<description><![CDATA[It has been a while since I&#8217;ve written a blog post about, well anything &#8230; partially because I&#8217;ve been very busy and mainly because I&#8217;m a bit lazy. As you can see from the title of this post, I&#8217;ve intentionally decided to break this &#8220;huge hiatus&#8221; with MEF. One of the main reasons is because [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a while since I&#8217;ve written a blog post about, well anything &#8230; partially because I&#8217;ve been very busy and mainly because I&#8217;m a bit lazy. As you can see from the title of this post, I&#8217;ve intentionally decided to break this &#8220;huge hiatus&#8221; with MEF. One of the main reasons is because it&#8217;s the most exciting thing that I&#8217;ve used so far.</p>
<p>So here we go &#8230;</p>
<h1><a>What is MEF?</a></h1>
<p><a href="http://mef.codeplex.com" target="_blank"><img title="MEF - Managed Extensibility Framework" src="http://phalanx.spartansoft.org/wp-content/uploads/2011/05/MEF.png" alt="MEF - Managed Extensibility Framework" width="150" /></a></p>
<p><a href="http://mef.codeplex.com/" target="_blank">Managed Extensibility Framework (MEF)</a> is a new library in the .NET Framework 4 and in Silverlight 4 that simplifies the design of composable systems that can be extended by third parties after they have been deployed. It is all about enabling dynamic composition of components across your application, adding/removing classes/components from your running system dynamically without any changes to the running code.</p>
<p>MEF is most useful with dynamic dependencies known only at run-time and it is optimized for discovering unknown parts of the system. It is built for <strong>open systems</strong> – scenarios in which the complete set of components that make up an application is defined at run-time.</p>
<p>You can read more about MEF here:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/dd460648.aspx">Managed Extensibility Framework Overview</a></li>
<li><a href="http://mef.codeplex.com">MEF on CodePlex</a></li>
<li><a href="http://msdn.microsoft.com/en-us/magazine/ee291628.aspx">Building Composable Apps in .NET 4 with the Managed Extensibility Framework</a></li>
<li><a href="http://blogs.msdn.com/b/gblock/">Glenn Block</a> (the former PM of MEF team and now PM on the WCF team working on Microsoft’s future HTTP and REST stack).</li>
<li><a href="http://blogs.msdn.com/b/nblumhardt/">Nicholas Blumhardt</a> &#8211; the founder of <a href="http://code.google.com/p/autofac/">Autofac</a></li>
<li><a href="http://blogs.msdn.com/b/hammett/">Hamilton Verissimo aka hammett</a> &#8211;  founder of the <a href="http://www.castleproject.org/">Castle Project</a></li>
<li><a href="http://mef.codeplex.com/wikipage?title=Samples&amp;referringTitle=Documentation">MEF Samples</a></li>
<li><a href="http://www.hanselman.com/blog/ExtendingNerdDinnerAddingMEFAndPluginsToASPNETMVC.aspx">Extending NerdDinner: Adding MEF and plugins to ASP.NET MVC </a>- by Scott Hanselman</li>
</ul>
<p>Each application that I&#8217;ve built, whether it&#8217;s web or desktop app., it is consisted of one or more components each responsible for doing some particular job, manage products, generate reports etc. Before MEF, I would simply reference the assembly and call it directly from the code.  This is what I&#8217;ve always been doing and probably all of you. But as much as you try, yes you can make your code reusable and all that stuff, but you cannot avoid dependencies. Eventually, you are probably going to deploy more assemblies that you are paid for.</p>
<p>And by introducing dependencies you end up with:</p>
<ul>
<li>tight coupling of software components</li>
<li>software becomes hard to maintain</li>
<li>difficult to isolate when testing</li>
</ul>
<p>And most importantly, you cannot add new components without modifying the existing source code, because you may not have access to the source code for the components, they might have been developed by third party companies / developers.</p>
<p>But with MEF you can avoid that, because it  provides a way to discover software components implicitly, via composition. (more on this later.)</p>
<p>Consider for example Visual Studio 2010, everyone can <a href="http://blogs.msdn.com/b/visualstudio/archive/2010/04/21/extending-visual-studio-2010.aspx">develop extensions</a> and make them available for download so other developers can install them in their development environment, but you don&#8217;t have access in the source code of VS. But you do it based on some shared contracts and some pre-defined conventions. In this way, VS knows how to load them and make them available to you.</p>
<h1><a>Where is MEF used?</a></h1>
<p>MEF is used by Microsoft internally … it is not some patterns &amp; practices given to you as an example on how to build applications. They actually use this and have things that rely on MEF. In the following post you can see the applications of MEF - <a href="http://codebetter.com/glennblock/2010/06/03/mef-has-shipped/">http://codebetter.com/glennblock/2010/06/03/mef-has-shipped</a>.</p>
<p>I&#8217;ve followed the development of MEF on codeplex since <a href="http://mef.codeplex.com/releases/view/25797">MEF Preview 5</a> and I am very glad to say that it has truly matured since then. I can only say that it has seriously changed my thinking on solving complex problems.</p>
<p>In future I&#8217;m planning to write a series of posts where I&#8217;m going to cover the main concepts of MEF and show you how you can build extensible web applications using MEF and Asp.Net MVC.</p>
<p>So stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2011/05/14/introduction-to-mef/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Troubleshooting the database could not be exclusively locked to perform the operation error on SQL Server</title>
		<link>http://phalanx.spartansoft.org/2010/02/20/troubleshooting-the-database-could-not-be-exclusively-locked-to-perform-the-operation-error-on-sql-server/</link>
		<comments>http://phalanx.spartansoft.org/2010/02/20/troubleshooting-the-database-could-not-be-exclusively-locked-to-perform-the-operation-error-on-sql-server/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 19:25:20 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[troubleshooting]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=918</guid>
		<description><![CDATA[Recently we have started to implement our own Content Management System codename  DeepThought and when testing the creation of articles and menus we realized that we cannot use the default database collation of SQL Server. The reason is that in future we might have clients that require the use of Cyrillic characters.  Therefore the collation that should be used for these cases is SQL_Latin1_General_CP1251_CS_AS.

But when trying to change the collation I came across the following error: The database could not be exclusively locked to perform the operation.]]></description>
			<content:encoded><![CDATA[<p>Recently we have started to implement our own Content Management System codename  DeepThought and when testing the creation of articles and menus we realized that we cannot use the default database collation of SQL Server. The reason is that in future we might have clients that require the use of Cyrillic characters.  Therefore the collation that should be used for these cases is <strong>SQL_Latin1_General_CP1251_CS_AS.</strong></p>
<p>But when trying to change the collation I came across the following error:</p>
<p style="text-align: center;"><strong><span style="color: #ff0000;">The database could not be exclusively locked to perform the operation.</span></strong></p>
<p style="text-align: left;"><span style="color: #000000;">Apparently you cannot change the database collation if the accessibility of the database is set to MULTI_USER (which is by default).</span></p>
<p style="text-align: left;"><span style="color: #000000;"> Therefore if you want to change the database collation, you must do the following:</span></p>
<ul>
<li>Set the restrict access of the database to  SINGLE_USER (note the you might kill all the active sessions)</li>
<li>Change the collation of database to whatever you want it, in my case SQL_Latin1_General_CP1251_CS_AS</li>
<li>And switch back the restrict access of the database to MULTI_USER</li>
</ul>
<p>You can accomplish this either by executing the following t-sql in you sql management studio:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #66cc66;">&#91;</span>database_name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">SET</span> SINGLE_USER <span style="color: #993333; font-weight: bold;">WITH</span> ROLLBACK IMMEDIATE
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #66cc66;">&#91;</span>database_name<span style="color: #66cc66;">&#93;</span> COLLATE <span style="color: #66cc66;">&#91;</span>desired collation<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #66cc66;">&#91;</span>database_name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">SET</span> MULTI_USER</pre></div></div>

<p>or from SQL Management Studio by choosing the database properties:</p>
<p><a href="http://phalanx.spartansoft.org/wp-content/uploads/2010/02/database-properties.jpg"><img class="aligncenter size-full wp-image-919" title="Database Properties" src="http://phalanx.spartansoft.org/wp-content/uploads/2010/02/database-properties.jpg" alt="Database Properties" width="704" height="631" /></a></p>
<p>Sometimes the simplest issues can cause you a real headache.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/02/20/troubleshooting-the-database-could-not-be-exclusively-locked-to-perform-the-operation-error-on-sql-server/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Happy New Year 2010</title>
		<link>http://phalanx.spartansoft.org/2010/01/02/happy-new-year-2010/</link>
		<comments>http://phalanx.spartansoft.org/2010/01/02/happy-new-year-2010/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 16:44:20 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=884</guid>
		<description><![CDATA[I'd like to wish to all of our readers a happy new year.

Each day we grow as developers and also in 2010 we'll be here to continue to share our experience on solving complex programming problems and inspirational highlights.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d like to wish to all of our readers a happy new year.</p>
<p>Each day we grow as developers and also in 2010 we&#8217;ll be here to continue to share our experience on solving complex programming problems and inspirational highlights.</p>
<div class="wp-caption aligncenter" style="width: 530px"><a href="http://www.gpsmagazine.com/assets/happy_new_year_fireworks.jpg"><img title="Happy New Year 2010" src="http://www.gpsmagazine.com/assets/happy_new_year_fireworks.jpg" alt="Happy New Year 2010" width="520" height="349" /></a><p class="wp-caption-text">Happy New Year 2010</p></div>
<p>A big thank you for all your support &#8211; we could not do it without you (our readers).</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/01/02/happy-new-year-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing Strategy Pattern in C#</title>
		<link>http://phalanx.spartansoft.org/2009/11/06/implementing-strategy-design-pattern-in-cshar/</link>
		<comments>http://phalanx.spartansoft.org/2009/11/06/implementing-strategy-design-pattern-in-cshar/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 14:39:03 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=780</guid>
		<description><![CDATA[As a software developer, my primary goal is making my code correct, elegant, extensible and efficient. Although simplistic, every programming decision I make is largely based on maintainability. Back at my studies (Computer Sciences in SEE-University) I had the chance to read C# 3.0 Design Patterns by Judith Bishop, a great book which covers the full set of 23 patterns that were originally proposed in Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides’s Design Patterns: Elements of Reusable Object-Oriented Software in 1994.  By reading this book, I’ve acquired skills in: programming design patterns, basic UML modeling notation, ability to select appropriate patterns for given scenarios and advanced language features of C# 3.0. I highly recommend this book.]]></description>
			<content:encoded><![CDATA[<p>As a software developer, my primary goal is making my code correct, elegant, extensible and efficient. Although simplistic, every programming decision I make is largely based on maintainability. I&#8217;m very loyal to Don’t Repeat Yourself (DRY) principle.</p>
<p><a href="http://oreilly.com/catalog/9780596527730" target="_blank"><img class="alignleft" title="C# 3.0 Design Patterns" src="http://covers.oreilly.com/images/9780596527730/cat.gif" alt="C# 3.0 Design Patterns" height="150" /></a>Back at my studies (Computer Sciences in SEE-University) I had the chance to read C# 3.0 Design Patterns by Judith Bishop, a great book which covers the full set of 23 patterns that were originally proposed in Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides’s Design Patterns: Elements of Reusable Object-Oriented Software in 1994.  By reading this book, I’ve acquired skills in: programming design patterns, basic UML modeling notation, ability to select appropriate patterns for given scenarios and advanced language features of C# 3.0. I highly recommend this book.</p>
<div style="margin-left:0px;">
<h1><a>why strategy pattern?</a></h1>
<p>There are cases that for a given problem you could apply various algorithms. If  you keep all your algorithms in single class as a result you’ll have a very messy code with lots of conditional statements. It may work but it’s very hard to maintain, you may not recognize it after a while, it’s not the right way of doing it and it can be your worst nightmare. The ultimate tool in making your code maintainable is to keep it as simple as possible.</p></div>
<p>The strategy pattern enables a client to choose which algorithm to use from a range of supported algorithms and gives it a simple way to access it for ex. you have an News syndication service and you want to give you users the ability to grab those news in RSS or ATOM. In this case the scenario was to choose whether to process xml or text files.</p>
<h1><a>Design</a></h1>
<p>The key players for strategy patterns are as follows:</p>
<ol>
<li> <strong>Context </strong>- <em>Contains an IStrategy object’s algorithm in which we’re working on</em></li>
<li> <strong>IStrategy </strong>- <em>Defines an interface common to all the strategies</em></li>
<li> <strong>TextFileImporter</strong>, <strong>XmlFileImporter </strong>- <em>Classes that include algorithms that implement the IStrategy interface</em></li>
</ol>
<p>Below you have the UML diagram:</p>
<div id="attachment_784" class="wp-caption aligncenter" style="width: 514px"><img class="size-full wp-image-784" title="Strategy Pattern UML Diagram" src="http://phalanx.spartansoft.org/wp-content/uploads/2009/11/strategy_pattern_uml.JPG" alt="Strategy Pattern UML Diagram" width="504" height="228" /><p class="wp-caption-text">Strategy Pattern UML Diagram</p></div>
<h1><a>Implementation</a></h1>
<p>The <strong>IStrategy</strong> interface is used to define a contract that is shared between <strong>TextFileImporter</strong> and <strong>XmlFileImporter</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> FileImporter.<span style="color: #0000FF;">BusinessLogic</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #008080; font-style: italic;">/// A common interface to all supported file types</span>
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IStrategy
    <span style="color: #000000;">&#123;</span>
        XDocument ImportOrders<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Both <strong>TextFileImporter</strong> and <strong>XmlFileImporter</strong> classes implement <strong>IStrategy</strong> interface and they have a constructor which takes the file path as parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> FileImporter.<span style="color: #0000FF;">BusinessLogic</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TextFileImporter<span style="color: #008000;">:</span>IStrategy
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _path<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> TextFileImporter<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> path<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>._path <span style="color: #008000;">=</span> path<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region IStrategy Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span> XDocument ImportOrders<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            XDocument doc <span style="color: #008000;">=</span> XDocument.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span>_path<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> doc<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>        
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlFileImporter<span style="color: #008000;">:</span>IStrategy
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> _path<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> XmlFileImporter<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> path<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">this</span>._path <span style="color: #008000;">=</span> path<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region IStrategy Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span>  XDocument ImportOrders<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            XDocument doc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            doc <span style="color: #008000;">=</span> XDocument.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span>_path<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> doc<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>        
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here the context <strong>FileImporter</strong> points to a strategy and delegates to it. If a TextFileImporter is passed to the constructor than the TextFileImporter algorithm will take place and vise versa.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> FileImporter.<span style="color: #0000FF;">BusinessLogic</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FileImporter
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> IStrategy _strategy<span style="color: #008000;">;</span>              
&nbsp;
        <span style="color: #0600FF;">public</span> FileImporter<span style="color: #000000;">&#40;</span>IStrategy strategy<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _strategy <span style="color: #008000;">=</span> strategy<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ImportOrders<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            XDocument xmlDoc <span style="color: #008000;">=</span> _strategy.<span style="color: #0000FF;">ImportOrders</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            SqlHelper.<span style="color: #0000FF;">InsertOrders</span><span style="color: #000000;">&#40;</span>xmlDoc<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And here is where the decision making process takes place. Here the client can choose whether to use the <strong>XmlFileImporter</strong> or <strong>TextFileImporter</strong>. This is a code snippet from a windows service (more on this on later posts) which monitors a given directory and whenever a new file is created, it checks what type of file is (.xml or .txt) and based on the file extension if uses an appropriate strategy.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> watcher_Created<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileSystemEventArgs</span> e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">FullPath</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
        FileImporter.<span style="color: #0000FF;">BusinessLogic</span>.<span style="color: #0000FF;">FileImporter</span> importer <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">string</span> ext <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'.'</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>ext<span style="color: #000000;">&#41;</span>
       <span style="color: #000000;">&#123;</span>
             <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;.xml&quot;</span><span style="color: #008000;">:</span>
                   importer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileImporter.<span style="color: #0000FF;">BusinessLogic</span>.<span style="color: #0000FF;">FileImporter</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> XmlFileImporter<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">FullPath</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                   break<span style="color: #008000;">;</span>
             <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;.txt&quot;</span><span style="color: #008000;">:</span>
                   importer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileImporter.<span style="color: #0000FF;">BusinessLogic</span>.<span style="color: #0000FF;">FileImporter</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> TextFileImporter<span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">FullPath</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                   break<span style="color: #008000;">;</span>
             <span style="color: #0600FF;">default</span><span style="color: #008000;">:</span>
                   break<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>importer <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
              Thread thr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ThreadStart<span style="color: #000000;">&#40;</span>importer.<span style="color: #0000FF;">ImportOrders</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
              thr.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Hope this helps <img src='http://phalanx.spartansoft.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2009/11/06/implementing-strategy-design-pattern-in-cshar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Answer To Life, The Universe and Everything</title>
		<link>http://phalanx.spartansoft.org/2009/10/21/the-answer-to-life-the-universe-and-everything-2/</link>
		<comments>http://phalanx.spartansoft.org/2009/10/21/the-answer-to-life-the-universe-and-everything-2/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 10:23:54 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=759</guid>
		<description><![CDATA[One quiet morning, while going through the motions of my daily routine - drinking coffee with my fellow programmers, I got a call from a client who said: I just checked our vacancies page and it sucks, we don't have an online form where users would submit their information but instead they have to fill the form locally and then send it to us by mail or fax. We have to improve this fast. I need this for tomorrow!]]></description>
			<content:encoded><![CDATA[<p>One quiet morning, while going through the motions of my daily routine &#8211; drinking coffee with my fellow programmers, I got a call from a client who said:</p>
<div class="quotation">I just checked our vacancies page and it sucks, we don&#8217;t have an online form where users would submit their information but instead they have to fill the form locally and then send it to us by mail or fax. We have to improve this fast. I need this for tomorrow!</div>
<p>So I drank my coffee and started brainstorming on how I could accomplish this task the best way I could, always keeping in mind that I  would have to finish it during the day. The task was peace of cake, I could do it in two hours, but the challenge was on UI. How to make it more dynamic (allowing users to enter multiple previous job positions, multiple phone numbers, multiple addresses etc.) and give users a greater experience on using the app. I wanted to accomplish this with no postbacks at all.</p>
<p>So in this case, <strong><a href="http://en.wikipedia.org/wiki/The_Answer_to_Life,_the_Universe,_and_Everything" target="_blank">the answer to life, the universe, and everything</a></strong> for me it was jQuery.</p>
<p>You&#8217;ve probably heard about <strong><a href="http://jquery.com" target="_blank">jQuery</a></strong>, The Write Less, Do More JavaScript Framework. For those who didn&#8217;t have the chance to get their hands into jQuery, let me summarize it for you:</p>
<ul>
<li>It&#8217;s a lightweight JavaScript framework (less than 20 KB)</li>
<li>Access DOM element using CSS selectors.</li>
<li>Needless to check for null objects</li>
<li>Uses chains of actions</li>
<li>MVC JavaScript Architecture</li>
<li>Simple and very clean &#8230; the SPARTAN WAY</li>
<li>CSS 3.0 compliant</li>
<li>Cross-Browser</li>
<li>Extensible (support for plugins)</li>
<li>And a great community support</li>
</ul>
<p>And without further ado, let&#8217;s do that voodoo that we do do so well.</p>
<p>Below you have a screenshot of the above scenario.</p>
<div>
<dl id="attachment_738" style="width: 691px;">
<dt><img title="jQuery Life Saviour" src="http://phalanx.spartansoft.org/wp-content/uploads/2009/10/jQueryLifeSaviour.png" alt="jQuery Life Saviour" width="681" height="347" /></dt>
<dd>jQuery Life Saviour</dd>
</dl>
</div>
<p>As you can see, the job position and company name are added based on user desire and are appended dynamically to the DOM. Initially I&#8217;ve defined only the FamilyName and the FirstName of the applicant. Then I&#8217;ve defined the container <strong>workContainer</strong> in which after the DOM is ready, are initialized the first controls<br />
<span style="color:red;font-weight:bold;">[ updated ]</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;% using <span style="color: #66cc66;">&#40;</span>Html.BeginForm<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Result&quot;</span>, <span style="color: #ff0000;">&quot;Home&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">       <span style="color: #66cc66;">&#123;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Your Information<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        Family Name:
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;">&lt;%=Html.TextBox<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FamilyName&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span>, new <span style="color: #66cc66;">&#123;</span> @class = <span style="color: #ff0000;">&quot;txt&quot;</span>, style = <span style="color: #ff0000;">&quot;width: 200px&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        First Name:
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;">&lt;%=Html.TextBox<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FirstName&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span>, new <span style="color: #66cc66;">&#123;</span> @class = <span style="color: #ff0000;">&quot;txt&quot;</span>, style = <span style="color: #ff0000;">&quot;width: 200px&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>%<span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        ...
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        ...
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        ...
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        ...
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        Work Experience?
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td</span> <span style="color: #000066;">align</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">valign</span>=<span style="color: #ff0000;">&quot;top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;workContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;">&lt;%= Html.AntiForgeryToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Apply For this Job&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;">&lt;%<span style="color: #66cc66;">&#125;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>


<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>            
	<span style="color: #006600; font-style: italic;">//wait until DOM is ready</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//prepare initial controls</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;table id=&quot;tblWork&quot;&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Position&lt;/th&gt;&lt;th&gt;Company&lt;/th&gt;&lt;th&gt;&amp;nbsp;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;position&quot; /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;company&quot; /&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;'</span><span style="color: #009900;">&#41;</span>
			.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a href=&quot;#&quot; id=&quot;addAnother&quot; class=&quot;add&quot;&gt;Add Another&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span>
			.<span style="color: #660066;">prependTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#workContainer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                    
&nbsp;
		<span style="color: #006600; font-style: italic;">//atach an event handler to the addAnother link</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#addAnother&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			  <span style="color: #006600; font-style: italic;">//get the whole table                                           </span>
			  <span style="color: #003366; font-weight: bold;">var</span> $tblWork <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table#tblWork&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                      
&nbsp;
			  <span style="color: #006600; font-style: italic;">//add row at the end of the table</span>
			  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table#tblWork tr:last&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">after</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;tr&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;position&quot; /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;company&quot; /&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;a href=&quot;#&quot; class=&quot;delete&quot;&gt;Remove&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			  <span style="color: #006600; font-style: italic;">//find all links with &quot;delete&quot; class and atach the handler</span>
			  $tblWork.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.delete'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>              
				<span style="color: #006600; font-style: italic;">//remove the current table row                                      </span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                      
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>            
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>            
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The classes defined in this project are Applicant and WorkExperience.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> jQueryLifeSaviour.<span style="color: #0000FF;">Entities</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WorkExperience
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Position <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Company <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> jQueryLifeSaviour.<span style="color: #0000FF;">Entities</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Applicant
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FamilyName <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> List<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>WorkExperience<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> Experience <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And here is the action which occurs after you click on <strong>Apply for this job</strong> button.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// Get user input posted by view.</span>
<span style="color: #008080; font-style: italic;">/// Accepts only POST HTTP Methods,</span>
<span style="color: #008080; font-style: italic;">/// although in this case the GET would be just fine since we are not dealing with sensitive data</span>
<span style="color: #008080; font-style: italic;">///The collection of posted form elements</span>
<span style="color: #008080; font-style: italic;">/// The current view (in this case Result)</span>
<span style="color: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Result<span style="color: #000000;">&#40;</span>FormCollection form<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//create a new intance of Applicant class</span>
     Applicant obj <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Applicant
     <span style="color: #000000;">&#123;</span>
          FamilyName <span style="color: #008000;">=</span> form<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FamilyName&quot;</span><span style="color: #000000;">&#93;</span>,
          FirstName <span style="color: #008000;">=</span> form<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#93;</span>,
          Experience <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>WorkExperience<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">//hold all input fields regarding company info</span>
     <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> companies <span style="color: #008000;">=</span> form.<span style="color: #0000FF;">GetValues</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;company&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">//hold all input fields regarding job position</span>
     <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> positions <span style="color: #008000;">=</span> form.<span style="color: #0000FF;">GetValues</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;position&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">//iterate through posted work experience and fill the WorkExperience list</span>
     <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> positions.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          obj.<span style="color: #0000FF;">Experience</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WorkExperience
          <span style="color: #000000;">&#123;</span>
               Position <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>positions<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>,
               Company <span style="color: #008000;">=</span> companies<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>
          <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
      ViewData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Applicant&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> obj<span style="color: #008000;">;</span>
      <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I&#8217;ve been using jQuery for a while and it made me realize that life is so much easier when using jQuery. With jQuery, JavaScript is no more &#8220;the necessary evil&#8221;, it&#8217;s even fun to code with.</p>
<p>You can download the full project <a href="http://phalanx.spartansoft.org/wp-content/uploads/2009/10/jQueryLifeSaviour.rar">here</a>. This demo is developed on ASP.NET MVC 1.0 Framework, so you must install it in prior, otherwise you want be able to open it.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2009/10/21/the-answer-to-life-the-universe-and-everything-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

