<?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>Akash Kava &#187; Assembly</title>
	<atom:link href="http://akashkava.com/blog/tag/assembly/feed/" rel="self" type="application/rss+xml" />
	<link>http://akashkava.com/blog</link>
	<description>Founder of NeuroSpeech</description>
	<lastBuildDate>Tue, 10 Apr 2012 18:00:43 +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>Using SQL Assembly to replace T-SQL Business Logic</title>
		<link>http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/</link>
		<comments>http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 09:00:32 +0000</pubDate>
		<dc:creator>Akash Kava</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://akashkava.com/blog/?p=259</guid>
		<description><![CDATA[SQL Assembly brings power of CLR (Common language runtime) inside SQL Database Stored Procedure which can overcome T-SQL programming pains of Intellisense, Debugging, Logging and Versioning. Traditional Problems with T-SQL and Stored Procedures I never liked programming in T-SQL, for various reasons listed below, No Intellisense, of course it has been introduced SQL 2008 onwards, [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		digg_url = "http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/";
		digg_bgcolor = "";
		digg_skin = "";
		digg_window = "";
		digg_title = "Using+SQL+Assembly+to+replace+T-SQL+Business+Logic";
		digg_media = "";
		digg_topic = "";
		digg_bodytext = "";
		//-->
		</script>
		<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fakashkava.com%2Fblog%2F259%2Fusing-sql-assembly-to-replace-t-sql-business-logic%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fakashkava.com%2Fblog%2F259%2Fusing-sql-assembly-to-replace-t-sql-business-logic%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>SQL Assembly brings power of CLR (Common language runtime) inside SQL Database Stored Procedure which can overcome T-SQL programming pains of Intellisense, Debugging, Logging and Versioning.</p>
<h2>Traditional Problems with T-SQL and Stored Procedures</h2>
<p>I never liked programming in T-SQL, for various reasons listed below,</p>
<ol>
<li><font color="#333333">No Intellisense, of course it has been introduced SQL 2008 onwards, but still its far behind what we get in Visual Studio.</font> </li>
<li><font color="#333333">Source Code Control, although scripts can be added to source code control, usually its unawareness and unwillingness of Database Administrator / Designer to use it correctly and mess things up.</font> </li>
<li><font color="#333333">Less human readable, T-SQL is very difficult to understand if its not documented well.</font> </li>
<li><font color="#333333">Source code is open, it can be modified, so in short Database Administrator has complete access to alter business logic.</font> </li>
<li><font color="#333333">Very less reusable, unless you do copy paste, no way to standardize the development.</font> </li>
<li><font color="#333333">No support for advanced visual tools to design T-SQL Stored Procedures.</font> </li>
<li><font color="#333333">Deployment is difficult, minor alternation can cause problems.</font> </li>
<li><font color="#333333">Most of business logic involves, loops, if-else, switch statements, and it takes 10 times more code to execute such logic in T-SQL then any other programming language in world.</font> </li>
<li><font color="#333333">You need multiple language talent, one with either VB.NET/C# and T-SQL.</font> </li>
<li>Difficult to Log information.</li>
</ol>
<h2>Introduction of SQL Assembly</h2>
<p>Since SQL 2005, introduction of SQL Assembly has been of great importance, however not much of people still use it, because of unawareness. But SQL Assembly can be modularized and reused, and it exists as simple CLR assembly inside database. </p>
<p>And following are benefits of SQL Assembly, besides few listed in <a href="http://msdn.microsoft.com/en-us/library/k2e1fb36(v=VS.100).aspx" target="_blank">MSDN here</a>.</p>
<ol>
<li><font color="#333333">LINQ in CLR Stored Procedure.</font> </li>
<li><font color="#333333">Better Source Code control over Visual C#/Visual Basic.NET project through Team System.</font> </li>
<li><font color="#333333">Intellisense.</font> </li>
<li><font color="#333333">Closed source, its safe.</font> </li>
<li><font color="#333333">Highly secure as Database Administrator can not alter business logic.</font> </li>
<li><font color="#333333">C# and VB.NET along with LINQ are more human readable, understanding and reusing code can be very easy.</font> </li>
<li><font color="#333333">Support for advanced documentation in comments.</font> </li>
<li><font color="#333333">Visual Studio Model designer can be used.</font> </li>
<li>Reuse your Business Logic for other database vendor products, e.g. Oracle, MySQL (Explained below). </li>
<li>You can do advanced logging with log-4-net or custom logging.</li>
</ol>
<h2>Small Example of How to use LINQ in SQL Assembly</h2>
<ol>
<li>Create a simple Visual C# Class Library Project. </li>
<li>Add reference of “System.Data” Assembly. </li>
<li>Create new ADO.NET Entity Model, and you can create it from Database or empty model, the way you want to develop your project. </li>
<li>Add few entities, I am going to demonstrate entity “Cart” which is a saved shopping cart for the Customer. </li>
</ol>
<h2><a href="http://akashkava.com/blog/wp-content/uploads/2010/04/CartDemo.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="CartDemo" border="0" alt="CartDemo" src="http://akashkava.com/blog/wp-content/uploads/2010/04/CartDemo_thumb.png" width="389" height="206" /></a>Create a Stored Procedure Class</h2>
<p>After creating model, lets create a class called “ShoppingCart” and add a static method as shown below.</p>
<blockquote><pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">ShoppingCart
</span>{

    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Creates new Cart if none exists and returns the Cart ID
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;param name=&quot;CustomerID&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span>[<span style="color: #2b91af">SqlProcedure</span>]
    <span style="color: blue">public static </span><span style="color: #2b91af">SqlInt64 </span>GetSavedCartID(<span style="color: #2b91af">SqlInt64 </span>CustomerID)
    {

        <span style="color: green">// Set context connection=true
        </span><span style="color: #2b91af">ShoppingCartModelContainer </span>context =
            <span style="color: blue">new </span><span style="color: #2b91af">ShoppingCartModelContainer</span>(<span style="color: #a31515">&quot;context connection=true&quot;</span>);

        <span style="color: green">// Query for exisitng cart
        </span><span style="color: #2b91af">Cart </span>c = context.Carts.FirstOrDefault(
                        t =&gt; t.CustomerID == CustomerID.Value);

        <span style="color: blue">if </span>(c == <span style="color: blue">null</span>) { 

            <span style="color: green">// create new Cart
            </span>c = <span style="color: #2b91af">Cart</span>.CreateCart(0, CustomerID.Value, <span style="color: #2b91af">DateTime</span>.Now);
            context.AddToCarts(c);
            context.SaveChanges();
        }

        <span style="color: green">// return the Cart ID
        </span><span style="color: blue">return new </span><span style="color: #2b91af">SqlInt64</span>(c.CartdID);

    }

}</pre>
</blockquote>
<p align="justify">This code demonstrates how easy it is to write SQL Assembly, now here is the ultimate benefit of everything.</p>
<h2>Ultimate Benefit</h2>
<p align="justify">You can use your Business Logic in multiple vendor databases, like for example for Oracle and MySQL, you can reuse your SQL Assembly and just change your connection string. You can reuse your business logic in your front end code with simple wrapper.</p>
<p align="justify">And with SQL Server, you can reuse this assembly inside SQL Database.</p>
<p align="justify"><a href="http://akashkava.com/blog/wp-content/uploads/2010/04/CodeSharing.png"><img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="CodeSharing" border="0" alt="CodeSharing" src="http://akashkava.com/blog/wp-content/uploads/2010/04/CodeSharing_thumb.png" width="644" height="200" /></a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fakashkava.com%2Fblog%2F259%2Fusing-sql-assembly-to-replace-t-sql-business-logic%2F&amp;title=Using%20SQL%20Assembly%20to%20replace%20T-SQL%20Business%20Logic"><img src="http://akashkava.com/blog/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://akashkava.com/blog/259/using-sql-assembly-to-replace-t-sql-business-logic/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

