<?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"
	>

<channel>
	<title>relax! it's just 0s and 1s</title>
	<atom:link href="http://rajanadar.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rajanadar.com</link>
	<description>life's good when you relax a BIT</description>
	<pubDate>Sat, 12 Jun 2010 06:52:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>cancel all event handlers on postback</title>
		<link>http://rajanadar.com/2010/06/cancel-all-event-handlers/</link>
		<comments>http://rajanadar.com/2010/06/cancel-all-event-handlers/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 06:10:25 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[asp.net]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[asp.net events]]></category>

		<category><![CDATA[page cycle]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=22</guid>
		<description><![CDATA[recently i had an interesting problem in my project.. which is an asp.net 3.5 web application. it consists of a monster page, which nests close to million user controls within it. these user controls post back at will, and sometimes whimsically too&#8230; the page itself had a RAD Navigation control, the wizard king.

now the one [...]]]></description>
			<content:encoded><![CDATA[<p>recently i had an interesting problem in my project.. which is an asp.net 3.5 web application. it consists of a monster page, which nests close to million user controls within it. these user controls post back at will, and sometimes whimsically too&#8230; the page itself had a RAD Navigation control, the wizard king.<br />
<br />
now the one common object these user controls and the page itself, was working was my Entity object from Entity Framework. Now the page is basically a wizard, which keeps filling this object, and there are options to save this object explicitly, or create templated items out of it.<br />
<br />
This same object (Business Entity) could also be worked by multiple users across different browser sessions/work stations. sort of a disconnected data set. Any persistent action done on this object updates the &#8216;Last Modified&#8217; date time of this object. (locally and in the database)<br />
<br />
My requirement was that, during every page load, i needed to check if this local object was the latest copy, and if stale, redirect the user to a <strong>&#8220;no donuts for you&#8221;</strong> page&#8230;</p>
<p>Now, in the normal scenario, i would write a simple method in the page load of my monster page, which checks if the object is latest, and if not, just do a<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Response.<span style="color: #0000FF;">Redirect</span><span style="color: #000000;">&#40;</span>donutUrl, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>
But Murphy being who he is, never gives normal scenarios to developers. I had to pop up a Modal window to the user to inform that, &#8220;<strong>you are working on a stale entity and need to refresh your view</strong>.&#8221; And on the click of a confirmation, redirect the user.<br />
<br />
So i used an AJAX Modal popup, and popped up the message. On the OK click, the user was redirected as i had wished for.<br />
<br />
Now as much as it looks hunky-dory, a weird thing was happening&#8230; if the user clicked on the <strong>&#8220;Save My Object So Far&#8221;</strong> button, the page load did its task, of popping up the popup properly (tongue twisty?) but the event handler for the &#8220;<strong>Save</strong>&#8221; button continued to happen behind, and it saved the entity&#8230; that is not what i wish for on a Monday morning, and then the next morning, and then the next&#8230;<br />
<br />
now i had to prevent any further operation after the popup&#8230; so i began to put the stuff between by ears into effect&#8230; as lame as they were, i thought of the following ideas: (if they are that)<br />
</p>
<ul>
<li>have a boolean flag, and chain it across the event handlers, so that if the entity was modified, do not proceed with the event handling code. (lame&#8230;. if there are too many event handlers.. and there were)</li>
<li>end the page response, after the popup.. so that no further code gets executed after the page load method. but next to grabbing a candy from my 2 year old cousin quietly, i have not been able to do this.. either a blank page is rendered, or everything happens as unexpected&#8230; there are no half-measures with the asp.net page life cycle.. (i cannot use document.Write())</li>
<li>how about if i manage to suppress all the event handlers on the page, once i detect that the entity is stale.</li>
</ul>
<p>
the 3rd idea seemed to make sense, and i proceeded with a dummy page.. which rendered a label on page load. and then, a button click modified this label.  then in the page load, i tried to do a<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">LabelMessage</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Page Load&quot;</span>;
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ButtonAction</span>.<span style="color: #0000FF;">Click</span> <span style="color: #008000;">-=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ButtonAction_Click</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> ButtonAction_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">LabelMessage</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Clicked Me, overwrote Page Load&quot;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
and <strong>holy guacamole</strong>&#8230; it worked.. the event handler was not executed&#8230;<br />
<br />
now another problem i noticed, is that i don&#8217;t know which control posted back.. also, which event of the control caused the postback.. so i had to solve 2 problems..<br />
</p>
<ul>
<li>identify the control that posted back</li>
<li>remove all event handlers for that control..</li>
</ul>
<p>
and the doors of a solution seem to open up&#8230; finding the postback control was a standard snippet i use, and reflection rocks, when i have to dig out the protected &#8220;Events&#8221; property of any Control. i also realized that, the event handler delegates are stored in a linked list format for every event&#8230; which also need to be retrieved using reflection..<br />
<br />
so i wrote a utility method, which, if given a page, finds the control that posted back, and removes all the event handlers of the control.<br />
<br />
this way, you have a central generic method, which can suppress any further event handling in your code.<br />
<br />
<strong>now that i have typed close to two pages of a newspaper, time to post the snippet, which you&#8217;ll scroll through, in the first place, in any case.. so here goes nothing&#8230;</strong><br />
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #008080; font-style: italic;">// Cancels all the event handling code for the control that posted back.</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> CancelPostbackEvent<span style="color: #000000;">&#40;</span>Page page<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>page.<span style="color: #0000FF;">IsPostBack</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var postBackControl <span style="color: #008000;">=</span> Utility.<span style="color: #0000FF;">GetPostBackControl</span><span style="color: #000000;">&#40;</span>page<span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>postBackControl <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var controlType <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>Control<span style="color: #000000;">&#41;</span>;
&nbsp;
            var postBackControlEventHandlerList <span style="color: #008000;">=</span> controlType.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Events&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">GetProperty</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">Instance</span>,
            <span style="color: #0600FF;">null</span>, postBackControl, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> EventHandlerList;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>postBackControlEventHandlerList <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                var eventHandlerListType <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>EventHandlerList<span style="color: #000000;">&#41;</span>;
                <span style="color: #FF0000;">object</span> headEventHandlerListEntry <span style="color: #008000;">=</span> eventHandlerListType.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;head&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">GetField</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">Instance</span>,
                <span style="color: #0600FF;">null</span>, postBackControlEventHandlerList, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>headEventHandlerListEntry <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    var delegatesDictionary <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span>, <span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                    Utility.<span style="color: #0000FF;">GetEventHandlersRecursively</span><span style="color: #000000;">&#40;</span>delegatesDictionary, headEventHandlerListEntry<span style="color: #000000;">&#41;</span>;
&nbsp;
                    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var delegateContainer <span style="color: #0600FF;">in</span> delegatesDictionary<span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var index <span style="color: #008000;">=</span> delegateContainer.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>; index <span style="color: #008000;">&amp;</span>gt;<span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>; <span style="color: #008000;">--</span>index<span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            postBackControlEventHandlerList.<span style="color: #0000FF;">RemoveHandler</span><span style="color: #000000;">&#40;</span>delegateContainer.<span style="color: #0000FF;">Key</span>, delegateContainer.<span style="color: #0000FF;">Value</span><span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
                        <span style="color: #000000;">&#125;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Control GetPostBackControl<span style="color: #000000;">&#40;</span>Page page<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Control postBackControl <span style="color: #008000;">=</span> <span style="color: #0600FF;">null</span>;
&nbsp;
    var postBackControlName <span style="color: #008000;">=</span> page.<span style="color: #0000FF;">Request</span>.<span style="color: #0600FF;">Params</span>.<span style="color: #0000FF;">Get</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;__EVENTTARGET&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>postBackControlName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        postBackControl <span style="color: #008000;">=</span> page.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span>postBackControlName<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">else</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> controlName <span style="color: #0600FF;">in</span> page.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Form</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var control <span style="color: #008000;">=</span> page.<span style="color: #0000FF;">FindControl</span><span style="color: #000000;">&#40;</span>controlName<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>control <span style="color: #008000;">is</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">UI</span>.<span style="color: #0000FF;">WebControls</span>.<span style="color: #0000FF;">Button</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                postBackControl <span style="color: #008000;">=</span> control;
                <span style="color: #0600FF;">break</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> postBackControl;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> GetEventHandlersRecursively<span style="color: #000000;">&#40;</span>Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span>, <span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span> delegatesDictionary, <span style="color: #FF0000;">object</span> currentEventHandlerListEntry<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>currentEventHandlerListEntry <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var eventHandlerListEntryType <span style="color: #008000;">=</span> currentEventHandlerListEntry.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        var eventHandler <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#41;</span>eventHandlerListEntryType.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;handler&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">GetField</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">Instance</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">NonPublic</span>,
        <span style="color: #0600FF;">null</span>, currentEventHandlerListEntry, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #FF0000;">object</span> key <span style="color: #008000;">=</span> eventHandlerListEntryType.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;key&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">GetField</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">Instance</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">NonPublic</span>,
        <span style="color: #0600FF;">null</span>, currentEventHandlerListEntry, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        var nextEventHandlerListEntry <span style="color: #008000;">=</span> eventHandlerListEntryType.<span style="color: #0000FF;">InvokeMember</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;next&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">GetField</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">Instance</span> <span style="color: #008000;">|</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Reflection</span>.<span style="color: #0000FF;">BindingFlags</span>.<span style="color: #0000FF;">NonPublic</span>,
        <span style="color: #0600FF;">null</span>, currentEventHandlerListEntry, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>eventHandler <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var eventDelegates <span style="color: #008000;">=</span> eventHandler.<span style="color: #0000FF;">GetInvocationList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>eventDelegates <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;</span>amp;<span style="color: #008000;">&amp;</span>amp; eventDelegates.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&amp;</span>gt; <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                delegatesDictionary.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>key, eventDelegates<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>nextEventHandlerListEntry <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Utility.<span style="color: #0000FF;">GetEventHandlersRecursively</span><span style="color: #000000;">&#40;</span>delegatesDictionary, nextEventHandlerListEntry<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
do let me know, if you have a better solution, or if the above code upsets your chihuahua&#8230;</p>
<div><strong>my lack of conscience really pricks me, when i write new code..</strong></div>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2010/06/cancel-all-event-handlers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C#.NET and MySql</title>
		<link>http://rajanadar.com/2009/02/cnet-and-mysql/</link>
		<comments>http://rajanadar.com/2009/02/cnet-and-mysql/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 02:46:56 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[.net]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[mysql]]></category>

		<category><![CDATA[raja mysql c#]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=20</guid>
		<description><![CDATA[with the use of LINQ and Entity Framework, I haven&#8217;t been writing much ADO.NET code to for the Data Access layer. Until recently, when one of my projects in the solution is still .NET 2.0 based, with the standard ADO.NET Data Access. its been some time, since I saw the SqlConnection and SqlCommand classses.
 
offline, I [...]]]></description>
			<content:encoded><![CDATA[<p>with the use of LINQ and Entity Framework, I haven&#8217;t been writing much ADO.NET code to for the Data Access layer. Until recently, when one of my projects in the solution is still .NET 2.0 based, with the standard ADO.NET Data Access. its been some time, since I saw the SqlConnection and SqlCommand classses.</p>
<p> </p>
<p>offline, I was working with a pastime application of mine, demonstrating Data Source independence. It is a provider architecture, where the specific data source is easily pluggable.</p>
<p> </p>
<p>The base interface (IDataProvider) is used by the application. I already had the SqlDataProvider defined for SQL Databases. I tried switiching the provider to a MySql data source. At the end of it, just wanted to publish a couple of snippets to do data access tasks using C# and MySql.</p>
<p> </p>
<ul>
<li>You can use the ADO.NET Driver provided by MySql. Download the latest MySql Connector for .NET from: <a href="http://dev.mysql.com/downloads/connector/net">http://dev.mysql.com/downloads/connector/net</a>. This is a free developer version of the component.</li>
<li>You can download the appropriate connector based on the .NET version.</li>
<li>You can also use the Odbc Driver, but my snippet is for the MySql connector.</li>
<li>Add a reference to the MySqlData.DLL to your project. Do not forget to ship this DLL.</li>
</ul>
<p> </p>
<p>Add the following namespace:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Data</span>;
<span style="color: #0600FF;">using</span> MySql.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">MySqlClient</span>;</pre></div></div>

<p> </p>
<p><strong>C# and MySql Data Access without a Transaction.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>MySqlConnection connection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MySqlConnection<span style="color: #000000;">&#40;</span>mySqlConnectionString<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>MySqlCommand command <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MySqlCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        command.<span style="color: #0000FF;">Connection</span> <span style="color: #008000;">=</span> connection;
        command.<span style="color: #0000FF;">CommandType</span> <span style="color: #008000;">=</span> CommandType.<span style="color: #0000FF;">StoredProcedure</span>;
        command.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;SELNewRecord&quot;</span>;
&nbsp;
        command.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Param1&quot;</span>, value1<span style="color: #000000;">&#41;</span>;
&nbsp;
        connection.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>MySqlDataReader reader <span style="color: #008000;">=</span> command.<span style="color: #0000FF;">ExecuteReader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// read the contents.</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p> </p>
<p><strong>C# and MySql Data Access with a Transaction.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>MySqlConnection connection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MySqlConnection<span style="color: #000000;">&#40;</span>mySqlConnectionString<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>MySqlCommand command <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MySqlCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        command.<span style="color: #0000FF;">Connection</span> <span style="color: #008000;">=</span> connection;
        command.<span style="color: #0000FF;">CommandType</span> <span style="color: #008000;">=</span> CommandType.<span style="color: #0000FF;">StoredProcedure</span>;
        command.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;INSNewRecord&quot;</span>;
&nbsp;
        connection.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>command.<span style="color: #0000FF;">Transaction</span> <span style="color: #008000;">=</span> connection.<span style="color: #0000FF;">BeginTransaction</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            command.<span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #008080; font-style: italic;">// Execute additional SQL Queries.</span>
&nbsp;
            command.<span style="color: #0000FF;">Transaction</span>.<span style="color: #0000FF;">Commit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong> </strong></p>
<p>after all the latest ORM technologies and ease of code writing, it feels nice to write a good old data access snippet. <strong>though only once every 12 months&#8230; </strong></p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2009/02/cnet-and-mysql/feed/</wfw:commentRss>
		</item>
		<item>
		<title>rss and atom api in .NET 3.5</title>
		<link>http://rajanadar.com/2008/12/rss-and-atom-api/</link>
		<comments>http://rajanadar.com/2008/12/rss-and-atom-api/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 06:48:20 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[.net 3.5]]></category>

		<category><![CDATA[syndication]]></category>

		<category><![CDATA[atom]]></category>

		<category><![CDATA[rss]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=19</guid>
		<description><![CDATA[i love the features which creep into an API. and the .NET framework classes grow richer and richer. my latest random activity has been to develop User controls (blog rolls, rss widgets) in .NET 3.5.
to think of it, one can easily write an RSS reader control, with inline commenting feature for blog posts, searching by [...]]]></description>
			<content:encoded><![CDATA[<p>i love the features which creep into an API. and the .NET framework classes grow richer and richer. my latest random activity has been to develop User controls (blog rolls, rss widgets) in .NET 3.5.</p>
<p>to think of it, one can easily write an RSS reader control, with inline commenting feature for blog posts, searching by category, authors etc. all that, by just using the Syndication API in .NET 3.5.</p>
<p> </p>
<p>here are the classes that i am talking about:</p>
<ul>
<li><strong>SyndicationFeed:</strong> this is the top-level class representing the RSS or ATOM feed</li>
<li><strong>SyndicationItem:</strong> this represents a blog post</li>
<li><strong>SyndicationCategory:</strong> a category defined in a blog, applied to posts</li>
<li><strong>SyndicationLink:</strong> a link associated with a blog post. (self, edit, comments etc)</li>
<li><strong>SyndicationPerson:</strong> author of the blog post (name, email, profile Url etc)</li>
</ul>
<p> </p>
<p>without further ado, here is a quick snippet to get you started on the RSS/Atom APIs..</p>
<p>firstly, a reference needs to be added:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">System</span>.<span style="color: #0000FF;">ServiceModel</span>.<span style="color: #0000FF;">Web</span></pre></div></div>

<p>the namespaces required are:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">ServiceModel</span>.<span style="color: #0000FF;">Syndication</span>;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Xml</span>;</pre></div></div>

<p><strong>Code Snippet:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #FF0000;">string</span> atomOrRssUrl <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;http://rajanadar.blogspot.com/atom.xml&quot;</span>;
&nbsp;
SyndicationFeed rssOrAtomFeed <span style="color: #008000;">=</span> <span style="color: #0600FF;">null</span>;
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>XmlReader feedXmlReader <span style="color: #008000;">=</span> XmlReader.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>atomOrRssUrl<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Top-level feed in (Atom 1.0) or Rss 2.0</span>
    rssOrAtomFeed <span style="color: #008000;">=</span> SyndicationFeed.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span>feedXmlReader<span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">// Read the blog posts in this blog.</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>SyndicationItem blogItem <span style="color: #0600FF;">in</span> rssOrAtomFeed.<span style="color: #0000FF;">Items</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Important Blog Post attributes.</span>
        blogItem.<span style="color: #0000FF;">Id</span>;
        blogItem.<span style="color: #0000FF;">Title</span>.<span style="color: #0000FF;">Text</span>;
        blogItem.<span style="color: #0000FF;">PublishDate</span>;
        blogItem.<span style="color: #0000FF;">LastUpdatedTime</span>;
        blogItem.<span style="color: #0000FF;">Copyright</span>;
&nbsp;
        <span style="color: #008080; font-style: italic;">// Contents</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>blogItem.<span style="color: #0000FF;">Content</span> <span style="color: #008000;">is</span> TextSyndicationContent<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// typically, this will be HTML.</span>
            <span style="color: #000000;">&#40;</span>blogItem.<span style="color: #0000FF;">Content</span> <span style="color: #0600FF;">as</span> TextSyndicationContent<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Text</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Categories of this blog post.</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>SyndicationCategory category <span style="color: #0600FF;">in</span> blogItem.<span style="color: #0000FF;">Categories</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            category.<span style="color: #0000FF;">Name</span>;
            category.<span style="color: #0000FF;">Scheme</span>;
            category.<span style="color: #0000FF;">Label</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Get the Links associated with this blog post.</span>
        <span style="color: #008080; font-style: italic;">// e.g. Comments (replies), self, alternate, edit links etc</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>SyndicationLink link <span style="color: #0600FF;">in</span> blogItem.<span style="color: #0000FF;">Links</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            link.<span style="color: #0000FF;">Title</span>;
            link.<span style="color: #0000FF;">Uri</span>;
            link.<span style="color: #0000FF;">RelationshipType</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Get the Authors associated with the blog post.</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>SyndicationPerson author <span style="color: #0600FF;">in</span> blogItem.<span style="color: #0000FF;">Authors</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            author.<span style="color: #0000FF;">Name</span>;  <span style="color: #008080; font-style: italic;">// Raja Nadar</span>
            author.<span style="color: #0000FF;">Email</span>; <span style="color: #008080; font-style: italic;">// noreply@blogger.com</span>
            author.<span style="color: #0000FF;">Uri</span>;   <span style="color: #008080; font-style: italic;">// Profile Url for blogger</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>obviously, the code won&#8217;t compile :), not because i wrote it, but that i have just highlighted the relevant fields I used. not assigned or used them anywhere..</p>
<p> </p>
<p>so explore the Syndication APIs and code happily ever after.</p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/12/rss-and-atom-api/feed/</wfw:commentRss>
		</item>
		<item>
		<title>WCF, certificates, event logs and silly security exceptions</title>
		<link>http://rajanadar.com/2008/07/wcf-certificates-event-logs-and-silly-security-exceptions/</link>
		<comments>http://rajanadar.com/2008/07/wcf-certificates-event-logs-and-silly-security-exceptions/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 23:57:24 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[security]]></category>

		<category><![CDATA[wcf]]></category>

		<category><![CDATA[certificates]]></category>

		<category><![CDATA[event logs]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=18</guid>
		<description><![CDATA[my friend was working on certificate based WCF transport messages. she prototyped a demo, and was testing it out. she kept on hitting the following exception:
Found multiple X.509 certificates using the following search criteria: StoreName &#8216;My&#8217;, StoreLocation &#8216;LocalMachine&#8217;, FindType &#8216;FindBySubjectName&#8217;, FindValue &#8221;. Provide a more specific find value.
the error message could not have been more concise.. [...]]]></description>
			<content:encoded><![CDATA[<p>my friend was working on certificate based WCF transport messages. she prototyped a demo, and was testing it out. she kept on hitting the following exception:</p>
<p><em><strong>Found multiple X.509 certificates using the following search criteria: StoreName &#8216;My&#8217;, StoreLocation &#8216;LocalMachine&#8217;, FindType &#8216;FindBySubjectName&#8217;, FindValue &#8221;. Provide a more specific find value.</strong></em></p>
<p>the error message could not have been more concise.. I had a look at the code, and there was nothing programmatic to verify. it was all WCF configuration driven [that I like so much J].</p>
<p>the configuration for binding was as follows:</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;">&lt;</span><span style="font-size: 10pt; color: #a31515;">bindings</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">wsHttpBinding</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">binding</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">name</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">wsHttpEndpointBinding</span>&#8220;<span style="color: #0000ff;">&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">      </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">security</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">mode</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">Message</span>&#8220;<span style="color: #0000ff;">&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">        </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">message</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">clientCredentialType</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">Certificate</span>&#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">      </span>&lt;/</span><span style="font-size: 10pt; color: #a31515;">security</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span>&lt;/</span><span style="font-size: 10pt; color: #a31515;">binding</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;/</span><span style="font-size: 10pt; color: #a31515;">wsHttpBinding</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: #0000ff; line-height: 115%;">&lt;/</span><span style="font-size: 10pt; color: #a31515; line-height: 115%;">bindings</span><span style="font-size: 10pt; color: #0000ff; line-height: 115%;">&gt;</span></p>
<p>the configuration for service credentials was as follows:</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;">&lt;</span><span style="font-size: 10pt; color: #a31515;">serviceCredentials</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">clientCertificate</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">certificate</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">storeLocation</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">LocalMachine</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">storeName</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">My</span>&#8220;<span style="color: #0000ff;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: "><span style="color: #ff0000;">                 x509FindType</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">FindBySubjectName</span>&#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">authentication</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">revocationMode</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">Online</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">trustedStoreLocation</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">CurrentUser</span>&#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;/</span><span style="font-size: 10pt; color: #a31515;">clientCertificate</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">serviceCertificate</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">findValue</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="background: yellow; color: #0000ff; mso-highlight: yellow;">rajanadar.com</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">storeName</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">My</span>&#8220;<span style="color: #0000ff;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: "><span style="color: #ff0000;">                      storeLocation</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">LocalMachine</span>&#8220;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span></span><span style="font-size: 10pt; color: #ff0000;">x509FindType</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">FindBySubjectName</span>&#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: #0000ff; line-height: 115%;">&lt;/</span><span style="font-size: 10pt; color: #a31515; line-height: 115%;">serviceCredentials</span><span style="font-size: 10pt; color: #0000ff; line-height: 115%;">&gt;</span></p>
<p>I thought, the search ‘rajanadar.com&#8217; may be returning more than one certificate from the store. (may be due to root certificates etc.., I don&#8217;t know)</p>
<p>I checked my certificate store, and gave a specific (unique) Subject Name and tried different things.</p>
<p>no luck, still the same issue.</p>
<p>after a little observation, I read the error message a little more carefully.. (why didn&#8217;t I do this the first time?)</p>
<p><em><strong>Found multiple X.509 certificates using the following search criteria: StoreName &#8216;My&#8217;, StoreLocation &#8216;LocalMachine&#8217;, FindType &#8216;FindBySubjectName&#8217;, <span style="color: #333399;">FindValue &#8221;.</span> Provide a more specific find value.</strong></em></p>
<p>it complained of a blank ‘<strong>FindValue&#8217;</strong></p>
<p>then it struck me that we missed the FindValue for the client certificate, not the service certificate.</p>
<p>The corrected configuration was:</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;">&lt;</span><span style="font-size: 10pt; color: #a31515;">serviceCredentials</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">clientCertificate</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">certificate</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">storeLocation</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">LocalMachine</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">storeName</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">My</span>&#8220;<span style="color: #0000ff;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: "><span style="color: #ff0000;">        x509FindType</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">FindBySubjectName</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">findValue</span><span style="color: #0000ff;">=</span>&#8220;<span style="background: yellow; color: #0000ff; mso-highlight: yellow;">uniqueclient</span><span style="background: yellow; mso-highlight: yellow;">.<span style="color: #0000ff;">rajanadar.com</span></span> &#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">    </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">authentication</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">revocationMode</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">Online</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">trustedStoreLocation</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">CurrentUser</span>&#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;/</span><span style="font-size: 10pt; color: #a31515;">clientCertificate</span><span style="font-size: 10pt; color: #0000ff;">&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;">  </span>&lt;</span><span style="font-size: 10pt; color: #a31515;">serviceCertificate</span><span style="font-size: 10pt; color: #0000ff;"> </span><span style="font-size: 10pt; color: #ff0000;">findValue</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">server.rajanadar.com</span>&#8220;<span style="color: #0000ff;"> </span><span style="color: #ff0000;">storeName</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">My</span>&#8220;<span style="color: #0000ff;"> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: "><span style="color: #ff0000;">             storeLocation</span><span style="color: #0000ff;">=</span>&#8220;<span style="color: #0000ff;">LocalMachine</span>&#8220;</span><span style="font-size: 10pt; color: #0000ff;"><span style="mso-spacerun: yes;"> </span></span><span style="font-size: 10pt; color: #ff0000;">x509FindType</span><span style="font-size: 10pt; color: #0000ff;">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: #0000ff;">FindBySubjectName</span>&#8220;<span style="color: #0000ff;"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: #0000ff; line-height: 115%;">&lt;/</span><span style="font-size: 10pt; color: #a31515; line-height: 115%;">serviceCredentials</span><span style="font-size: 10pt; color: #0000ff; line-height: 115%;">&gt;</span></p>
<p>that solved the issue. it was a simple silly mistake. (obviously only after it was caught)</p>
<p> </p>
<p>the next error I encountered, sounded something like:</p>
<p><em><strong>Unhandled Exception: System.Net.WebException: The underlying connection was closed: Could not establish secure channel for SSL/TLS.</strong></em></p>
<p> </p>
<p>Fortunately, my past sleight of hand on WSE and SSL certificates, quickly reminded me that, when dealing with Web Applications, I need to give sufficient access permissions to the aspnet user account, to the PFX files of the certificates.</p>
<p>I modified the access permissions of the PFX file in question, (yeah the <a href="about:blank">\AppData\Microsoft\Crypto\RSA\MachineKeys</a> path) and the application seemed to work without any more issues. silly things, nonetheless there&#8217;s a first time..</p>
<p> </p>
<p>p.s. the aspnet user account permission issue reminds me of one more classic issue that I encountered most of the times..</p>
<p><strong><em>[SecurityException: Requested registry access is not allowed.]</em> </strong></p>
<p><em><strong>Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)</strong></em></p>
<p><em><strong>System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly)</strong></em></p>
<p><em><strong>System.Diagnostics.EventLog.SourceExists(String source, String machineName) +79</strong></em></p>
<p><em><strong>System.Diagnostics.EventLog.SourceExists(String source)</strong></em></p>
<p> </p>
<p>this is again because, creating a new event log or event source, needs registry write permissions, typically not possessed by the aspnet account.</p>
<p> </p>
<p><strong>Solution</strong>: initially, I used to grant write permissions to the registry keys</p>
<p><strong>(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\NewLog or HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\</strong><strong>Eventlog\Application\Source)<br />
</strong> </p>
<p>But then, this is not a good security approach. during the lifetime of the application, it just needs to write to the event logs and never create new ones. Hence I created the new registry keys (effectively new event logs/sources) using my Installers, and granted read permissions to the web application accounts. this sounded good.</p>
<p> </p>
<p><span style="font-size: 10pt; color: #548dd4;"><span style="color: #0000ff;">there’s a solution to every problem; given enough time and sometimes, well, just time&#8230;</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/07/wcf-certificates-event-logs-and-silly-security-exceptions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linq compiled queries (2 of 2)</title>
		<link>http://rajanadar.com/2008/06/linq-compiled-queries-2-of-2/</link>
		<comments>http://rajanadar.com/2008/06/linq-compiled-queries-2-of-2/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 18:42:11 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[c# 3.0]]></category>

		<category><![CDATA[linq]]></category>

		<category><![CDATA[compiled query]]></category>

		<category><![CDATA[delegates]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=17</guid>
		<description><![CDATA[so in my last post, I wrote about the Linq compiled queries syntax, before diving into its use. this post is about the usage of compiled queries.
every time, a Linq Query is executed (accessed), the expressions tree/Linq query is translated into its equivalent SQL/data source query and the results are fetched. for static data and [...]]]></description>
			<content:encoded><![CDATA[<p>so in my last <a href="http://rajanadar.com/2008/06/linq-compiled-queries-1-of-2/" target="_blank">post</a>, I wrote about the Linq compiled queries syntax, before diving into its use. this post is about the usage of compiled queries.</p>
<p>every time, a Linq Query is executed (accessed), the expressions tree/Linq query is translated into its equivalent SQL/data source query and the results are fetched. for static data and data, changing on parameter values, the repeated translation is redundant.</p>
<p>Assume we have the following methods:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">GetAllProductsAuthorizedToBeSoldInIndia<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;">// Linq query to join Products and Country table on key for </span>
 <span style="color: #008080; font-style: italic;">// India and return the expected products.</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>now if we access this method N times, the LINQ query will have N SQL translations, for the same result that it will fetch. clearly this is a performance bottleneck.</p>
<p>also consider a parameterized method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">GetAllProductsAuthorizedToBeSoldInACountry<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> countryId<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
 <span style="color: #008080; font-style: italic;">// Linq query to join Products and Country table on </span>
 <span style="color: #008080; font-style: italic;">// CountryId and return the expected products.</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Except for the parameter value, the generated SQL query has to be re-generated every time.</p>
<p>To solve this, we use Linq Compiled Queries. They facilitate one time translation of the Linq query, and re-use of the translated query across multiple calls.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyCompiledQueries
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> 
  Func<span style="color: #008000;">&lt;</span>MyDatabaseContext, IQueryable<span style="color: #008000;">&lt;</span>Product<span style="color: #008000;">&gt;&gt;</span> 
  ProductsAuthorizedToBeSoldInACountry 
  <span style="color: #008000;">=</span> CompiledQuery.<span style="color: #0000FF;">Compile</span><span style="color: #000000;">&#40;</span>
  <span style="color: #000000;">&#40;</span>MyDatabaseContext db, <span style="color: #FF0000;">int</span> countryId<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> 
  db.<span style="color: #0000FF;">Products</span><span style="color: #000000;">&#40;</span>p<span style="color: #008000;">=&gt;</span>p.<span style="color: #0000FF;">CountryId</span><span style="color: #008000;">=</span>countryId<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; 
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> List<span style="color: #008000;">&lt;</span>Product<span style="color: #008000;">&gt;</span>  
  GetAllProductsAuthorizedToBeSoldInACountry
  <span style="color: #000000;">&#40;</span>MyDatabaseContext db, <span style="color: #FF0000;">int</span> countryId<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> MyCompiledQueries. <span style="color: #0000FF;">ProductsAuthorizedToBeSoldInACountry</span><span style="color: #000000;">&#40;</span>
    db, countryId<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #008000;">&lt;</span>Product<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>the queries are translated once, and the parameter values are substituted at runtime.</p>
<ol>
<li>The CompileQuery.Compile method returns us a delegate, which can be invoked repeatedly.</li>
<li>The appropriate parameters need to be passed to the delegate during the invocation. (data context + query parameters)</li>
<li>Because we need to reuse the delegate to actually have any performance benefit, static variables/method wrappers are preferred.</li>
<li>It is a good practice to put all of your Compiled Queries into a separate class, at one place, and possibly have wrapper methods which make more business sense than entity names.</li>
<li>on my box, performance increased by 220%, when I changed my queries to Compiled Queries. I was working on a test domain data application using Linq. this was just slightly less than the data reader performance, we get.</li>
</ol>
<p>
the syntax for compiled queries makes it a little tough to maintain, but the benefits are sweet n totally worth the effort.</p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/06/linq-compiled-queries-2-of-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linq compiled queries (1 of 2)</title>
		<link>http://rajanadar.com/2008/06/linq-compiled-queries-1-of-2/</link>
		<comments>http://rajanadar.com/2008/06/linq-compiled-queries-1-of-2/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 18:49:46 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[c# 3.0]]></category>

		<category><![CDATA[linq]]></category>

		<category><![CDATA[compiled query]]></category>

		<category><![CDATA[delegates]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=16</guid>
		<description><![CDATA[I wanted to blog about Compiled queries in LINQ in this post. However, before doing that, I realized it would be good if I wrote something about the foundation on which the Compiled Query syntax is built.
This is because the call to a Compiled Linq Query involves calling the Compile() method:

public static Func&#60;TArg0, TArg1, TResult&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to blog about Compiled queries in LINQ in this post. However, before doing that, I realized it would be good if I wrote something about the foundation on which the Compiled Query syntax is built.</p>
<p>This is because the call to a Compiled Linq Query involves calling the Compile() method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Func<span style="color: #008000;">&lt;</span>TArg0, TArg1, TResult<span style="color: #008000;">&gt;</span> 
&nbsp;
CompiledQuery.<span style="color: #0000FF;">Compile</span><span style="color: #008000;">&lt;</span>TArg0, TArg1,  TResult<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #000000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TArg0, TArg1, TResult<span style="color: #008000;">&gt;&gt;</span> query<span style="color: #000000;">&#41;</span>
&nbsp;
 where TArg0 <span style="color: #008000;">:</span> DataContext;</pre></div></div>

<p>Now if the above syntax makes total sense to you, then you are probably over the following words. Else, you could read on to make sense of the above syntax.</p>
<p>Assume we have a delegate type defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #FF0000;">delegate</span> <span style="color: #FF0000;">int</span> MyDelegateType<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">int</span> b<span style="color: #000000;">&#41;</span></pre></div></div>

<p>this delegate can <em>contain</em> (thinking of code container) all methods which return ‘int&#8217; and take 2 integer parameters.<br />
E.g.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> AddMethod<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">int</span> b<span style="color: #000000;">&#41;</span>
&nbsp;
MyDelegateType myDelegateObject <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyDelegateType<span style="color: #000000;">&#40;</span>AddMethod<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>As you know, C# 2.0, simplifies this synax. we can now do:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">MyDelegateType myDelegateObject <span style="color: #008000;">=</span> AddMethod;</pre></div></div>

<p>looks good so far..</p>
<p>Now suppose, we make the delegate deal with generic types.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #FF0000;">delegate</span> T MyGenericDelegateType<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span>T a, T b<span style="color: #000000;">&#41;</span></pre></div></div>

<p>this delegate can contain all methods which return ‘T&#8217; type and take 2 T type parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">MyGenericDelegateType<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> myGenericDelegateObject <span style="color: #008000;">=</span> AddMethod;</pre></div></div>

<p>Now, this is the AddMethod</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> AddMethod<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">int</span> b<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">return</span> a <span style="color: #008000;">+</span> b;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you know using C# 2.0 anonymous methods, we could write:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">MyGenericDelegateType<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> myGenericDelegateObject <span style="color: #008000;">=</span>
<span style="color: #FF0000;">delegate</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">int</span> b<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> a <span style="color: #008000;">+</span> b <span style="color: #000000;">&#125;</span>;</pre></div></div>

<p>C# 3.0, further simplifies this anonymous method syntax, by eliminating even the delegate keyword, and getting the => operator. (Lambda Expressions coming in..)</p>
<p>Hence in C# 3.0, we can write:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">MyGenericDelegateType<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> myGenericDelegateObject <span style="color: #008000;">=</span>
<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">int</span> b<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF;">return</span> a <span style="color: #008000;">+</span> b;</pre></div></div>

<p>A further simplification to the above syntax is removing the types declared. This is because C# 3.0 is equipped with the type inference feature.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">MyGenericDelegateType<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> myGenericDelegateObject <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>a,b<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a <span style="color: #008000;">+</span> b;</pre></div></div>

<p>Notice how we removed even the return keyword. This is perfectly valid, if the method contains only a single return statement.</p>
<p>Now the <strong>System.Linq</strong> namespace already defines some generic delegates for us. e.g. One of them is:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> TResult Func<span style="color: #008000;">&lt;</span>A0, A1, TResult<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span>A0 arg0, A1 arg1<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>What this means is we don&#8217;t need to define our own delegate types, every time we want to use one with a similar signature.</p>
<p>we could directly say:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> myLinqDelegateObject <span style="color: #008000;">=</span> AddMethod;</pre></div></div>

<p>Here TResult, A0 and A1 are <strong>int</strong> types. Replacing by lambda expressions,</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> myLinqDelegateObject <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>a,b<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a<span style="color: #008000;">+</span>b;</pre></div></div>

<p>We can use this delegate object as</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #FF0000;">int</span> sum <span style="color: #008000;">=</span> myLinqDelegateObject<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span>;  <span style="color: #008080; font-style: italic;">// sum = 7;</span></pre></div></div>

<p>Here myLinqDelegateObject is nothing but a delegate object (instance), of a delegate type already defined by the System.Linq namespace.</p>
<p>An Expression Tree is nothing but an object of type</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Linq</span>.<span style="color: #0000FF;">Expressions</span>.<span style="color: #0000FF;">Expression</span><span style="color: #008000;">&lt;</span>TDelegateType<span style="color: #008000;">&gt;</span></pre></div></div>

<p>, where <strong>TDelegateType</strong> is the delegate the tree represents.</p>
<p>e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span>, <span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;&gt;</span> myExpressionTreeObject <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>a,b<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> a<span style="color: #008000;">+</span>b;</pre></div></div>

<p>Expression trees are nothing but an in memory representation of code. sort of a mini code segment, which can be evaluated later on demand, and can be built upon.</p>
<p>Based on all the points above, if we see the syntax now:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Func<span style="color: #008000;">&lt;</span>TArg0, TArg1, TResult<span style="color: #008000;">&gt;</span> 
&nbsp;
CompiledQuery.<span style="color: #0000FF;">Compile</span><span style="color: #008000;">&lt;</span>TArg0, TArg1,  TResult<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #000000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TArg0, TArg1, TResult<span style="color: #008000;">&gt;&gt;</span> query<span style="color: #000000;">&#41;</span>
&nbsp;
 where TArg0 <span style="color: #008000;">:</span> DataContext;</pre></div></div>

<p>It should look clear that, the Compile method takes in an Expression Tree object (query), of which the first parameter should be of type DataContext (Generic Contraint), and the return type is a Delegate object. <strong>Func<TArg0, TArg1, TResult></strong></p>
<p>So the use of the Compile method would be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">var queryDelegate <span style="color: #008000;">=</span>  CompiledQuery.<span style="color: #0000FF;">Compile</span><span style="color: #000000;">&#40;</span>
&nbsp;
  <span style="color: #000000;">&#40;</span>MyDataContext db, <span style="color: #FF0000;">string</span> param1, <span style="color: #FF0000;">string</span> param2<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
&nbsp;
  db.<span style="color: #0000FF;">MyEntity</span>
     .<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> v.<span style="color: #0000FF;">Column1</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> param1.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
     .<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>v <span style="color: #008000;">=&gt;</span> v.<span style="color: #0000FF;">Column2</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> param2.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Here queryDelegate is of type:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Func<span style="color: #008000;">&lt;</span>MyDataContext, <span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">string</span>, IQueryable<span style="color: #008000;">&lt;</span>MyEntity<span style="color: #008000;">&gt;&gt;</span></pre></div></div>

<p>and can be used as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">var MyEnitities <span style="color: #008000;">=</span> queryDelegate<span style="color: #000000;">&#40;</span>db, param1value, param2value<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>As you can see, concise delegate syntax ( => ), generics (T), anonymous methods/types (var), type inference (var), lambda expressions (=>) etc, work under the hood for the Compile method. C# 3.0 is cool.</p>
<p>As for the exact use of Linq compiled queries, their advantages and disadvantages, I&#8217;ll be writing about it in part 2 of this post..</p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/06/linq-compiled-queries-1-of-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>software life cycle stages</title>
		<link>http://rajanadar.com/2008/06/software-life-cycle-stages/</link>
		<comments>http://rajanadar.com/2008/06/software-life-cycle-stages/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 18:28:26 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[software engineering]]></category>

		<category><![CDATA[life cycle]]></category>

		<category><![CDATA[release]]></category>

		<category><![CDATA[software]]></category>

		<category><![CDATA[stages]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=15</guid>
		<description><![CDATA[ When I started with the terms, I was a bit confused on the order/meaning of the following in the software release life cycle: alpha, beta, gamma, CTP, RTM, RTW, RC, GA, Gold, Box Copy etc&#8230;
I decided to get clear on these terms with the help of my friend, Wiki. So completely based on it, here is [...]]]></description>
			<content:encoded><![CDATA[<p> When I started with the terms, I was a bit confused on the order/meaning of the following in the software release life cycle: alpha, beta, gamma, CTP, RTM, RTW, RC, GA, Gold, Box Copy etc&#8230;</p>
<p>I decided to get clear on these terms with the help of my friend, Wiki. So completely based on it, here is the quick summary of the release cycle stages, their order and meaning..</p>
<ol>
<li><strong>Pre-Alpha:</strong> Not feature complete, refers to all activities prior to Software Testing. e.g. Milestone versions, Nightly builds etc</li>
<li><strong>Alpha:</strong> Build delivered to the internal Software Testers for testing.</li>
<li><strong>Beta:</strong> (also known as <strong>Preview</strong>, <strong>Prototype</strong>, <strong>Technical Preview</strong> or <strong>Community Technical Preview</strong> Release stage): Build which has passed alpha testing and has been released to external customers/prospective customers (acting as free beta testers) for external feedback/improvements.</li>
<li><strong>Release Candidate (RC):</strong> (also known as <strong>Golden Master</strong>, <strong>Gamma</strong>, <strong>Delta</strong>, <strong>Omega</strong>, <strong>Zenith</strong> etc): A code complete version with potential to be a final product, with no fatal bugs/showstoppers.</li>
<li><strong>Gold</strong> (also known as <strong>General Availability</strong> Release <strong>GA</strong>): Production or Live version of a particular product. ready for distribution.</li>
<li><strong>Release To Manufacturing (RTM):</strong> Used when the Gold version is sent to a product manufacturer for physical distribution (CDs, DVDs etc) <strong>Box Copy </strong>is the term used for this physically created version of the product.</li>
<li><strong>Release To Web (RTW):</strong> If the RTM distribution is online, RTM is called as RTW.</li>
</ol>
<p>Always good to know the terms once, and be clear forever.</p>
<p>Source: <a href="http://en.wikipedia.org/wiki/Software_release_life_cycle" target="_blank">http://en.wikipedia.org/wiki/Software_release_life_cycle</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/06/software-life-cycle-stages/feed/</wfw:commentRss>
		</item>
		<item>
		<title>VSTS Testing Private Members and Methods using Accessors</title>
		<link>http://rajanadar.com/2008/06/vsts-testing-private-members-accessors/</link>
		<comments>http://rajanadar.com/2008/06/vsts-testing-private-members-accessors/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 23:48:32 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[unit testing]]></category>

		<category><![CDATA[accessors]]></category>

		<category><![CDATA[expectedexception]]></category>

		<category><![CDATA[private]]></category>

		<category><![CDATA[shadowing]]></category>

		<category><![CDATA[vsts]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=14</guid>
		<description><![CDATA[as explained in my previous post, we can easily test the public methods of a class. At the end of that blog, I mentioned about private accessors. this post explains that cool feature.
assume we have a class as follows:
 

public class BusinessObject
&#123;
    private const int MagicFactor = 18;
&#160;
    private int [...]]]></description>
			<content:encoded><![CDATA[<p>as explained in <a href="http://rajanadar.com/2008/05/vs-testing-suite-intro/" target="_blank">my previous post</a>, we can easily test the public methods of a class. At the end of that blog, I mentioned about <strong>private accessors</strong>. this post explains that cool feature.</p>
<p>assume we have a class as follows:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BusinessObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> MagicFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">18</span>;
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> CalculateConfidentialValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> baseAmount<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> baseAmount <span style="color: #008000;">*</span> BusinessObject.<span style="color: #0000FF;">MagicFactor</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>obviously, the method and magic factor are not super-confidential or anything. but let us pretend, they are.</p>
<p>here both the constant and the method are <strong>private</strong>. needless to say, the following Test Method won&#8217;t compile.</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WontCompileCalculateConfidentialValueTestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    BusinessObject myObject <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusinessObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #FF0000;">int</span> magicFactor <span style="color: #008000;">=</span> BusinessObject.<span style="color: #0000FF;">MagicFactor</span>;
&nbsp;
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">18</span>, magicFactor<span style="color: #000000;">&#41;</span>;
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>magicFactor <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span>, myObject.<span style="color: #0000FF;">CalculateConfidentialValue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>this is where private accessors help us. Private Accessors help us to access the Private members and Methods of an object, so that we can unit test them.</p>
<ol>
<li>go to the BusinessObjects class file</li>
<li>Right Click &gt;&gt; Create Private Accessor &gt;&gt; Test project..</li>
</ol>
<p>something similar is created behind the scenes:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>Shadowing<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;PrivateAccessorDemo.BusinessObject&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BusinessObject_Accessor <span style="color: #008000;">:</span> BaseShadow
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">static</span> PrivateType m_privateType;
&nbsp;
    <span style="color: #000000;">&#91;</span>Shadowing<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;.ctor@0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> BusinessObject_Accessor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0600FF;">public</span> BusinessObject_Accessor<span style="color: #000000;">&#40;</span>PrivateObject __p1<span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #000000;">&#91;</span>Shadowing<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;MagicFactor&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> MagicFactor <span style="color: #000000;">&#123;</span> get; <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> PrivateType ShadowedType <span style="color: #000000;">&#123;</span> get; <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> BusinessObject_Accessor AttachShadow<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> __p1<span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#91;</span>Shadowing<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;CalculateConfidentialValue@1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> CalculateConfidentialValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> baseAmount<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>you don&#8217;t need to try and understand that. all you need is how to use the created code, and here it is:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> CalculateConfidentialValueTestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    BusinessObject_Accessor target <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusinessObject_Accessor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #FF0000;">int</span> magicFactor <span style="color: #008000;">=</span> BusinessObject_Accessor.<span style="color: #0000FF;">MagicFactor</span>;
&nbsp;
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">18</span>, magicFactor<span style="color: #000000;">&#41;</span>;
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>magicFactor <span style="color: #008000;">*</span> <span style="color: #FF0000;">2</span>, target.<span style="color: #0000FF;">CalculateConfidentialValue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As we can see, we could now test the Private Member and the Private Method using the accessor.</p>
<p>Another good thing about the private accessors now, is the way we can test for exceptions thrown by the method.</p>
<p>Assume we modify our source method to:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BusinessObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> MagicFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">18</span>;
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> CalculateConfidentialValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> baseAmount<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>baseAmount <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;argument cannot be zero.&quot;</span>, <span style="color: #808080;">&quot;baseAmount&quot;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> baseAmount <span style="color: #008000;">*</span> BusinessObject.<span style="color: #0000FF;">MagicFactor</span>;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To test, if an argument exception is indeed thrown, we could write an unit test case as follows:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#91;</span>ExpectedException<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">ArgumentException</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> CalculateConfidentialValueWithZeroParameterTestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    BusinessObject_Accessor target <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusinessObject_Accessor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    target.<span style="color: #0000FF;">CalculateConfidentialValue</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<ol>
<li>I believe this is possible, because of the <strong>Shadowing</strong> attribute which is now used for private accessors changing the way exceptions are returned by the target object.</li>
<li>Before the Shadow based reflection, the accessor always threw a System.Reflection.TargetInvocationException and the inner exception needed to be evaluated.</li>
<li>The concise attribute based exception check for <strong>ArgumentException</strong> could not be checked.</li>
</ol>
<p> </p>
<p>Private Accessors are very useful, when you are testing the Business Layer or OM.</p>
<p><span style="font-size: 10pt; color: #548dd4;"><span style="color: #0000ff;">there’s a solution to every problem; given enough time and money..</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/06/vsts-testing-private-members-accessors/feed/</wfw:commentRss>
		</item>
		<item>
		<title>plead the friendly dev 5th</title>
		<link>http://rajanadar.com/2008/06/plead-the-friendly-dev-5th/</link>
		<comments>http://rajanadar.com/2008/06/plead-the-friendly-dev-5th/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 01:23:00 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[fun]]></category>

		<category><![CDATA[fifth]]></category>

		<category><![CDATA[humor]]></category>

		<category><![CDATA[plead]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=13</guid>
		<description><![CDATA[No dev shall be held to answer for a bug, or otherwise infamous defect, unless on a triage or request of a VP, except in cases arising in the penultimate or ultimate releases, or in a Red Alert, when in actual service in time of Bug War or Job Security; nor shall any dev be [...]]]></description>
			<content:encoded><![CDATA[<p>No <strong><span style="color: #000000;">dev</span></strong> shall be held to answer for a <strong>bug</strong>, or otherwise infamous <strong>defect</strong>, unless on a <strong>triage</strong> or request of a <strong>VP</strong>, except in cases arising in the <strong>penultimate</strong> or <strong>ultimate</strong> <strong>releases</strong>, or in a <strong>Red Alert</strong>, when in actual service in time of <strong>Bug War</strong> or <strong>Job Security</strong>; nor shall any <strong>dev</strong> be subject for the same <strong>code defect</strong> to be twice put in jeopardy of <strong>mind</strong> or <strong>fingers</strong>; nor shall be compelled in any <strong>bug fix</strong> to be a <strong>code defect</strong> against himself, nor be deprived of <strong>snacks, games, or soda</strong>, without due process of <strong>triage</strong>; nor shall private <strong>work items</strong> be taken for public<strong> bug fixing</strong>, without just compensation.</p>
<p><em>&#8211; intended for reading/laughing/forgetting purposes only</em></p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/06/plead-the-friendly-dev-5th/feed/</wfw:commentRss>
		</item>
		<item>
		<title>VS unit testing suite</title>
		<link>http://rajanadar.com/2008/05/vs-testing-suite-intro/</link>
		<comments>http://rajanadar.com/2008/05/vs-testing-suite-intro/#comments</comments>
		<pubDate>Sat, 24 May 2008 18:37:28 +0000</pubDate>
		<dc:creator>Raja Nadar</dc:creator>
		
		<category><![CDATA[unit testing]]></category>

		<category><![CDATA[attributes]]></category>

		<category><![CDATA[test case]]></category>

		<category><![CDATA[testing]]></category>

		<category><![CDATA[testrunconfig]]></category>

		<category><![CDATA[unit]]></category>

		<category><![CDATA[vsmdi]]></category>

		<category><![CDATA[vsts]]></category>

		<guid isPermaLink="false">http://rajanadar.com/?p=12</guid>
		<description><![CDATA[as you know, VS introduced its own unit testing framework, inbuilt for TDD. there are some very good things I like about it.

it removes the dependency from any external unit testing libraries like NUnit. (though NUnit was worth all its bits)
it is part of the build process and we can use the VSTS test runner [...]]]></description>
			<content:encoded><![CDATA[<p>as you know, VS introduced its own unit testing framework, inbuilt for TDD. there are some very good things I like about it.</p>
<ol>
<li>it removes the dependency from any external unit testing libraries like NUnit. (though NUnit was worth all its bits)</li>
<li>it is part of the build process and we can use the VSTS test runner (MSTest.exe), if the test cases need to be run separately, rather than from the IDE.</li>
<li>Code coverage can be done using the test config file.</li>
<li>it also provides support for deploying the required support files for the test cases to run successfully. (xml files, config files etc) explained later.</li>
<li>the code can be debugged using the test cases.</li>
<li>regression testing is very reliable, when features change in your application.</li>
<li>The test cases can be organized using the ‘Test Manager&#8217;</li>
</ol>
<p> Let us take a small and simple example. I have a sample ‘Add&#8217; method which I want to test:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Add<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">int</span> b<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> a <span style="color: #008000;">+</span> b;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To create a Unit test for this method/project, you could just right click inside the file containing this method &gt;&gt; <strong>Create Unit Tests</strong> and you can have the whole test project template within seconds.</p>
<p>The Unit Test is as follows:</p>
<p> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddTest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Calculator target <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Calculator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #FF0000;">int</span> a <span style="color: #008000;">=</span> <span style="color: #FF0000;">8</span>;
&nbsp;
    <span style="color: #FF0000;">int</span> b <span style="color: #008000;">=</span> <span style="color: #FF0000;">16</span>;
&nbsp;
    <span style="color: #FF0000;">int</span> expected <span style="color: #008000;">=</span> <span style="color: #FF0000;">24</span>;
    <span style="color: #FF0000;">int</span> actual;
&nbsp;
    actual <span style="color: #008000;">=</span> target.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>a, b<span style="color: #000000;">&#41;</span>;
&nbsp;
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span>expected, actual<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p> </p>
<ul>
<li>The Test class is attributed with the [TestClass()]attribute.</li>
<li>The Test method is attributed with the [TestMethod()]attribute.</li>
<li>There are also some other interesting attributes at the Test Method level:</li>
<li><span style="font-size: x-small;"><span style="font-size: x-small;">[ </span></span><span style="font-size: x-small; color: #2b91af;"><span style="font-size: x-small; color: #2b91af;">ExpectedException</span></span><span style="font-size: x-small;">(</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">typeof</span></span><span style="font-size: x-small;">(</span><span style="font-size: x-small; color: #2b91af;"><span style="font-size: x-small; color: #2b91af;">Exception</span></span><span style="font-size: x-small;">))]</span> &gt;&gt; This attribute makes the unit case, expect the type of exception provided, and does not fail the test case when that exception occurs. This is usefule if there are -ve test cases. (E.g FileNotFoundException, Business Exceptions etc)</li>
<li><span style="font-size: x-small;">[</span><span style="font-size: x-small; color: #2b91af;"><span style="font-size: x-small; color: #2b91af;">Description</span></span><span style="font-size: x-small;">(</span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">"Test Case Id: 2.34 Add two valid numbers."</span></span><span style="font-size: x-small;">)] </span>&gt;&gt; This attribute makes it easier to name a test case according to your test case list. Whenever the test case fails, you can see the Description and identify the exact test case which failed.</li>
<li><span style="font-size: x-small;"><span style="font-size: x-small;">[<span style="color: #2b91af;">WorkItem</span><span style="font-size: x-small;">(9999)]</span> &gt;&gt; This attribute is helpful in associating the test case with a work item in your scrum/tfs/project management tool.</span></span></li>
<li><span style="font-size: x-small;"><span style="font-size: x-small;">[<span style="color: #2b91af;">Ignore</span><span style="font-size: x-small;">()] </span>&gt;&gt; If there are some test cases, which need not be executed at this point of time, this attribute can be used.</span></span></li>
</ul>
<p><strong> TestSolution.vsmdi</strong></p>
<ul>
<li>This file is basically what the test manager controls. It is used to group all the test cases in the solution.</li>
<li>As you notice, this file is a solution level item and hence common to all the project unit cases in your application.</li>
<li>You can create ‘<strong>New Test Lists&#8217;</strong> and arrange your test cases as per Projects/Business Objects/Layers etc.</li>
<li>Once arranged, the specific test cases can be executed.</li>
</ul>
<p><strong>localtestrun.testrunconfig</strong></p>
<ul>
<li>This is the common configuration file for all the test cases in your application.</li>
<li>It controls the naming scheme for the output folder it creates, where the test run results are stored.</li>
<li><strong>Code Coverage</strong> can be enabled for the assemblies.</li>
<li>The <strong>Deployment</strong> option provides the support files, which you may want to deploy for the test cases to run successfully.</li>
<li>It also provides a ‘<strong>Startup&#8217;</strong> and ‘<strong>Cleanup&#8217;</strong> script option to be run, before and after the test case run.</li>
</ul>
<p>there are other cool features of the Unit Testing framework like, reading test data from data sources, private <strong>accessors</strong>, web tests, load testing, linking it to external test <strong>controllers</strong> and agents etc..</p>
<p> <span style="font-size: 10pt; color: #548dd4;"><span style="color: #0000ff;">there’s a solution to every problem; given enough time and money..</span></span></p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://rajanadar.com/2008/05/vs-testing-suite-intro/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
