tag:blogger.com,1999:blog-84867057597014492672024-03-13T09:07:37.830+05:30SanjeetosphereBetween Heaven and EarthSanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-8486705759701449267.post-6078269491993047872011-12-06T20:15:00.001+05:302011-12-06T20:15:50.185+05:30Using JDO with Google AppEngine Datastore<div dir="ltr" style="text-align: left;" trbidi="on">
Today we’ll walk through on steps how to perform an insert into <a href="http://code.google.com/appengine/docs/java/gettingstarted/usingdatastore.html">Google AppEngine datastore</a> using <a href="http://code.google.com/appengine/docs/java/datastore/" target="_blank">Datastore API</a> and <a href="http://code.google.com/appengine/docs/java/datastore/jdo/" target="_blank">JDO</a>. <br />
In my last project I used <a href="http://code.google.com/appengine/docs/java/gettingstarted/usingdatastore.html">Google AppEngine datastore</a> without <a href="http://code.google.com/appengine/docs/java/datastore/jdo/">JDO or JPA</a>. It was fun. Well, I had to do a lot of transformations on my own from Entities to POJOs. Something like the following:<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; height: 130px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;">
<div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum1" style="color: #606060;"> 1:</span> Entity brandEntity = <span style="color: blue;">new</span> Entity(Brand.<span style="color: blue;">class</span>.getSimpleName());</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum2" style="color: #606060;"> 2:</span> brandEntity.setProperty(<span style="color: #006080;">"name"</span>, brand.getName());</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum3" style="color: #606060;"> 3:</span> brandEntity.setProperty(<span style="color: #006080;">"description"</span>, brand.getDescription());</pre>
</div>
</div>
<br />
With <a href="http://code.google.com/appengine/docs/java/datastore/jdo/">JDO</a> you don’t have to worry about it. You need to worry about your Java objects and not Datastore entities. So, no more string literals and you are all set with type safety. The above code will look similar to something like the following:<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; height: 99px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum1" style="color: #606060;"> 1:</span> Brand brand = <span style="color: blue;">new</span> Brand();</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum2" style="color: #606060;"> 2:</span> brand.setName(<span style="color: #006080;">"Xbox 360"</span>);</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum3" style="color: #606060;"> 3:</span> brand.setDescription(<span style="color: #006080;">"Gaming console by Microsoft"</span>);</pre>
</div>
</div>
<br />
Using Datastore API you insert you data using the following:<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; height: 192px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum1" style="color: #606060;"> 1:</span> DatastoreService datastore = Datastore.get();</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum2" style="color: #606060;"> 2:</span> Entity brandEntity = <span style="color: blue;">new</span> Entity(Brand.<span style="color: blue;">class</span>.getSimpleName());</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum3" style="color: #606060;"> 3:</span> brandEntity.setProperty(<span style="color: #006080;">"name"</span>, brand.getName());</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum4" style="color: #606060;"> 4:</span> brandEntity.setProperty(<span style="color: #006080;">"description"</span>, brand.getDescription());</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum5" style="color: #606060;"> 5:</span> brandEntity.setProperty(<span style="color: #006080;">"manufacturerId"</span>, brand.getManufacturerId()); </pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum6" style="color: #606060;"> 6:</span> datastore.put(brandEntity);</pre>
</div>
</div>
<br />
Datastore.java is a custom class defined as:<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; height: 246px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: blue;">public</span> <span style="color: blue;">class</span> Datastore {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> DatastoreService datastore = </pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum3" style="color: #606060;"> 3:</span> DatastoreServiceFactory.getDatastoreService();</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum4" style="color: #606060;"> 4:</span> </pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: blue;">private</span> Datastore() {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum6" style="color: #606060;"> 6:</span> }</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum7" style="color: #606060;"> 7:</span> </pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: green;">/**</span></pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: green;"> * singleton instance of the datastore service</span></pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: green;"> * </span></pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: green;"> * @return</span></pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: green;"> */</span></pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> DatastoreService get() {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: blue;">return</span> datastore;</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum15" style="color: #606060;"> 15:</span> }</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum16" style="color: #606060;"> 16:</span> }</pre>
</div>
</div>
<br />
Using JDO, the same can be achieved by the following:<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; height: 239px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum1" style="color: #606060;"> 1:</span> PersistenceManager db = Persistancemanager.getPersistenceManager();</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum2" style="color: #606060;"> 2:</span> Brand brand = <span style="color: blue;">new</span> Brand();</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum3" style="color: #606060;"> 3:</span> brand.setName(<span style="color: #006080;">"Xbox 360"</span>);</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum4" style="color: #606060;"> 4:</span> brand.setDescription(<span style="color: #006080;">"Gaming console by Microsoft"</span>);</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: blue;">try</span> {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum6" style="color: #606060;"> 6:</span> db.makePersistent(brand);</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum7" style="color: #606060;"> 7:</span> } <span style="color: blue;">finally</span> {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum8" style="color: #606060;"> 8:</span> db.close();</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum9" style="color: #606060;"> 9:</span> }</pre>
</div>
</div>
<br />
Persistencemanager.java is a custom class that is a wrapper over JDO PersistenceManager defined as following:<br />
<br />
<div id="codeSnippetWrapper" style="background-color: #f4f4f4; border-bottom: silver 1px solid; border-left: silver 1px solid; border-right: silver 1px solid; border-top: silver 1px solid; cursor: text; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding-bottom: 4px; padding-left: 4px; padding-right: 4px; padding-top: 4px; text-align: left; width: 97.5%;">
<br />
<div id="codeSnippet" style="background-color: #f4f4f4; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;">
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: blue;">public</span> <span style="color: blue;">final</span> <span style="color: blue;">class</span> Persistancemanager {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> PersistenceManagerFactory pmfInstance = </pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum3" style="color: #606060;"> 3:</span> JDOHelper.getPersistenceManagerFactory(<span style="color: #006080;">"transactions-optional"</span>);</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum4" style="color: #606060;"> 4:</span> </pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: blue;">private</span> Persistancemanager() {</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum6" style="color: #606060;"> 6:</span> }</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum7" style="color: #606060;"> 7:</span> </pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> PersistenceManagerFactory getInstance() {</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: blue;">return</span> pmfInstance;</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum10" style="color: #606060;"> 10:</span> }</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum11" style="color: #606060;"> 11:</span> </pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> PersistenceManager getPersistenceManager() {</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: blue;">return</span> pmfInstance.getPersistenceManager();</pre>
<pre style="background-color: #e3e3e3; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum14" style="color: #606060;"> 14:</span> }</pre>
<pre style="background-color: white; border-bottom-style: none; border-left-style: none; border-right-style: none; border-top-style: none; color: black; direction: ltr; font-family: 'Courier New', courier, monospace; font-size: 12px; line-height: 12pt; margin: 0em; overflow: visible; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: left; width: 100%;"><span id="lnum15" style="color: #606060;"> 15:</span> }</pre>
</div>
</div>
<br />
The Google AppEngine Datastore is schemaless. JDO and/or JPA provides a way to add a soft schema layer on top of the datastore. So, apart from the benefit of CRUD-ing data easily we can also enforce a lot of application level constraints like association between entities and so on.<br />
<br />
I am sure the above is going to help a lot of beginners and Google AppEngine enthusiasts in getting upto speed with the datastore.</div>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com1tag:blogger.com,1999:blog-8486705759701449267.post-18828019015341348242010-07-07T09:36:00.001+05:302010-07-12T08:54:42.144+05:30The power of “NO”<p>There are a lot of people who can’t say NO; to just about anything. Even if it hurts them personally. </p> <p>In one of my previous companies I attended a training on <a href="http://www.leadersdirect.com/perseffect.html" target="_blank">Personal Effectiveness</a>. It covered different personality traits. They discussed about a concept called C.O.R.E. In an exercise they evaluated the attendees using a questionnaire. I was declared a person with a dominating “E” trait. Everyone has a dominating trait and certain other supporting traits. Later on they seated the attendees based on their dominating traits starting with C. Obviously I was in the last row. But, the most important point is that in a class of 20 there was only 1 guy who sat with me on that last row. I knew I am kind of insane but I didn’t know the extent. Before I started to feel lonely, the trainer started explaining things about the concept. Normally I remember all the humiliations and insults I go through but, this is kind of exceptional. I do not remember what the acronyms C, O, R and E stand for, but I remember what he said about E. Well, later on it turned out that E people are more humane than other people, so I was happy! <br /> <br />E people can’t say NO. They want everybody around them to be happy. So much so that they care about the opinions of a strangers too. They try to be modest and ensure that they always leave a good impression. They like to make friends in the group. They get disturbed if they said something bad (even if it was the need of the hour). They can’t do routine stuff. If traits like perfectionism is also present these people are a perfect fit. They ensure a good healthy environment and get things done efficiently too! But, the biggest worry is that the points that go in favor are points that go against them. The fact that they can’t say NO, from the organization point of view, these people are effective but they always are flooded with work. They like to please people that’s why they can’t take tough decisions – and that’s one of the reasons they like to work alone so that nobody interferes and nobody gets hurt!! Wonderful! <br /> <br />However, the other extreme is also present and its abundant. The people who say NO to everything. The first word they utter after a discussion is over and if you asked him – they will say NO. These people are “C” people. They like to do routine stuff. They never take chances. They are conservative and resist change. Unlike E guys who like to try and work out new things, they will act a laggard and stay away from new things and try only when they are asked for or mandated to do so. You can easily figure them out in a conversation, and judge how they will react to a situation. In any conversation or discussion they are mostly on the opposing side. </p> <p>Its often said that its easy to criticize than to support – its because of these people. The pointers that go against them are the same ones that work in their favor. The fact that they resist change is the fact they bring stability to the organization. They bring the other aspect of the conversation strongly, and most importantly they never lose money in recession – because they invest in Fixed deposits and debt instruments than equity based instruments :) These are the people who survive bad times because of the power of NO. <br /> <br />There is nothing like right or wrong or good traits or bad traits. Everything in this world is relative. The people who let the correct trait dominate according to the given circumstance are winners eventually.</p> Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-55488076291635118222010-07-02T14:16:00.000+05:302010-07-02T14:26:44.463+05:30What’s in the name?<p>Let’s take a step back. Alright, several steps back. When you started reading this, did you get excited by the fact that you will learn something new? It will be fun? No, you thought its just another blog talking about some other weird misnomer (go get a life, watch 3 Idiots!).</p> <p>If you like the practice of software engineering – you are most welcome. If you are not, then you are not wasting your time, read on, its fun. I am not as funny as a porcupine wearing a Li.Lo. (Lindsay Lohan – they say she likes girls! No big deal, even I do) red wrap-around, but you can try. Ever heard of adventure?</p> <p>I thought of writing this when I was stuck in a traffic jam with all vehicles getting in way of the other – like a deadlock you know – you techie guy. But, I like saying all this (read shouting) to all those guys who are “accidently in love” (OST Shrek) with software engineering. I will call them coders from now on. These coders code for fun. Because, they make fun of coding. This is the first in series of my misadventures with coders. They have transformed me into a very calm and composed person. I am thankful to them.</p> <p>I am also a coder. I like to be like this for many more years to come because I am in love with coding. Its a different matter altogether that my definition of coding captures architecting, then designing, then developing, then unit testing, and finally delivering to business with agile-ity. </p> <p>We will talk about other aspects later, but for now I am concentrating on the greatest myth of all – naming the variables.</p> <p>What the F. is in the name? It doesn’t matter, does it? My dear numbnuts, it does. People who care about it have written books on it. Go fetch.</p> <p>Alright, tell me what could be wrong a variable names like: “strString”, or “intInt”, or for the imaginative few “iInt”, or for the pseudo-coders “strName”? Did I forget to mention i, x, y, k etc. What about F? What’s wrong in specifying the data type along with (data type?), something! Concentrate on the functional aspect of the variable not on the data type - see what it does, don’t stress on what it is. Its meaningless isn’t it? What about trying names like “firstName”, “lastName” or “totalFiles” for a change?</p> <p>People try and be imaginative and use names like “m_strName”, “p_intAge”, or “g_oSessionObject” – they are just trying to attach scope of the variable with their names. No big deal. You ain’t doin’ structured programming are you?</p> <p>Some of them use names like “Form_Collection.cs” in a windows application to name a Windows Form, and “Form_Collection.aspx” in a Web application. You will find all files in their solution explorer with that redundant “Form_”. Why is that? Will it become an “edmx” if you do not prefix “Form_”? Redundant, agonizing.</p> <p> Some choose to use names like “TCImpersonator” or “TCComputerManagement” for their classes (kudos to them that they created one). “TC” was the name of the application. Namespaces anyone?</p> <p>So, you see the different trends and patterns in naming. Mind you that I have worked in about 5 to 6 different companies, most of them very huge enterprises, and the story is same everywhere. Nobody takes this aspect of programming seriously. One of my colleagues declared that some “jobless person is given a task to create some guidelines” – they don’t even give a damn to it. But, let me tell you its very important. Its as important as a brick is to a building or a friend is in facebook.</p> <p>Would you like to hear a person talking about hygiene and cleanliness if he doesn’t even use water to clean his donkey (try a synonym)? Now, do you understand where I am going? It all starts with this. If a coder is not convinced about keeping his variable names under check how can you expect him to try and understand concepts like “Separation of Concerns” and “n-tier” architecture? Even if they do theoretically understand, they will hesitate to practice it. The result is smelly, redundant spaghetti code.</p> <p>The true takeaway of this post is to make my fellow coders understand the fact that keeping things simple and following patterns and practices is like “a stitch on time saves nine”. I know the fact that deadlines, peer pressures, and sometimes biased guidelines takes a toll on us, but that shouldn’t stop us from writing good, human readable code. Although machine executes it, but there is a reason why C# or any other high level language is in English not in binary. Leave your Visual Basic and VC++ 6.0 days behind you, be <a href="http://www.extremeprogramming.org/" target="_blank">pragmatic</a>, be objective, and be functional. Start now, its not that difficult, and you will see how your confidence develops, and you too can be cool!</p> <p>Don’t listen to what I say, just give it a thought, and try the following links, and you will know what’s good and what’s not.</p> <p><a href="http://msdn.microsoft.com/en-us/library/xzf533w0(v=VS.71).aspx" target="_blank">Naming Guidelines</a></p> Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-4902908076248481262009-09-02T21:33:00.005+05:302009-09-02T21:37:06.761+05:30Design PrinciplesA Design principle is a basic tool or technique that can be applied to designing or writing code to make it more maintainable, flexible or extensible.<br />
<br />
Some of the design principles are:<br />
<ul><li>Encapsulate what varies<span style="font-family: Times New Roman; font-size: 12pt;"><br />
</span></li>
<li>Code to an interface rather than to an implementation<br />
</li>
<li>Each class in your application should have only one reason for change<br />
</li>
<li>Classes are about behavior and functionality<br />
</li>
<li> Classes should be open for extension and closed for modification - <b>Open Closed Principle</b><ul><li>It features Inheritance, and composition<br />
</li>
<li>It exhibits abstraction and encapsulation in action </li>
</ul></li>
<li> Avoid duplicate code by abstracting out things that are common and placing those things in a single location - <b>Don't repeat yourself</b><ul><li>It's really about one requirement at one place<br />
</li>
<li>It's not only about removing duplication; it's also about making good decisions about how to break up your systems functionality.<br />
</li>
</ul></li>
<li> Every object in your system should have a single responsibility and all its services should be focused on carrying out that single responsibility - <b>The Single Responsibility Principle (SRP)</b><br />
<ul><li>Using this principle doesn't essentially make your classes smaller; it makes them larger instead, as all services exposed by the class remains at one point. Also, it will enable you to create fewer classes which mean better maintenance.<br />
</li>
<li>Cohesion is the other name for this principle, if you are writing a highly cohesive class it means that this principle is correctly implemented.<br />
</li>
</ul></li>
<li> Subtypes must be substitutable for their base types - <b>The Liskov Substitution Principle (LSP)</b><br />
<ul><li>The LSP<b> </b>is all<b> </b>about well designed inheritance. When you inherit from your base class you must be able to substitute your subclass for that base class without things going terribly wrong. If it does, then you must have used inheritance incorrectly.<br />
</li>
<li>Violating LSP makes the code confusing. It's hard to understand code that uses inheritance incorrectly.<br />
</li>
<li> <b>Delegation</b> comes to rescue in such circumstances - Delegation is when you hand over the responsibility for a particular task to another class or method.<br />
<ul><li>If you need to use functionality in another class, but you don't want to change that functionality, consider using delegation over inheritance.<br />
</li>
</ul></li>
<li> <b>Composition</b><br />
<ul><li>An alternative to<b><br />
</b>inheritance just as delegation.<br />
</li>
<li>In composition, your object completely owns the composed objects, and they do not exist outside of their usage in your object.<br />
</li>
<li>E.g. a text file and its contents. If you delete the text file all its contents are gone!<br />
</li>
</ul></li>
<li> <b>Aggregation</b><br />
<ul><li>Another alternative to inheritance.<br />
</li>
<li>In aggregation, you use another object inside your object. Both can exist outside of each other.<br />
</li>
<li>e.g. if you copy a file inside a folder, the file(s) and the folder continue to exist, even if you delete the folder, the file that's outside the folder will remain as it is.<br />
</li>
</ul></li>
<li> If you favor delegation, composition, and aggregation over inheritance, your software will be more flexible, easier to maintain, extend and reuse.<br />
<div style="margin-left: 27pt;"><b><br />
</b> </div></li>
</ul></li>
</ul>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com1tag:blogger.com,1999:blog-8486705759701449267.post-67271699204768815742009-09-02T16:14:00.001+05:302009-09-02T16:16:33.879+05:30Design Patterns Quick View<ol> <p>There are 3 types of design patterns</p> <li>Creational <p>These are the ones that take care of creating new objects. Sometimes creating objects simply with new can complicate the design of the software. To handle such unique situations the following patterns are used.</p> <p>Abstract Factory Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5Lj0PlIEI/AAAAAAAAD-g/GF_YO5onqf8/s1600-h/clip_image001%5B6%5D%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[6]" border="0" alt="clip_image001[6]" src="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5LmaWozGI/AAAAAAAAD-k/bZhwvvhZHfk/clip_image001%5B6%5D_thumb%5B3%5D.png?imgmax=800" width="557" height="236" /></a></p> <p>Factory Pattern</p> <p><a href="http://lh5.ggpht.com/_2xIBIxheSrg/Sp5LoGSC68I/AAAAAAAAD-o/csl3q4oaiv0/s1600-h/clip_image002%5B6%5D%5B6%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002[6]" border="0" alt="clip_image002[6]" src="http://lh5.ggpht.com/_2xIBIxheSrg/Sp5Lqr68ilI/AAAAAAAAD-s/mmI7RRIhAi4/clip_image002%5B6%5D_thumb%5B4%5D.png?imgmax=800" width="557" height="242" /></a></p> <p>Builder Pattern</p> <p><a href="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5LtU--KXI/AAAAAAAAD-w/EagY2xxiEU4/s1600-h/clip_image003%5B6%5D%5B9%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image003[6]" border="0" alt="clip_image003[6]" src="http://lh5.ggpht.com/_2xIBIxheSrg/Sp5LwHMOs-I/AAAAAAAAD-0/ARIoJZtbgys/clip_image003%5B6%5D_thumb%5B7%5D.png?imgmax=800" width="557" height="256" /></a></p> <p>Prototype Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5LyMC8R2I/AAAAAAAAD-4/6wG3615HNgs/s1600-h/clip_image004%5B6%5D%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004[6]" border="0" alt="clip_image004[6]" src="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5L0TSEcMI/AAAAAAAAD-8/XfaBKZ_neY4/clip_image004%5B6%5D_thumb%5B3%5D.png?imgmax=800" width="557" height="250" /></a></p> <p>Singleton Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5L2DQkSRI/AAAAAAAAD_A/QkJbPrQf6HE/s1600-h/clip_image005%5B6%5D%5B9%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image005[6]" border="0" alt="clip_image005[6]" src="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5L4TekgnI/AAAAAAAAD_E/9Bd3ee3dsT0/clip_image005%5B6%5D_thumb%5B7%5D.png?imgmax=800" width="557" height="256" /></a></p> </li> <li>Behavioral <p>These patterns identify common communication patterns between objects. They aim to increase the flexibility in carrying out the communication. Such patterns are listed below:</p> <p>Observer Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5L6db3opI/AAAAAAAAD_I/u37DNczDtfk/s1600-h/clip_image006%5B6%5D%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006[6]" border="0" alt="clip_image006[6]" src="http://lh5.ggpht.com/_2xIBIxheSrg/Sp5L8v24CJI/AAAAAAAAD_M/uJgj90UUSvU/clip_image006%5B6%5D_thumb%5B3%5D.png?imgmax=800" width="557" height="252" /></a></p> <p>Strategy Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5L-j4mEMI/AAAAAAAAD_Q/7Yz4GI9q2LI/s1600-h/clip_image007%5B6%5D%5B4%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image007[6]" border="0" alt="clip_image007[6]" src="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5MAxW-rQI/AAAAAAAAD_U/nMSw-3oJPRQ/clip_image007%5B6%5D_thumb%5B2%5D.png?imgmax=800" width="557" height="245" /></a></p> <p>Command Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5MC6fZLEI/AAAAAAAAD_Y/YrmDCuATu2k/s1600-h/clip_image008%5B6%5D%5B4%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008[6]" border="0" alt="clip_image008[6]" src="http://lh5.ggpht.com/_2xIBIxheSrg/Sp5MFCla8-I/AAAAAAAAD_c/WF_5kYpCpOg/clip_image008%5B6%5D_thumb%5B2%5D.png?imgmax=800" width="557" height="245" /></a></p> <p>Iterator Pattern</p> <p><a href="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5MG0Var3I/AAAAAAAAD_g/bVIh8cwXAjg/s1600-h/clip_image009%5B6%5D%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image009[6]" border="0" alt="clip_image009[6]" src="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5MJLCXaJI/AAAAAAAAD_k/6i4oZyZQ6bQ/clip_image009%5B6%5D_thumb%5B3%5D.png?imgmax=800" width="557" height="247" /></a></p> <p>State Pattern</p> <p><a href="http://lh5.ggpht.com/_2xIBIxheSrg/Sp5MLBq23hI/AAAAAAAAD_o/HuUbIA-ChHw/s1600-h/clip_image010%5B6%5D%5B5%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010[6]" border="0" alt="clip_image010[6]" src="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5MNEd3-0I/AAAAAAAAD_s/MFBfSBdmXCs/clip_image010%5B6%5D_thumb%5B3%5D.png?imgmax=800" width="557" height="248" /></a></p> </li> <li>Structural </li> </ol> <blockquote> <p>These are design patterns that ease the design by identifying a simple way to realize relationships between entities.</p> </blockquote> <blockquote> <p>Decorator Pattern</p> <p><a href="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5MPHARxGI/AAAAAAAAD_w/LPqNpUyRoPc/s1600-h/clip_image001%5B8%5D%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[8]" border="0" alt="clip_image001[8]" src="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5MRVdSsMI/AAAAAAAAD_0/DBaykvFw1LQ/clip_image001%5B8%5D_thumb%5B1%5D.png?imgmax=800" width="557" height="241" /></a></p> <p>Adapter Pattern</p> <p><a href="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5MT6ioWHI/AAAAAAAAD_4/tSLD_OU7Cu4/s1600-h/clip_image002%5B8%5D%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002[8]" border="0" alt="clip_image002[8]" src="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5MWGF1XyI/AAAAAAAAD_8/HABnQK7gUwo/clip_image002%5B8%5D_thumb%5B1%5D.png?imgmax=800" width="557" height="237" /></a></p> <p>Façade Pattern</p> <p><a href="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5MXymiInI/AAAAAAAAEAA/qzGOH_es4g0/s1600-h/clip_image003%5B8%5D%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image003[8]" border="0" alt="clip_image003[8]" src="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5MaOXP9uI/AAAAAAAAEAE/9gYjHShcjk0/clip_image003%5B8%5D_thumb%5B1%5D.png?imgmax=800" width="557" height="230" /></a></p> <p>Bridge Pattern</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5MdGbEINI/AAAAAAAAEAI/Lfehlwnq7jA/s1600-h/clip_image004%5B8%5D%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004[8]" border="0" alt="clip_image004[8]" src="http://lh3.ggpht.com/_2xIBIxheSrg/Sp5MfQ7Tj0I/AAAAAAAAEAM/s1r8Z5RKyyk/clip_image004%5B8%5D_thumb%5B1%5D.png?imgmax=800" width="557" height="234" /></a></p> <p>Proxy Pattern</p> <p><a href="http://lh6.ggpht.com/_2xIBIxheSrg/Sp5MhICDEzI/AAAAAAAAEAQ/jcp8uiRZb4k/s1600-h/clip_image005%5B8%5D%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image005[8]" border="0" alt="clip_image005[8]" src="http://lh4.ggpht.com/_2xIBIxheSrg/Sp5MjLKVb8I/AAAAAAAAEAU/VJlsb74zx0I/clip_image005%5B8%5D_thumb%5B1%5D.png?imgmax=800" width="557" height="237" /></a></p></blockquote> Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-5617258686491997262009-08-30T22:42:00.001+05:302009-08-31T22:06:01.033+05:30Assembly refresher<span xmlns="xmlns"> <ol> <li> <ol> <li>The concept <ol> <li>It’s a collection of one or more files containing type definitions and resource files like images, xml files etc used in your program. </li> <li>It also contains something called <strong>Manifest</strong> <ol> <li>A <strong>manifest</strong> is a set of metadata tables that contains names of the files that are part of the assembly. </li> <li>It contains the metadata information about the assembly like its version, culture, publisher etc. </li> </ol> </li> </ol> <ol> <li>It’s the smallest unit of deployment in .NET because CLR operates on Assembly. </li> <li>CLR always loads the file that contains the manifest metadata tables first and then uses it get the names of the files that are part of the assembly. </li> <li>You can have a multi-file assembly one of which can contain types that are frequently used, and the other ones containing types that are less likely to be used. This works well for programs that work over the internet. They only need to download the parts of the assembly that is mostly used. Other parts will be downloaded on demand. <ol> <li>Its job of the CLR to load the parts of the assembly whenever required - locally or over the Internet, the address is present. </li> <li>You can use the multi-file assembly feature to <ol> <li>add resource or data files to your assembly or </li> <li>you can also use it in case types are implemented using different programming languages, and combine them to produce a single assembly. </li> </ol> </li> </ol> </li> </ol> <ol> <li>The problem is that VS doesn't provide you the feature to create multi-file assembly, you will have to use command-line utilities like <strong>csc</strong>.exe <ol> <li>All you do is create multiple .<strong>netmodules</strong> using <strong>csc</strong>.exe <strong>/t:module</strong>, and combine them using /<strong>addmodule</strong> switch. </li> <li>Lets have two C# files: Apple.cs and Mango.cs; do the following: <ol> <li><strong>csc</strong> <strong>/t:module</strong> Apple.cs [creates a .netmodule] </li> <li><strong>csc</strong> /<strong>out</strong>:MyTypes.dll <strong>/t:library</strong> /<strong>addmodule</strong>:Apple.<strong>netmodule</strong> Mango.cs [creates an assembly] </li> </ol> </li> </ol> <ol> <li>The resultant assembly will have two managed modules and one manifest </li> </ol> </li> </ol> </li> <li>Types of assemblies <ol> <li>There are two types of assemblies in .NET: <ol> <li>Weakly named, and </li> <li>Strongly named </li> </ol> </li> <li>The fundamental difference between the two is that the strongly named assembly is signed with a publisher's public/private key pair that uniquely identifies the publisher. </li> <li>A strongly named assembly can be deployed either privately or publically. The GAC is the repository where CLR looks out for strong named public assemblies. </li> <li>Having a central repository means that the assemblies need more than just a filename to be distinguished. The following shows the Assembly folder before and after its exposed using the command: <strong>regsvr32</strong> D:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\<strong>shfusion</strong>.dll <strong>-u</strong> </li> </ol> </li> </ol> <p><a href="http://lh6.ggpht.com/_2xIBIxheSrg/Spv6662XjzI/AAAAAAAAD-Q/C0IGTdm1rOk/s1600-h/AssemblyFolder%5B4%5D.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AssemblyFolder" border="0" alt="AssemblyFolder" src="http://lh4.ggpht.com/_2xIBIxheSrg/Spv68xSRqHI/AAAAAAAAD-U/rwqSRg-u8BI/AssemblyFolder_thumb%5B2%5D.jpg?imgmax=800" width="278" height="158" /></a> <a href="http://lh6.ggpht.com/_2xIBIxheSrg/Spv6-uM0ZwI/AAAAAAAAD-Y/-ny_LUt2w84/s1600-h/AssemblyFolderExposed%5B2%5D.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AssemblyFolderExposed" border="0" alt="AssemblyFolderExposed" src="http://lh3.ggpht.com/_2xIBIxheSrg/Spv7A3oQizI/AAAAAAAAD-c/ZMSht0EIuqw/AssemblyFolderExposed_thumb.jpg?imgmax=800" width="244" height="223" /></a> </p> </li> </ol> </span> Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com1tag:blogger.com,1999:blog-8486705759701449267.post-60057183828507374182009-08-30T18:27:00.001+05:302009-08-30T18:29:27.623+05:30Iterative and Evolutionary development - Heads up<span xmlns=''><ul><li>In this lifecycle approach, the development is organized into a series of short, fixed length mini projects called <strong>Iterations.</strong><br /> </li><li>The resultant of each iteration is a subset of the actual production level software. Its neither a prototype nor an experimental throw away.<br /></li><li>The software should grow with each iteration until it unifies with the expected end result and that's why its called <strong>Iterative and Incremental Development</strong>.<br /></li><li>Its called <strong>Iterative and Evolutionary</strong> because feedbacks and adaptation evolves the specification and design.<br /></li><li>Each iteration follows a model of Requirements analysis, followed by Design, followed by Implementation and test and finally complete system Integration test.<br /></li><li><div>How changes are handled in such an iterative approach?<br /></div><ul><li>The only thing that's permanent is <strong>Change</strong> its inevitable, so there is no point confronting it - embrace change instead.<br /></li><li>Having said that, the iterative development doesn't encourage or invoke sudden changes, it tries to ease the process of change.<br /></li><li>The early iterations can help in getting quick feedback from the business user, developer and tester - this minimizes speculation, and helps reaping the actual requirements and validating the path of development.<br /></li><li>Its easy to understand that work proceeds through a series of build-feedback-adapt cycles. The early iterations will no doubt, have some deviations from the actual requirements or the true path, but it will align itself to the true path later on.<br /></li></ul></li><li><div>There are multiple benefits of the Iterative Development:<br /></div><ul><li>Projects are less likely to fail.<br /></li><li>Early rather than late mitigation of risks like technical, requirements, objectives, usability etc.,<br /></li><li>Visible progress<br /></li><li>Early feedbacks, user engagement, and adaptation leads to a refined and acceptable system<br /></li><li>Complexity is managed - the team is saved from analysis-paralysis or very long and complex steps - they have portions in their plate that they can eat easily.<br /></li></ul></li><li><div>The length of an iteration is equally important - it doesn't make sense to have a 6-week iteration in a 2 month long project.<br /></div><ul><li>Short is good<br /></li><li>Small steps, rapid feedback and adaptation are at the core of Iterative development.<br /></li><li>A key idea is that whatever be the best length it has to be <strong>timeboxed </strong>- fixed. The partial system must be integrated, tested, and stabilized by the scheduled date - no exceptions at that.<br /></li></ul></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-27492997088261079382009-08-30T12:16:00.001+05:302009-08-30T12:18:15.164+05:30Analysis and Design - Heads up<span xmlns=''><ul><li><div><strong>Analysis</strong> emphasizes on investigation of the problem and requirements. It's a broad term, so we will elaborate on the key ones:<br /></div><ul><li><div>Requirements analysis<br /></div><ul><li>Investigates requirements<br /></li></ul></li><li><div>Object oriented analysis<br /></div><ul><li>Investigates domain objects<br /></li></ul></li></ul></li><li><div>During the analysis of the problem and its requirements when a project is started the points to discuss are:<br /></div><ul><li>How is it going to be used?<br /></li><li>What problems it solves?<br /></li><li>What are its functions?<br /></li></ul></li><li>During an object oriented analysis the focus is on finding and describing the objects or concepts in the problem domain. e.g. Department, Designation and Role in a Payroll system.<br /></li><li><div><strong>Design</strong> emphasizes on a conceptual solution that is supposed to fulfill the requirements and doesn't care about implementation yet.<br /></div><ul><li>A description of a database schema and domain objects<br /></li></ul></li><li>During object oriented design the focus is on defining the objects or concepts and figuring out how they collaborate with each other to fulfill a requirement. e.g. a Department may have many designations, and a designation may have different roles.<br /></li><li>Object oriented programming follows analysis and design.<br /></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-64964981520202004482009-08-30T11:49:00.003+05:302009-08-30T11:49:57.411+05:30What seasoned Architects say about the craft<span xmlns=''><ul><li>I cannot stress this enough: Keep working to clarify your understanding of the desired solution. If you come from a development background, your disposition is to start working on a solution from the moment that the problem is stated. The effect that this has is that the rest of the conversation sometimes gets tuned out while your mind turns to solving the problem that you have heard. It is important to resist this urge and pay attention, so that you can understand what the person is saying. Ask questions, and challenge the things that you do not understand.<br /></li><li>In India, the general answer was always immediately, "Yes, it can be done," and then the developers would go off to huddle and try to figure out what they thought was being asked for in the solution. In Eastern Europe, every request was responded to with a barrage of questions, and unclear ideas were challenged until everyone had a clear understanding of the desired solution.<br /></li><li>Communications skills are the skills that I work on the most, because they are the most critical for an architect. Without top communications skills, it's going to be difficult to get your vision into the heads of others. The highest responsibility of an architect is to communicate the solution to the business leaders, technical leaders, and any other interested parties in a language that they understand. <br /></li><li> Architects must have a mastery of three languages: business language, for communicating with the business people; industry language, for communicating in the vernacular of the vertical; and technical language, for communicating to the technical leadership and the developers.<br /></li><li> You must become familiar with the domain in which you will be developing a solution. I believe that there is a tremendous benefit to having experience in multiple industries and domains. This allows you to be like a bumble-bee and cross-pollinate the best ideas across industries. I have found also that the DNA of most architects contains a hunger for knowledge in a broad range of subject areas. Architects are interested in knowing about things and understanding how things work. Architects can then synthesize this knowledge into creating solutions.<br /></li><li>As architects, our minds can be far into the future (as they should be), but we also should not assume that the rest of the team can see that far. We must communicate, educate, and mentor the team to our level of understanding, so that they can understand the full vision. Be like a U.S. Marine: Leave no one behind.<br /></li><li>I wear a Microsoft Xbox 360 wristband that reads, "Challenge Me"—a statement that I think gets to the heart of what drives a solutions architect. IT solutions architecture is a constantly evolving field, with an ever-increasing set of new challenges. Albert Einstein once said, <strong>"You cannot solve tomorrow's problems with today's level of thinking." </strong>We must always be striving to take it to the next level. This has been what has driven me for the past 25 years in IT, and will continue to drive me into the future.<br /></li><li><div>The DNA of an architect is to strive to understand the problem, envision the solution, and then communicate the vision to the folks who will implement the solution. Educate and mentor your team, so that everyone comes away with the same vision.<br /></div><p> <br /> </p><p><strong>Critical-Thinking Questions<br /></strong></p><ul><li>Are you listening to the customer? I mean, really listening to the customer? Are you asking questions to clarify the problem? Are you willing to stay in the envisioning phase until everything is clear?<br /></li><li>What are you going to do today, to sharpen your communication skills?<br /></li><li>Are you stepping back to see the big picture? How far into the future do you see this vision? Can you communicate this vision?<br /></li><li><div>Are you taking the time to bring the team along? Are you making sure that your team understands the vision?<br /></div><p> <br /> </p><p> <br /> </p></li></ul><p>Source:<br /></p><p><a href='http://msdn.microsoft.com/en-us/library/bb447671.aspx'>http://msdn.microsoft.com/en-us/library/bb447671.aspx</a><br /> </p></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-15153505136091123362009-08-30T08:33:00.001+05:302009-08-30T10:07:58.868+05:30Dissecting ‘Hello World’<p>Look at the following piece the age old Hello World program in C#.</p> <p><a href="http://lh3.ggpht.com/_2xIBIxheSrg/SpnzdeYUEZI/AAAAAAAAD-I/f4YtQat7AA4/s1600-h/clip_image001%5B3%5D.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://lh3.ggpht.com/_2xIBIxheSrg/SpnzeFlhGCI/AAAAAAAAD-M/IgJcq9_15rk/clip_image001_thumb.jpg?imgmax=800" width="244" height="117" /></a></p> <p>The above piece of code can be explained as having</p> <ol> <li>Defined a type called Program having a single public static method called Main </li> <li>Main refers to a type called System.Console which is written by Microsoft and the IL code that implements it is in a file called <strong>MsCorLib</strong>.dll </li> <li>To compile the program you could have executed the following statement: <ol> <li><strong>csc</strong>.exe /<strong>out</strong>:Program.exe /<strong>t[arget]:</strong>exe /<strong>r[eference]</strong>:MSCorLib.dll Program.cs </li> </ol> </li> <li>But, the following statement also works: <ol> <li><strong>csc</strong>.exe Program.cs </li> </ol> </li> <li>Reason being the fact that the compiler has some defaults and reference to <strong>MsCorLib</strong>.dll is one such default. Please note that <strong>MsCorLib</strong>.dll is the most critical of all .Net BCLs. It contains all the core types like String, Int32 etc, exe is another default. </li> <li>However, if you want to beak it try the following: <ol> <li><strong>csc</strong>.exe /<strong>nostdlib</strong> Program.cs </li> <li>/<strong>nostdlib</strong> asks the compiler to ignore <strong>MsCorLib</strong>.dll. If this switch is breaking a simple statement then why is it useful, you might ask. </li> <li>The reason is simple when the compiler compiles <strong>MsCorLib</strong>.dll is doesn’t need a reference to <strong>MsCorLib</strong>.dll does it? It uses the /<strong>nostdlib</strong> then. </li> </ol> </li> </ol> <p>As a result of the above command, a simple executable file called Program.exe is now created. Lets dive a little deeper into it.</p> <ol> <li>It’s a PE (Portable Executable) having the following:</li> <ol> <li>PE header(32/32+)</li> <li>CLR header</li> <li>Metadata</li> <ol> <li>To examine the Metadata within a managed PE file use <strong>ILDasm</strong>.exe</li> </ol> <li>IL</li> </ol> <li>It's also an <strong>Assembly</strong>.</li> </ol> <p>See you soon!</p> Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-3183703868024604342009-08-29T16:37:00.001+05:302009-08-29T16:37:15.735+05:30A quick note on IL<ul> <li>IL is a CPU-independent machine language created by Microsoft.</li> <li>It is a much higher level language than most CPU machine languages. Its part of the managed module that compiler creates out of source code. </li> <li>It can access and manipulate object types and has instructions to create and initialize objects, call virtual methods on objects, and manipulate array elements directly. It even has instructions to throw and catch exceptions for error handling.</li> <li>IL can be written in Assembly language.</li> <li>Bear in mind that any high level language, most likely, will expose only a subset of facilities provided by the CLR. However, the IL assembly language will expose all the facilities provided by CLR. So, in case you want to use any CLR facility your preferred language doesn't expose you have two options:</li> <ul> <li>Use IL to code the desired part</li> <li>Use any other CLR language that exposes the missing facility.</li> </ul> <li>This is an amazing feature provided by the CLR where you can use specialized language to perform a specific task in your project. Often overlooked, but wouldn't it be great to perform normal operations like I/O in C#/VB.Net and leverage APL for engineering calculations?</li> </ul> Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-63421155700127119602009-08-29T16:17:00.003+05:302009-08-29T16:18:13.861+05:30Understanding Metadata<span xmlns=''><ul><li>In addition to emitting IL, every compiler targeting the CLR is required to emit full metadata into every managed module.<br /></li><li>The metadata is a set of data tables that describe what is defined in the module, such as types and their members.<br /></li><li>It also has tables indicating what the managed module references, such as imported types and their members.<br /></li><li>Metadata is a superset of older technologies such as Type Libraries and Interface Definition Language (IDL) files. Its embedded in the same exe/dll which means that's its impossible to separate IL and Metadata.<br /></li><li><div>It has many other benefits like:<br /></div><ul><li>Header and library files are not needed anymore as it contains all type information. Compilers can read metadata directly from managed modules.<br /></li><li>It allows IntelliSense in Visual studio to work.<br /></li><li>CLRs code verification process ensures that the code is "safe" using metadata.<br /></li><li>It helps in recreating objects state across processes or machines as the object's fields can be serialized and de-serialized in memory and sent across.<br /></li><li>It allows garbage collector to track the life time of objects because of the type and object information it contains.<br /></li></ul></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-67857560507231180752009-08-29T16:17:00.001+05:302009-08-29T16:18:13.862+05:30Understanding Managed Modules<span xmlns=''><ul><li>The common language runtime (CLR) is just what its name says it is; a runtime that is usable by different and varied programming languages. The features of the CLR are available to any and all programming languages that target it—period.<br /></li><li>In fact, at runtime, the CLR has no idea which programming language the developer used for the source code. This means that you should choose whatever programming language allows you to express your intentions most easily.<br /></li><li>When you compile the English like source code written in your preferred language, C# is most preferred I guess, the compiler does the syntax checking and source code analysis. It then produces a <strong>Managed Module</strong> .<br /></li><li>A managed module is a standard 32-bit <strong>Microsoft Windows portable executable (PE32)</strong> file or a standard 64-bit <strong>Windows portable executable (PE32+)</strong> file that requires the CLR to execute.<br /></li><li>Microsoft ships two command-line utilities, <strong>DumpBin</strong>.exe and <strong>CorFlags</strong>.exe, that you can use to examine the header information emitted in a managed module by the compiler.<br /></li><li><div>The following are the parts of a managed module:<br /></div><ul><li><div>PE32 or PE32+ header<br /></div><ul><li>If the header uses the PE32 format, the file can run on a 32-bit or 64-bit version of Windows. If the header uses the PE32+ format, the file requires a 64-bit version of Windows to run.<br /></li><li>For modules that contain only IL code, the bulk of the information in the PE32(+) header is ignored. For modules that contain native CPU code, this header contains information about the native CPU code.<br /></li></ul></li><li><div>CLR Header<br /></div><ul><li>Contains the information (interpreted by the CLR and utilities) that makes this a managed module.<br /></li><li>The header includes the version of the CLR required, some flags, managed module's entry point method (Main method), and the location/size of the module's metadata, resources, strong name etc.<br /></li></ul></li><li><div>Metadata<br /></div><ul><li>Every managed module contains metadata tables. <br /></li><li><div>There are two main types of tables: <br /></div><ul><li>tables that describe the types and members <strong>defined in your source code</strong> and <br /></li><li>tables that describe the types and members <strong>referenced by your source code</strong>.<br /></li></ul></li></ul></li><li><div>Intermediate Language (IL) code<br /></div><ul><li>It's the code the compiler produces as it compiled the source code. At run time, the CLR compiles the IL into native CPU instructions.<br /></li></ul></li></ul></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-48718291878416748062009-08-29T09:39:00.003+05:302009-08-29T09:40:46.121+05:30Zachman Framework<span xmlns=''><ul><li>One of the popular frameworks originated at IBM and is called the Zachman Framework. The Zachman Framework predated the popularity of object orientation and took the perspective of separating data from process. <br /></li><li><div>The Zachman Framework formed a matrix of architectural descriptions that are also organized in terms of levels. <br /></div><ul><li>There are five levels of description above the information system implementation. <br /></li><li>They range from architectural planning done by individual programmers at the finest grain to the overall enterprise requirements from the investors' perspective of the information system. <br /></li><li>In total, the Zachman Framework identifies thirty architectural specifications, which provide a complete description of the information system. In practice, no real-world project is capable of creating these thirty or more detailed plans and keeping them all in synchronization. <br /></li><li>When the Zachman Framework is applied, systems architects partition the viewpoint into various categories and create architectural specifications that cover some or all of the different Zachman descriptions without having to create the large number of individual specification documents that the Zachman Framework spans.<br /></li></ul></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-16426984173686694032009-08-29T09:39:00.001+05:302009-08-29T09:40:46.122+05:30The need for Software Architecture<span xmlns=''><p style='margin-left: 22pt'>To accommodate system complexities in the world of distributed processing, Architects have three new needs:<br /></p><ol><li><div>First, architects need the ability to separate complex concerns, in particular to separate concerns about business-application functionality, from concerns about distributed-system complexity. <br /></div><ol><li>Problems and challenges of distributed computing have nothing to do fundamentally with business-application functionality.<br /></li><li>In a typical application, 70% of application code is infrastructure. Some of this code is unique to the application even though it might not directly address business requirements. By separating concerns, developers can focus on the business functionality that is the true purpose of the information system.<br /></li></ol></li><li><div>Software architects also need the ability to future-proof the information systems that they are planning. <br /></div><ol><li>It is important to accommodate commercial technology evolution, which is known to be accelerating and provides substantial challenges for architects and developers. <br /></li><li>Future-proofing also requires the ability to adapt to new user requirements, since requirements do change frequently and account for a majority of system software costs over the life cycle. <br /></li><li>It is important to plan information systems to support the likely and inevitable changes that users will require in order to conduct business.<br /></li></ol></li><li><div>A third need for software architects is the ability to increase the likelihood of system success. <br /></div><ol><li>Corporate developers to date have had a very poor track record of creating successful systems. <br /></li><li>The software architect is responsible for planning systems with the maximum probability of delivering success and key benefits for the business. <br /></li><li>Through proper information technology planning, it is possible to increase the likelihood of system delivery on time and on budget.<br /></li></ol><p> <br /> </p></li></ol></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-74147565212956573412009-08-29T09:36:00.001+05:302009-08-29T09:38:49.535+05:30Open Distributed Processing<span xmlns=''><ul><li>Among the various architectural approaches, a useful international standard, the Reference Model for Open Distributed Processing (RM-ODP), defines what information systems architecture means [ISO 1996].<br /></li><li><div>RM-ODP defines five essential viewpoints for modeling systems architecture:<br /></div><ul><li>Enterprise viewpoint<br /></li><li>Information viewpoint<br /></li><li>Computational viewpoint<br /></li><li>Engineering viewpoint<br /></li><li>Technology viewpoint<br /></li></ul></li><li><div>The five viewpoints provide a comprehensive model of a single information system, with each viewpoint being a perspective on a single information system. The set of viewpoints is not closed, so that additional viewpoints can be added as the needs arise.<br /></div><ul><li>Another of their purposes is to provide information descriptions that address the questions and needs of particular stakeholders in the system. <br /></li><li>By standardizing five viewpoints, RM-ODP is claiming that these five stakeholder perspectives are sufficient for resolving both business functionality and distributed systems issues in the architecture and design of information systems. <br /></li></ul></li><li><div><strong>The enterprise viewpoint</strong> of the RM-ODP takes the perspective of a business model. <br /></div><ul><li>Managers and end users in the business environment should be able to understand the enterprise models readily. <br /></li><li>The enterprise viewpoint ensures that business needs are satisfied through the architecture and provides a description that enables validation of these assertions with the end users.<br /></li></ul></li><li><div><strong>The information viewpoint</strong> defines the universe of discourse in the information system. <br /></div><ul><li>The perspective is similar to the design information generated by a database modeler. <br /></li><li>The information viewpoint is a logical representation of the data and processes on data in the information system.<br /></li><li>The information viewpoint is an object-oriented logical model of the information assets in the business and how these assets are processed and manipulated.<br /></li></ul></li><li><div><strong>The computational viewpoint</strong> partitions the system into software components that are capable of supporting distribution. <br /></div><ul><li>It takes the perspective of a designer of application program interfaces for component-ware. <br /></li><li>The computational viewpoint defines the boundaries between the software elements in the information system. <br /></li><li>Generally, these boundaries are the architectural controls that ensure that the system structure will embody the qualities of adaptability in management of complexity that are appropriate to meet changing business needs and incorporate the evolving commercial technology.<br /></li></ul></li><li><div><strong>The engineering viewpoint</strong> of RM-ODP exposes the distributed nature of the system. <br /></div><ul><li>Its perspective is similar to that of an operating system engineer who is familiar with the protocol stacks and allocation issues necessary to define the distributed processing solutions for the information system.<br /></li></ul></li><li><div><strong>The technology viewpoint</strong> defines the mappings between the engineering objects and other objects designed to specific standards and technologies including product selections. <br /></div><ul><li><div>The viewpoint is similar to that of a network engineer who is familiar with the commercially available protocol standards and products that are appropriate selections to configure the information system.<br /></div><p> <br /> </p></li></ul><p>The RM-ODP viewpoints enable the separation of concerns that divide the business and logical functionality of the system from the distributed computing and commercial technology decisions of the architecture.<br /></p></li></ul></span>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-21034050009170109652008-10-10T16:19:00.002+05:302008-10-10T16:55:55.593+05:30Business Logic in C# or T-Sql? This has been a topic of debate for a long time. If you think this discussion is trivial, you need to evolve. Read on if you <br />think you got something to think. Practically developers, and designers prefer to use stored procedures simply because of one of the following reasons:<br><br> - Code looks good! They get a feeling that at least they have done something right in their life. They keep ridiculing others for having used in-line queries. They once read somewhere that SPs are the way to go.<br><br> - The above category is the follower - mindless follower. They are hard to convince and they will follow the same practice for years. Never mind. They also came to know in their college days that SPs are "precompiled", "fast", and "secure". They won't be able to tell you the whys and hows of things.<br><br> - Some experienced and good developers attribute the usage of SPs over parametrized queries by saying that it saves you a lot of Network bandwidth, and gives you a single point of failure. You know where things can go wrong.<br><br> - The flag bearers of business-logic-in-application-language-like-C# will tell you that the purpose of an RDBMS is to store data, and the act of manipulating it should be done on a higher level language. Languages like C# arm you with better weapons than T-Sql could ever do. T-Sql is Set based and Procedural thus monolithic. You are not living in caves anymore, are you?<br><br> - What about Sql injection attacks? They are dreaded weapons of hackers. How does parametrized query work this out? Well, they are as safe as SPs. Both are parametrized, aren't they? Its different that in-line queries. In-line queries are prone to attacks. However, there are ways to break into SPs as well, so you are not safe anyways!<br><br> - Are you talking about doing away with SPs altogether? Certainly not. <br>You need to decide whats best for the purpose. CRUD operations doesn't seem logical to me anymore. Imagine having a about 100 normalised tables in your database. Would you like to have 100*4 SPs doing the normal Insert, Update, Select and Delete for you? Doesn't seem so convincing. Moreover, the performance benefits of SPs and queries have been diminishing since Sql Server 7.0.<br>As per books online:<br><font size="1"><i>SQL Server 2000 and SQL Server version 7.0 incorporate a number of<br />changes to statement processing that extend many of the performance<br />benefits of stored procedures to all SQL statements. SQL Server 2000<br />and SQL Server 7.0 do not save a partially compiled plan for stored<br />procedures when they are created. A stored procedure is compiled at<br />execution time, like any other Transact-SQL statement. SQL Server 2000<br />and SQL Server 7.0 retain execution plans for all SQL statements in the<br />procedure cache, not just stored procedure execution plans.</i></font><br><br> - If you are planning to be object based and enjoy the rich benefits of C# as a language you must consider LINQ, or any other efficient O/R mapper like NHibernate. You will start loving it. You will love the feeling of having used pragmatic best practices and approaches.<br><br> - If you have Business Logic written in C# it implies that you can do Unit Testing with Code Coverage. If you need to traverse the rows its much easier, as it doesn't have to create #Temp tables in tempDB for Cursors. The DBA is no longer required to write complicated, hard to maintain thousands of lines of monolithic code. He can focus on database management, fine tuning, backup, replication and other important stuff! You don't need to explain the business to your DBAs. Let them be specialised in what they are doing.<br><br> - I am not sure how successfully it isolates dependency on a platform(like Oracle or Sql Server), but it does relieve you of maintaining tons of unnecessary SPs.<br><br> - Bad C# coding is certainly not a replacement of SPs. If you are writing business logic in a highly evolved language like it, you need to make sure that you are following best coding practices and patterns. If you end up writing monolithic procedural code in C#, you need to evolve before you can understand the true benefits of this discussion.<br><br>- What about debugging? I don't think that's a big deal. You can use Sql Profiler for the parameters. Agreed that you can't have IDE like debugging but Sql Server IDE is sufficient to trace a bug. You can also unit test the output generated from the database in your application with NUnit. So if you have good coverage of the SP side code, you will get to know what all you have broken as a result of the changes you made. Neat. So, this doesn't convince me to port my business logic to C#.<br><br> - SPs can't be replaced in Data driven applications like report generators, but its advisable to code the business logic in C# for applications that do data manipulation like a Financial calculator!<br><br>You may consider reading the following articles for more details.<br><a title="Dude, where's my business logic?" href="http://www.codeproject.com/KB/architecture/DudeWheresMyBusinessLogic.aspx" id="vhvb">Dude, where's my business logic?</a> <br><a title="Stored procedures are bad, m'kay?" href="http://weblogs.asp.net/fbouma/archive/2003/11/18/38178.aspx" id="ck90">Stored procedures are bad, m'kay?</a> <br><br>This one is interesting because it has people fighting for their favorite approach.<br><a title="Who Needs Stored Procedures, Anyways?" href="http://www.codinghorror.com/blog/archives/000117.html" id="nh0-">Who Needs Stored Procedures, Anyways?</a><br><br><b>My take on this:</b> Be pragmatic. Be need driven. Be domain driven.<br><br>C#: Could be any programming language.<br>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-31335238546678792692008-10-03T16:52:00.003+05:302008-10-13T10:23:50.769+05:30Must HavesFollowing is the list of must-haves; most of the Architects are aware of it. However, if you are an aspiring architect you should start from here.<br /><br />Do remember that there are two ways of achieving a result: The easier way that always looks easy; and the not-so-easier-looking way which is actually fun and easy to walk on. If you are passionate about doing things in style, and quickly, and you want to be in complete control, always follow the industry best practices. Always look out for something new, keeping in mind the OO school of thought, you'll never go wrong. Frameworks will come, and go, but you need to know what triggered the need for that framework. How did they made it possible? What goes inside making such great stuff?<br /><br />I am contributing in a small way a list that I have compiled today.<br /><br />1. Frameworks<br />- <a title="Smart Client Software Factory" href="http://msdn.microsoft.com/en-us/library/aa480482.aspx" id="k13j">Smart Client Software Factory</a> for WinForms UI<br />- <a title="Web Client Software Factory" href="http://msdn.microsoft.com/en-us/library/bb264518.aspx" id="tfm7">Web Client Software Factory</a> for Web UI<br />- <a title="Spring.Net" href="http://www.springframework.net/" id="nv4v">Spring.Net</a> for AOP, DI and IoC, Logging, Data handling<br />- <a title="Unity Framework" href="http://msdn.microsoft.com/en-us/library/cc468366.aspx" id="w75b">Unity Framework</a> for DI and IoC<br />- Various Microsoft Software Factories; Enterprise Application Blocks<br />- <a title="Log4Net" href="http://logging.apache.org/log4net/index.html" id="s7d1">Log4Net</a> for logging<br /><br />2. Tools<br />- <a title="Resharper" href="http://www.jetbrains.com/resharper/" id="bshe">Resharper</a> for UnitTests and Code Analysis<br />- <a title="NCover" href="http://www.ncover.com/" id="w5qq">NCover</a><br />- <a title="NMock" href="http://www.nmock.org/index.html" id="wxhl">NMock</a><br />- <a title="NHibernate" href="http://www.hibernate.org/343.html" id="kcp.">NHibernate</a> for O/R Mapping<br />- <a title="Simian" href="http://www.redhillconsulting.com.au/products/simian/" id="bjqt">Simian</a> for Code duplication and Refactoring<br />- <a title="StyleCop" href="http://code.msdn.microsoft.com/sourceanalysis" id="v3-u">StyleCop</a> for Code Styling<br />- <a title="CLR Profiler" href="http://msdn.microsoft.com/en-us/library/ms979205.aspx" id="p9.8">CLR Profiler</a>/ANTS profiler for application profiling<br /><br />3. Architecture<br />- <a title="MVP/MVC" href="http://martinfowler.com/eaaDev/uiArchs.html" id="su9h">MVP/MVC</a><br />- <a href="http://msdn.microsoft.com/en-us/library/aa905335.aspx" id="jqig" title="Pragmatic Architecture">Pragmatic Architecture</a><br />- <a title="SOA" href="http://en.wikipedia.org/wiki/Service-oriented_architecture" id="o.el">SOA</a><br /><br />4. Software Engineering<br />- <a title="Extreme Programming" href="http://www.extremeprogramming.org/" id="dixp">Extreme Programming</a><br />- <a title="Agile Methodology" href="http://martinfowler.com/articles/newMethodology.html" id="i8g6">Agile Methodology</a><br /><br />5. Books<br />- Head First Design Patterns<br />- Head First OOAD<br />- Code Complete 2<br />- Patterns of Enterprise Application Architecture<br />- Test Driven Development<br />- Domain Driven Develeopment<br /><br />6. Practices<br />- <a title="TDD" href="http://en.wikipedia.org/wiki/Test-driven_development" id="l3-1">TDD</a><br />- <a title="DDD" href="http://en.wikipedia.org/wiki/Domain-driven_design" id="w5.l">DDD</a><br />- <a title="MDD" href="http://msdn.microsoft.com/en-us/library/aa964145.aspx" id="nzym">MDD</a><br /><br />7. Blogs<br /><a href="http://www.noop.nl/2008/09/top-100-blogs-for-development-managers-q3-2008.html" id="yui9" title="Top 100 Blogs ever!!!">Top 100 Blogs ever!!!</a><br /><a href="http://www.noop.nl/2008/06/top-100-best-software-engineering-books-ever.html" id="oxn1" title="Top 100 Software Engineering books ever!!!">Top 100 Software Engineering books ever!!!</a>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-37271503193073139932008-09-24T10:07:00.007+05:302008-09-24T10:33:17.544+05:30Using Code Generator to Create Sql Mapper Class<iframe scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:240px;height:66px;margin:3px;padding:0;border:1px solid #dde5e9;background-color:#ffffff;" src="http://cid-d13e955a6ac10873.skydrive.live.com/embedrowdetail.aspx/CSharpCode/CodeDOM"></iframe><br /><br />Good to see you again!!<br /><br />This sample code used <a href="http://msdn.microsoft.com/en-us/library/650ax5cx.aspx">CodeDOM(System.CodeDOM)</a> to create a code generator. The purpose is dual. It not only demonstrates how to use CodeDOM but also the way to automate a Sql input mapper generator.<br /><br />Internally it uses <a href="http://msdn.microsoft.com/en-us/library/ms131540.aspx">SqlDMO</a> COM dll to access a Sql Server database. It then fetches the required Stored Procedure, and exposes the input parameters.<br /><br />The downloadable code contains the code fit for the purpose. Its as generalised as it should be. Feel free to use it.<br /><br />The following is the class diagram for the CodeDOMDemo classes:<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_2xIBIxheSrg/SNnGXfTE_RI/AAAAAAAADGk/hF5T884czY4/s1600-h/CodeDOMDemo.jpg"><img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_2xIBIxheSrg/SNnGXfTE_RI/AAAAAAAADGk/hF5T884czY4/s320/CodeDOMDemo.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5249444947517832466" /></a><br /><br />The following is class diagram for the SqlCodeDOM classes:<br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_2xIBIxheSrg/SNnGlzHDlnI/AAAAAAAADGs/6ugm8OIPr_I/s1600-h/SqlDMO.jpg"><img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_2xIBIxheSrg/SNnGlzHDlnI/AAAAAAAADGs/6ugm8OIPr_I/s320/SqlDMO.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5249445193354286706" /></a><br /><br />The following fixture gives you some of the usage scenarios.<br /><br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="kwrd">using</span> NUnit.Framework;<br /><span class="kwrd">using</span> Sanjeet.Demos.CodeDOMDemo;<br /><br /><span class="kwrd">namespace</span> Sanjeet.Demos<br />{<br /> [TestFixture]<br /> <span class="kwrd">public</span> <span class="kwrd">class</span> GenerateStoredProcFixture<br /> {<br /> <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">string</span> serverName = <span class="str">"serverName"</span>;<br /> <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">string</span> username = <span class="str">@"username"</span>;<br /> <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">string</span> password = <span class="str">"password"</span>;<br /> <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">string</span> databaseName = <span class="str">"databaseName"</span>;<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GenerateCounterParty()<br /> {<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> spName = <span class="str">"GetCounterParties"</span>;<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> filename = <span class="str">@"c:\CounterParty.cs"</span>;<br /> GenerateStoredProcedure sp =<br /> <span class="kwrd">new</span> GenerateStoredProcedure(serverName, username, password, databaseName, spName, filename,<br /> <span class="str">"Sanjeet.Data"</span>);<br /> sp.Generate();<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GenerateDealsEnteredbyAllCounterParty()<br /> {<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> spName = <span class="str">"GetDealsEntered"</span>;<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> filename = <span class="str">@"c:\DealsEnteredbyAllCounterParty.cs"</span>;<br /> GenerateStoredProcedure sp =<br /> <span class="kwrd">new</span> GenerateStoredProcedure(serverName, username, password, databaseName, spName, filename,<br /> <span class="str">"Sanjeet.Data"</span>);<br /> sp.Generate();<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GenerateDealsEnteredbyCounterParty()<br /> {<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> spName = <span class="str">"DealsExport"</span>;<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> filename = <span class="str">@"c:\DealsEnteredbyCounterParty.cs"</span>;<br /> GenerateStoredProcedure sp =<br /> <span class="kwrd">new</span> GenerateStoredProcedure(serverName, username, password, databaseName, spName, filename,<br /> <span class="str">"Sanjeet.Data"</span>);<br /> sp.Generate();<br /> }<br /> }<br />}</pre><br /><br />Happy Coding!Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-19480749456391303522008-09-23T15:44:00.003+05:302008-09-23T16:02:53.050+05:30CSS Parser<iframe scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:240px;height:66px;margin:3px;padding:0;border:1px solid #dde5e9;background-color:#ffffff;" src="http://cid-d13e955a6ac10873.skydrive.live.com/embedrowdetail.aspx/CSharpCode/CSSParser/CSSParser.zip"></iframe><br /><br />Presenting to you a simple piece of code that parses CSS. It uses Regular Expressions to achieve the functionality.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_2xIBIxheSrg/SNjFbmAxOKI/AAAAAAAADGc/rMfrQE6-uog/s1600-h/CSSParser.jpg"><img style="cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_2xIBIxheSrg/SNjFbmAxOKI/AAAAAAAADGc/rMfrQE6-uog/s320/CSSParser.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5249162443551226018" /></a><br /><br />The sample code below gives you an idea how to use it.<br /><br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="rem">/*</span><br /><span class="rem">*</span><br /><span class="rem">* Date Created: 02-Sep-2008</span><br /><span class="rem">* Author: SAHAY, Sanjeet, IDC</span><br /><span class="rem">* Filename: CssParserFixture.cs</span><br /><span class="rem">* Assembly: Sanjeet.Demos</span><br /><span class="rem">* Project: Demos</span><br /><span class="rem"></span><br /><span class="rem"></span><br /><span class="rem"></span><br /><span class="rem">* Purpose: Add purpose</span><br /><span class="rem">*</span><br /><span class="rem">*/</span><br /><span class="kwrd">using</span> System.Collections.Generic;<br /><span class="kwrd">using</span> NUnit.Framework;<br /><span class="kwrd">using</span> Sanjeet.Demos.CSSParser;<br /><br /><span class="kwrd">namespace</span> Sanjeet.Demos<br />{<br /> [TestFixture]<br /> <span class="kwrd">public</span> <span class="kwrd">class</span> CssParserFixture<br /> {<br /> <span class="kwrd">private</span> IDictionary<<span class="kwrd">string</span>, <span class="kwrd">string</span>> properties;<br /><br /> <span class="kwrd">private</span> IContainer GetContainer()<br /> {<br /> <span class="kwrd">const</span> <span class="kwrd">string</span> cssFilepath = <span class="str">@"..\..\..\CssParser\StyleSheet.css"</span>;<br /> <span class="kwrd">return</span> <span class="kwrd">new</span> DefaultContainer(cssFilepath);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetButtonsClip()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"Buttons"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties[<span class="str">"clip"</span>].Equals(<span class="str">"rect(auto auto auto auto)"</span>));<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetButtonsPropertiesCount()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"Buttons"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties.Count == 16);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetH1Color()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"H1"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties[<span class="str">"color"</span>].Equals(<span class="str">"blue"</span>));<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetH1FontStyle()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"H1"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties[<span class="str">"font-style"</span>].Equals(<span class="str">"italic"</span>));<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetH1PropertiesCount()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"H1"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties.Count == 2);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetMasterTablePropertiesCount()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"MasterTable"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties.Count == 8);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetNameLabelPropertiesCount()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"NameLabel"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties.Count == 4);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetUL()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"UL"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties.Count == 4);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> GetULFontWeight()<br /> {<br /> IContainer container = <span class="kwrd">this</span>.GetContainer();<br /> <span class="kwrd">this</span>.properties = container.GetProperties(<span class="str">"UL"</span>);<br /> Assert.IsTrue(<span class="kwrd">this</span>.properties[<span class="str">"font-weight"</span>].Equals(<span class="str">"bold"</span>));<br /> }<br /> }<br />}</pre>Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com1tag:blogger.com,1999:blog-8486705759701449267.post-71538317189117364102008-09-23T14:48:00.010+05:302008-09-23T15:39:15.903+05:30Abstract Factory Pattern<iframe scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="width:240px;height:66px;margin:3px;padding:0;border:1px solid #dde5e9;background-color:#ffffff;" src="http://cid-d13e955a6ac10873.skydrive.live.com/embedrowdetail.aspx/CSharpCode/DesignPatterns/AbstractFactory.zip"></iframe><br /><br /><b>Purpose:</b><br /> The Abstract Factory Pattern involves the following OO priciple: Depend upon abstractions, do not depend upon concerete classes.<br /><br />Abstraction doesn't necessarily mean an Interface. It points to a super type. It could easily be an Abstract class.<br /><br /><b>Encapsulating Object Creation.</b> <i>But Why?</i><br />We all have been "newing" for a long time without worries. Now we say that its better to encapsulate object creation. How is it going to help us? We are "newing" anyways!<br /><br />The problem is certainly not with "new". Its the change that brings all the change. We need a way to encapsulate all the related "newing" at a single place so that its not spread across the code. In this example, a NuclearOrdananceFactory(Factory) is entrusted with creating Bullets, Guns, and Bombs(Products). Whenever Russia or China(Client) needs to create Nuclear bombs it goes to the Factory. It simply creates relevant weapons. No hassles, no worries, no code duplication, and no maintenance nightmares.<br /><br /><b>Abstract.</b> <i>Why Abstract?</i><br />The Factory classes like NuclearOrdnanceFactory, and BiologicalOrdnanaceFactory implement an Abstract interface. This allows the Client to create a set of related products.<br /><br />I am presenting to you the most common design pattern that is also widely mis-understood! I am trying to simplify things by giving you a code sample and a test fixture that explains the usage.<br /><br />It involves three distinct participants: The Factory, The Product, and The Client. The following class diagram will help ease down things.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_2xIBIxheSrg/SNi3NYHJZoI/AAAAAAAADGQ/_NleTAjLz_8/s1600-h/AbstractFactory.jpg"><img style="cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_2xIBIxheSrg/SNi3NYHJZoI/AAAAAAAADGQ/_NleTAjLz_8/s320/AbstractFactory.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5249146806138922626" /></a><br /><br />The following fixture explains the usage of the factory.<br /><br /><!-- code formatted by http://manoli.net/csharpformat/ --><br /><pre class="csharpcode"><br /><span class="rem">/*</span><br /><span class="rem">*</span><br /><span class="rem">* Date Created: 16-Sep-2008</span><br /><span class="rem">* Author: SAHAY, Sanjeet, IDC</span><br /><span class="rem">* Filename: AbstractFactoryFixture.cs</span><br /><span class="rem">* Assembly: Sanjeet.Demos</span><br /><span class="rem">* Project: Demos</span><br /><span class="rem">* Purpose: The Abstract Factory Pattern involves the following OO priciple:</span><br /><span class="rem"> * Depend upon abstractions, do not depend upon concerete classes</span><br /><span class="rem">*</span><br /><span class="rem">*/</span><br /><br /><span class="kwrd">using</span> NUnit.Framework;<br /><span class="kwrd">using</span> Sanjeet.Demos.Patterns.AbstractFactory.Client;<br /><span class="kwrd">using</span> Sanjeet.Demos.Patterns.AbstractFactory.Factory;<br /><br /><span class="kwrd">namespace</span> Sanjeet.Demos<br />{<br /> [TestFixture]<br /> <span class="kwrd">public</span> <span class="kwrd">class</span> AbstractFactoryFixture<br /> {<br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> CreateABiologicalCountry()<br /> {<br /> IOrdnanceFactory factory = <span class="kwrd">new</span> BiologicalOrdnanceFactory();<br /> ICountry country = <span class="kwrd">new</span> China();<br /> country.MakeWeapon(factory);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> CreateAChemicalCountry()<br /> {<br /> IOrdnanceFactory factory = <span class="kwrd">new</span> ChemicalOrdnanceFactory();<br /> ICountry country = <span class="kwrd">new</span> UnitedStates();<br /> country.MakeWeapon(factory);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> CreateANuclearCountry()<br /> {<br /> IOrdnanceFactory factory = <span class="kwrd">new</span> NuclearOrdnanceFactory();<br /> ICountry country = <span class="kwrd">new</span> India();<br /> country.MakeWeapon(factory);<br /> }<br /><br /> [Test]<br /> <span class="kwrd">public</span> <span class="kwrd">void</span> CreateAStrongCountry()<br /> {<br /> ICountry country = <span class="kwrd">new</span> India();<br /> country.MakeWeapon(<span class="kwrd">new</span> NuclearOrdnanceFactory());<br /> country.MakeWeapon(<span class="kwrd">new</span> ChemicalOrdnanceFactory());<br /> country.MakeWeapon(<span class="kwrd">new</span> BiologicalOrdnanceFactory());<br /> }<br /> }<br />}</pre><br /><br />Cheers!Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0tag:blogger.com,1999:blog-8486705759701449267.post-30862897816905723662008-09-16T15:49:00.002+05:302008-09-16T15:55:00.391+05:30WelcomeWelcome to my new blog.<br /><br />As the name suggests its going to be technical. I have been keeping a lot busy these days. But I hope to spare some time now, and share my technical findings with you all.<br /><br />My area of expertise is .Net and you may call me a OO purist. All these years I have tried my best to convince people around me that there is nothing like R.A.D.(Rapid Application Development). Its a myth. It ends up being S.C.A.D.(Slow and Cumbersome Application Development). I will explain you as we go along.<br /><br />My earlier technical outbursts can be seen <a href="http://blogs.msdn.com/sanjeets">here</a>.Sanjeet Sahayhttp://www.blogger.com/profile/12745891345421145994noreply@blogger.com0