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

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

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

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

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

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

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

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

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

<p>The usage:</p>

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

<p>And</p>

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

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

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

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1147</guid>
		<description><![CDATA[A few weeks back i was discussing with a friend about why Microsoft doesn&#8217;t implement any kind of paging option for result set&#8217;s in SQL Server. It is possible to implement kind of paging with ROW_NUMBER() window function, but in overall it makes code look awful. Microsoft recently release the CTP1 of its next SQL Server codenamed [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks back i was discussing with a friend about why Microsoft doesn&#8217;t implement any kind of paging option for result set&#8217;s in SQL Server. It is possible to implement kind of paging with ROW_NUMBER() window function, but in overall it makes code look awful. Microsoft recently release the CTP1 of its next SQL Server codenamed Denali and after a quick overview and googling i came to a blog post by Dave Ballantynes in the biggest SQL Server community online www.SQLServerCentral.com about paging result set&#8217;s features introduced in Denali. The feature offers very easy way to implement result set paging. The original post can be found here:</p>
<p><a href="http://www.sqlservercentral.com/blogs/dave_ballantynes_blog/archive/2010/11/10/denali-paging_1320_is-it-win_2F00_win-_3F00_.aspx">http://www.sqlservercentral.com/blogs/dave_ballantynes_blog/archive/2010/11/10/denali-paging_1320_is-it-win_2F00_win-_3F00_.aspx</a></p>
<p>While the new ORDER BY Clause definition here:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms188385(SQL.110).aspx">http://msdn.microsoft.com/en-us/library/ms188385(SQL.110).aspx</a></p>
<p>For other new features in Denali please see here:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/bb500435(v=SQL.110).aspx">http://msdn.microsoft.com/en-us/library/bb500435(v=SQL.110).aspx</a></p>
<p>Already starting to love it.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/11/23/sql-server-2011-aka-denali/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Meet me at Qt Developer Days 2010</title>
		<link>http://phalanx.spartansoft.org/2010/09/29/meet-me-at-qt-developer-days-2010/</link>
		<comments>http://phalanx.spartansoft.org/2010/09/29/meet-me-at-qt-developer-days-2010/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 14:55:00 +0000</pubDate>
		<dc:creator>Milot Shala</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1125</guid>
		<description><![CDATA[It has been a while since my last post! I am writing this to inform you that I will be attending Qt Developer Days 2010 in Munich, if you happen to be there during the days, feel free to ping me for a conversation.]]></description>
			<content:encoded><![CDATA[<p><center><img src="http://developer.qt.nokia.com/uploads/image_upload/blog_banner.png"></img></center></p>
<p>It has been a while since my last post!</p>
<p>I am writing this to inform you that I will be attending Qt Developer Days 2010 in Munich, if you happen to be there during the days, feel free to ping me for a conversation.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/09/29/meet-me-at-qt-developer-days-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using LINQ to SQL ORM</title>
		<link>http://phalanx.spartansoft.org/2010/06/03/using-linq-to-sql-orm/</link>
		<comments>http://phalanx.spartansoft.org/2010/06/03/using-linq-to-sql-orm/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 13:45:37 +0000</pubDate>
		<dc:creator>Mentor Ibrahimi</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[ModelView]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LINQ to SQL ORM]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1101</guid>
		<description><![CDATA[One of the hardest things i encounter when i do write LINQ queris, is that i always tend to approach results as i would in T-SQL. Well, recently i read an article about LINQ and the author said that one of the major steps a developer find hard to do, is working with LINQ without [...]]]></description>
			<content:encoded><![CDATA[<p>One of the hardest things i encounter when i do write LINQ queris, is that i always tend to approach results as i would in T-SQL. Well, recently i read an article about LINQ and the author said that one of the major steps a developer find hard to do, is working with LINQ without thinking of the problem in T-SQL way. Well that&#8217;s what i run yesterday, i started writing the query, and debugging, and over analyzing without giving it a clear view of what i wanted. To demonstrate that, let&#8217;s first show the model i was working against:</p>
<p style="text-align: center;">﻿﻿﻿<a href="http://phalanx.spartansoft.org/wp-content/uploads/2010/06/Capture.jpg"><img class="size-medium wp-image-1104 aligncenter" title="LINQ to SQL ORM" src="http://phalanx.spartansoft.org/wp-content/uploads/2010/06/Capture-300x205.jpg" alt="" width="300" height="205" /></a></p>
<p>What i wanted to achieve is to get three most sold pizzas and three most active clients. Although the result is easier that i supposed, as i said, i tended to get them as i think from the T-SQL world. My first approach was like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var topPizzas <span style="color: #008000;">=</span> from pizza <span style="color: #0600FF;">in</span> dataContext.<span style="color: #0000FF;">OrderDetails</span>
&nbsp;
.<span style="color: #0000FF;">GroupBy</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">PizzaTypeID</span><span style="color: #000000;">&#41;</span>
&nbsp;
select <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>pizza.<span style="color: #0000FF;">Key</span>, Total <span style="color: #008000;">=</span> pizza.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Quantity</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
var top3Pizzas <span style="color: #008000;">=</span> topPizzas.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var topCustomers <span style="color: #008000;">=</span> from customer <span style="color: #0600FF;">in</span> dataContext.<span style="color: #0000FF;">Orders</span>
&nbsp;
.<span style="color: #0000FF;">GroupBy</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">CustomerID</span><span style="color: #000000;">&#41;</span>
&nbsp;
select <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>customer.<span style="color: #0000FF;">Key</span>, Total <span style="color: #008000;">=</span> customer.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
var top3Customers <span style="color: #008000;">=</span> topCustomers.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This way we get the right result but we get just the id of the pizza and the sum of quantity orders it was ordered. Whenever we try to project to a pizza type we will hit the wall on full speed asking our selves where is the problem, it &#8220;MUST&#8221; work this way.</p>
<p>But this time must come, &#8220;Wait a minute, what do i want?&#8221; and yeah the answer is top pizzas and customers and probably not some order&#8230; thing <img src='http://phalanx.spartansoft.org/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  and the following query really must work:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var topPizzas <span style="color: #008000;">=</span> from pizza <span style="color: #0600FF;">in</span> dataContext.<span style="color: #0000FF;">PizzaTypes</span>
&nbsp;
orderby pizza.<span style="color: #0000FF;">OrderDetails</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Quantity</span><span style="color: #000000;">&#41;</span> descending
&nbsp;
select pizza<span style="color: #008000;">;</span>
&nbsp;
var top3Pizzas <span style="color: #008000;">=</span> topPizzas.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var topCustomers <span style="color: #008000;">=</span> from customer <span style="color: #0600FF;">in</span> dataContext.<span style="color: #0000FF;">Customers</span>
&nbsp;
orderby customer.<span style="color: #0000FF;">Orders</span>.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> descending
&nbsp;
select customer<span style="color: #008000;">;</span>
&nbsp;
var top3Customers <span style="color: #008000;">=</span> topCustomers.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
And even a shorter version with lambda expressions looks like <span style="color: #0600FF;">this</span><span style="color: #008000;">:</span>
&nbsp;
var topPizzas <span style="color: #008000;">=</span> dataContext.<span style="color: #0000FF;">PizzaTypes</span>
&nbsp;
.<span style="color: #0000FF;">OrderByDescending</span><span style="color: #000000;">&#40;</span>pizza <span style="color: #008000;">=&gt;</span> pizza.<span style="color: #0000FF;">OrderDetails</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Quantity</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var top3Pizzas <span style="color: #008000;">=</span> topPizzas.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var topCustomers <span style="color: #008000;">=</span> dataContext.<span style="color: #0000FF;">PizzaTypes</span>
&nbsp;
.<span style="color: #0000FF;">OrderByDescending</span><span style="color: #000000;">&#40;</span>pizza <span style="color: #008000;">=&gt;</span> pizza.<span style="color: #0000FF;">OrderDetails</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Quantity</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var top3Customers <span style="color: #008000;">=</span> topCustomers.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This is probably just a first step to forgetting T-SQL queries (perhaps not DDL <img src='http://phalanx.spartansoft.org/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ) and looking ahead! Don&#8217;t hesitate to ask yourself what you want, the answer is always inside your self&#8230; hahaha and no i&#8217;m not from social sciences, just joking.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/06/03/using-linq-to-sql-orm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automatic text generator using Markov Chains (in C#)</title>
		<link>http://phalanx.spartansoft.org/2010/03/30/markov-chain-generator-in-c/</link>
		<comments>http://phalanx.spartansoft.org/2010/03/30/markov-chain-generator-in-c/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 22:47:10 +0000</pubDate>
		<dc:creator>Visar Shehu</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[markov chain]]></category>
		<category><![CDATA[text generator]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/2010/03/30/markov-chain-generator-in-c/</guid>
		<description><![CDATA[Prandaj, pra, n&#8217;e doni fisin, mali, bregu edhe Malcija prej njaj goje sod t&#8217;brohrisim: Me gjuhë t&#8217;veten e lèn mbas dore. Në gjuhë shqype nanat tona qi prej djepit na kanë thânun, se asht një Zot, qi do ta dona; njatë, qi jetën na ka dhânun; edhe shqyp na thanë se Zoti për shqyptarë Shqypninë [...]]]></description>
			<content:encoded><![CDATA[<div class="quotation">Prandaj, pra, n&#8217;e doni fisin,<br />
mali, bregu edhe Malcija<br />
prej njaj goje sod t&#8217;brohrisim:<br />
Me gjuhë t&#8217;veten e lèn mbas dore.<br />
Në gjuhë shqype nanat tona<br />
qi prej djepit na kanë thânun,<br />
se asht një Zot, qi do ta dona;<br />
njatë, qi jetën na ka dhânun;<br />
edhe shqyp na thanë se Zoti<br />
për shqyptarë Shqypninë e fali,<br />
se sa t&#8217;enden stina e nji tërmetit,<br />
ngjashtu â&#8217; gjuha e jonë shqyptare.<br />
Ah! po; â&#8217; e toskë, malci e qyteta,<br />
gjuhën t&#8217;uej kurr mos ta gzojn kta djalë mbas dore.<br />
Në gjuhë hyjnore;</div>
<p>In the first glance this text might look like gibberish, however to some people it might resemble to something they have seen or read previously.</p>
<p>This one as well:</p>
<div class="quotation">praying and looking out the<br />
thigh-bones, wrapped them round in two sons of Olympus, and<br />
plunged into the silver hilt of his tent and all day Achilles called them in offering insult no man. Therefore I prayed, and now you alone of mortals. If I have<br />
ever decked your own prize, while I mean to go out<br />
with the host to plague the people, but upon<br />
the tenth day long the host. But of this she left its parent stem upon the other side. Then<br />
uprose smooth-tongued Nestor, the facile speaker of the hoar<br />
sea,</div>
<p>What we are seeing in the previous two examples is automatically generated text using <a title="Markov Chain" href="http://en.wikipedia.org/wiki/Markov_chain">Markov Chains</a>. The first one resembles the style of a famous Albanian poet: Gjergj Fishta.  The other one resembles The Illiad by Homer.</p>
<p>I needed to benchmark two databases: MySql and MongoDB. However, since I did not have any data (that would resemble human comments) I had to find a way to automatically generate them. That is why I choose to write an application that would automatically generate text.</p>
<p>One can say that automatically generating text is simple, just taking random words from the dictionary and you are done. However, this approach has one big problem: the text would not look natural at all. This is because we do not take into consideration the statistics of a language. In a language, each word is not followed randomly by another word; e.g., the word &#8220;hello&#8221; in the English language is more likely to be followed by the word &#8220;world” than by the word &#8220;hi&#8221;.</p>
<p>Markov Chain is a process of generating a finite state sequence of events (in our case words), in which you can transfer from each state to another based on some probabilistic model. E.g., suppose your current state is &#8220;hello&#8221;. From here you can jump to another state like: &#8220;world&#8221; or &#8220;home&#8221; because there is a probability that these two words proceed the word &#8220;hello&#8221;.</p>
<p>Building a Markov Model is fairly simple:</p>
<p>First you will need a text file, from which you will build the model.</p>
<p>For each word from that text file, create e k-level list of words proceeding it. k is a fixed number of words that proceed each word. The higher k is, phrases will make more sense. After you are done with all the words you will have a table, with each row representing a starting word, followed by k-other words.</p>
<p>Let’s suppose our text is the Wikipedia Article for Markov Chains (<a href="http://en.wikipedia.org/wiki/Markov_chain">http://en.wikipedia.org/wiki/Markov_chain</a>).</p>
<p>If k=2 then our table would have the following structure:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="213" valign="top">In</td>
<td width="213" valign="top">Mathematics,</td>
<td width="213" valign="top">A</td>
</tr>
<tr>
<td width="213" valign="top">Mathematics,</td>
<td width="213" valign="top">A</td>
<td width="213" valign="top">Markov</td>
</tr>
<tr>
<td width="213" valign="top">A</td>
<td width="213" valign="top">Markov</td>
<td width="213" valign="top">Chain,</td>
</tr>
<tr>
<td width="213" valign="top">Markov</td>
<td width="213" valign="top">Chain,</td>
<td width="213" valign="top">Named</td>
</tr>
<tr>
<td width="213" valign="top">….</td>
<td width="213" valign="top">…</td>
<td width="213" valign="top">…</td>
</tr>
</tbody>
</table>
<p>If k=3 then we would have the following table:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="160" valign="top">In</td>
<td width="160" valign="top">Mathematics,</td>
<td width="160" valign="top">A</td>
<td width="160" valign="top">Markov</td>
</tr>
<tr>
<td width="160" valign="top">Mathematics</td>
<td width="160" valign="top">A</td>
<td width="160" valign="top">Markov</td>
<td width="160" valign="top">Chain,</td>
</tr>
<tr>
<td width="160" valign="top">A</td>
<td width="160" valign="top">Markov</td>
<td width="160" valign="top">Chain,</td>
<td width="160" valign="top">Named</td>
</tr>
<tr>
<td width="160" valign="top">…</td>
<td width="160" valign="top">…</td>
<td width="160" valign="top">…</td>
<td width="160" valign="top">…</td>
</tr>
</tbody>
</table>
<p>One can continue with the same pattern for k=4, 5 &#8230; N.</p>
<p>Since now have our model, we can generate text from it:</p>
<p>1. Pick a random word (from our list of words). Let this word be the first word of your automatically generated text.</p>
<p>2. Randomly select one of the elements from our table that starts with the word that we picked.</p>
<p>3. Append all subsequent words from the list to the generated text.</p>
<p>4. Repeat from step 2, for the last word on your list.</p>
<p>My approach is a word level Markov Chain generator. One can change the same algorithm to generate character level chains, which also generate interesting text patterns.</p>
<p>You can download the entire solution written in C#:</p>
<p><div class="wpfilebase-attachment">
 <div class="wpfilebase-fileicon"><a href="http://phalanx.spartansoft.org/download/MarkovTextGenerator.rar" onclick="wpfilebase_dlclick(1, 'download/MarkovTextGenerator.rar')" title="Download Markov Chain Source"><img align="middle" src="http://phalanx.spartansoft.org/wp-includes/images/crystal/archive.png" alt="Markov Chain Source" /></a></div>
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://phalanx.spartansoft.org/download/MarkovTextGenerator.rar" onclick="wpfilebase_dlclick(1, 'download/MarkovTextGenerator.rar')" title="Download Markov Chain Source">Markov Chain Source</a><br />
   MarkovTextGenerator.rar<br />
   Version: 1.0<br />
   
  </div>
  <div class="wpfilebase-filedetails" id="wpfilebase-filedetails2" style="display: none;">
  <p></p>
  <table border="0">
   
   <tr><th>Author:</th><td>Visar Shehu</td></tr>
   
   
   
   
   <tr><th>Date:</th><td>February 6, 2012</td></tr>
   
  </table>
  </div>
 </div>
 <div class="wpfilebase-fileinfo">
  38.8 KiB<br />
  796 Downloads<br />
  <a href="#" onclick="return wpfilebase_filedetails(2);">Details...</a>
 </div>
 <div style="clear: both;"></div>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/03/30/markov-chain-generator-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Server 2008 &#8211; &#8220;Saving changes is not permitted&#8221;</title>
		<link>http://phalanx.spartansoft.org/2010/03/21/sql-server-2008-saving-changes-is-not-permitted/</link>
		<comments>http://phalanx.spartansoft.org/2010/03/21/sql-server-2008-saving-changes-is-not-permitted/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 22:15:51 +0000</pubDate>
		<dc:creator>Visar Shehu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/?p=1077</guid>
		<description><![CDATA[Since I installed SQL Server 2008, I was getting an annoying error while saving changes to a given table: &#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can&#8217;t be re-created or enabled the option Prevent [...]]]></description>
			<content:encoded><![CDATA[<p>Since I installed SQL Server 2008, I was getting an annoying error while saving changes to a given table: &#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can&#8217;t be re-created or enabled the option Prevent saving changes that require the table to be re-created.&#8221; </p>
<p>The solution is simple (but saved a lot of time for me): </p>
<p>In SQL Server Management Studio select<br />
Tools -> Options -> Designers and uncheck &#8220;Prevent saving changes that require table re-creation&#8221; </p>
<p>Simple as that <img src='http://phalanx.spartansoft.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/03/21/sql-server-2008-saving-changes-is-not-permitted/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ORM DHE LINq &#8211; PJESA E PARE</title>
		<link>http://phalanx.spartansoft.org/2010/03/20/orm-dhe-linq-pjesa-e-pare/</link>
		<comments>http://phalanx.spartansoft.org/2010/03/20/orm-dhe-linq-pjesa-e-pare/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 11:42:35 +0000</pubDate>
		<dc:creator>Visar Shehu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://phalanx.spartansoft.org/2010/03/20/orm-dhe-linq-pjesa-e-pare/</guid>
		<description><![CDATA[Gjate kesaj serie te postimeve, do te mundohem qe nepermjet te posteve te shkurtra, te shpjegoj se cfare paraqesin teknologjite ORM dhe LINQ dhe si mund te perdoren ne zgjidhjen e disa projekteve praktike. LINQ (Language Integrated Query) paraqet nje teknologji te re te Microsoft qe perkrahet nga .NET 3.5 e siper, e cila mundohet [...]]]></description>
			<content:encoded><![CDATA[<p>Gjate kesaj serie te postimeve, do te mundohem qe nepermjet te posteve te shkurtra, te shpjegoj se cfare paraqesin teknologjite ORM dhe LINQ dhe si mund te perdoren ne zgjidhjen e disa projekteve praktike.</p>
<p>LINQ (Language Integrated Query) paraqet nje teknologji te re te Microsoft qe perkrahet nga .NET 3.5 e siper, e cila mundohet te zgjedhe (ose lehtesoje) menyren e manipulimit me te dhena si databaza, XML ose te kolekcione rekordesh ne memorje (lista, vargje &#8230;).</p>
<h4>ORM</h4>
<p>Para se te flas per LINQ, ne dy pjeset e para te kesaj serie do paraqes nje hyrje te shkurter per Object Relational Mapping (ORM). ORM paraqet nje qasje (ose metodologji) programimi ku zhvilluesi mundohet qe te paraqese ne forme abstrakte objektet nga nje databaze (tabelat, lidhjet etj.) ne nje forme me te afert me konceptet e njohura nga modeli i orientuar ne objekte (OO Model).</p>
<p>Ekzistojne disa implementime komerciale dhe open source per ORM automatik. Ne kete post manualisht do te krijojme ORM, me specifikisht ate qe njihet Reverse Mapping (krijimi i klasave nga tabelat). Ky post nuk ka per qellim te pershkruje te gjithe specifikat e ORM, por vetem te parashtroje problemet e mundshme me te cilat do te ballafaqohej nje perdorues gjate krijimit te ORM.</p>
<p>Ne esence nje tabele ne databazen tone paraqet kolekcion reshtash dhe kolonash. Cdo resht mund te trajtohet si objekt, ndersa cdo kolone mund te trajtohet si atribut / karakteristike e atij objekti. Menjehere verehet nje ngjashmeri ndermjet tabelave dhe klasave. Cdo klase pershkruan nje objekt, objektet paraqesin instance te nje klase. Cdo objekt definohet nga karakteristikat e tij (qe implementohen me variabla) dhe sjelljeve (behaviors) te tij (qe implementohen me metoda).</p>
<p>Te marim nje shembull:</p>
<p><a href="http://phalanx.spartansoft.org/wp-content/uploads/2010/03/Table.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto;" title="Tabela e perdoruesve" src="http://phalanx.spartansoft.org/wp-content/uploads/2010/03/Table.png" alt="Tabela e perdoruesve" width="210" height="162" /></a></p>
<p>Me SQL kjo tabele do krijohej ne kete menyre:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Perdoruesit
<span style="color: #66cc66;">&#40;</span>
ID uniqueidentifier<span style="color: #66cc66;">,</span>
Emri nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
Mbiemri nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
EmriPerdoruesit nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
Fjalekalimi nvarchar <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>ID<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Nga skema relacionale e kesaj tabele, shihet qarte se ne mund te definojme nje klase:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Perdoruesi
<span style="color: #000000;">&#123;</span>
<span style="color: #0600FF;">public</span> Guid ID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Emri <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Mbiemri <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> EmriPerdoruesit <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Fjalekalimi <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>E qarte qe tani ne mund te krijojme instanca te kesaj klase:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Perdoruesi p <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Perdoruesi<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewID</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Emri</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Visar&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Mbiemri</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Shehu&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">EmriPerdoruesit</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;vshehu&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Fjalekalimi</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;sekret&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Nga kjo mund edhe te krijojme kolekcione te tipit Perdorues:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>Perdoruesit<span style="color: #008000;">&gt;</span> lista <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Perdoruesit<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
lista.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>p<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Pastaj dhe te manipulojme me to, psh ti listojme vetem ato perdorues Emri i te cileve fillon me shkronjen &#8220;A&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Perdoruesi tmpP <span style="color: #0600FF;">in</span> lista<span style="color: #000000;">&#41;</span>     
<span style="color: #000000;">&#123;</span>          
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>tmpP.<span style="color: #0000FF;">Emri</span>.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;A&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>          
<span style="color: #000000;">&#123;</span>               
Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>tmpP.<span style="color: #0000FF;">Emri</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>          
<span style="color: #000000;">&#125;</span>      
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Sidoqofte, deri tani ne kemi vetem nje kopje te skemes relacionale ne memorje. Cdo objekt qe do ta krijojme do te shkaterohet automatikisht dhe me te edhe te dhenat per ate objekt ose kolekcion. Qe te kemi nje aplikacion funksional ne duhet te implementojme CRUD operacionet (Create, Read, Update, Delete). Te gjithe keto do ti implementoj si metoda ne klasen Perdorues (qasje e cila eshte diskutabile, mirepo problemet me kete qasje do ti adresoj ne postin e ardhshem).</p>
<h2>Create</h2>
<p>Ky eshte edhe operacioni i pare qe do te deshim ta implementojme per objektin tone. Ideja eshte e thjeshte: te kemi nje mundesi qe objektin e krijuar ne memorje, ta regjistrojme si rekord ne databaze. Ne esence duam qe objekti jone te kete nje sjellje (behavior) qe do te ruaje gjendjen e ketij objekti. Sjelljet ne OO terminologjine implementohen me ane te metodave. Per rastin tone do te krijojme nje metode Save() qe do te na mundesoje kete. Klasa Perdoruesi tani do te kete kete forme:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Perdoruesi
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> Guid ID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Emri <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Mbiemri <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> EmriPerdoruesit <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">String</span> Fjalekalimi <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">String</span> sql <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;INSERT INTO Perdoruesit(ID, Emri, Mbiemri, EmriPerdoruesit, Fjalekalimi) VALUES (@ID, @Emri, @Mbiemri, @EmriPerdoruesit, @Fjalekalimi)&quot;</span><span style="color: #008000;">;</span>
            SqlConnection con <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            SqlCommand cmd <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            cmd.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> sql<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//Shtojme parametrat e nevojshem</span>
            cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@ID&quot;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ID</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Emri&quot;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Emri</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Mbiemri&quot;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Mbiemri</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@EmriPerdoruesit&quot;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">EmriPerdoruesit</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Fjalekalimi&quot;</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Fjalekalimi</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//Hapet konekcioni dhe ekzekutojme komanden</span>
            con.<span style="color: #0000FF;">ConnectionString</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Definojme connection string ketu&quot;</span><span style="color: #008000;">;</span>
            con.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            cmd.<span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            con.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Shfrytezuesit e kesaj librarie do te shohin vetem kete Interface te klases tone:</p>
<p><a href="http://phalanx.spartansoft.org/wp-content/uploads/2010/03/image.png"><img style="display: block; float: none; margin-left: auto; margin-right: auto; border: 0px;" title="image" src="http://phalanx.spartansoft.org/wp-content/uploads/2010/03/image_thumb.png" border="0" alt="image" width="189" height="244" /></a></p>
<p>Dhe kur nje shfrytezues do ta shfrytezoje kete klase, ai do te jete i sigurt se sapo ta perdore metoden Save() ne bazen e te dhenave do te regjistrohet nje perdorues i ri:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Perdoruesi p <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Perdoruesi<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewID</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Emri</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Visar&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Mbiemri</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Shehu&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">EmriPerdoruesit</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;vshehu&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Fjalekalimi</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;sekret&quot;</span><span style="color: #008000;">;</span>
p.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>Ngjashem mund te implementohet edhe metoda Delete(). Ne postin e ardhshem do te flas se si mund te implementohen metodat tjera per leximin e nje ose me teper rekordeve, qe me vone te kalojme ne perdorimin e ndonje ORM te gatshem.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/03/20/orm-dhe-linq-pjesa-e-pare/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Troubleshooting the database could not be exclusively locked to perform the operation error on SQL Server</title>
		<link>http://phalanx.spartansoft.org/2010/02/20/troubleshooting-the-database-could-not-be-exclusively-locked-to-perform-the-operation-error-on-sql-server/</link>
		<comments>http://phalanx.spartansoft.org/2010/02/20/troubleshooting-the-database-could-not-be-exclusively-locked-to-perform-the-operation-error-on-sql-server/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 19:25:20 +0000</pubDate>
		<dc:creator>Mehmetali Shaqiri</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[troubleshooting]]></category>

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

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

<p>or from SQL Management Studio by choosing the database properties:</p>
<p><a href="http://phalanx.spartansoft.org/wp-content/uploads/2010/02/database-properties.jpg"><img class="aligncenter size-full wp-image-919" title="Database Properties" src="http://phalanx.spartansoft.org/wp-content/uploads/2010/02/database-properties.jpg" alt="Database Properties" width="704" height="631" /></a></p>
<p>Sometimes the simplest issues can cause you a real headache.</p>
]]></content:encoded>
			<wfw:commentRss>http://phalanx.spartansoft.org/2010/02/20/troubleshooting-the-database-could-not-be-exclusively-locked-to-perform-the-operation-error-on-sql-server/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

