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

<channel>
	<title>PrimeDigit - A Design Blog by Will Shaver &#187; c#</title>
	<atom:link href="http://www.primedigit.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.primedigit.com</link>
	<description>Thoughts on ASP.NET, Visual Design, SQL Server 2005, C# and much more...</description>
	<lastBuildDate>Tue, 29 Jun 2010 16:33:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MVC RC1</title>
		<link>http://www.primedigit.com/2009/01/28/mvc-rc1/</link>
		<comments>http://www.primedigit.com/2009/01/28/mvc-rc1/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 18:05:05 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2009/01/28/mvc-rc1/</guid>
		<description><![CDATA[As I&#8217;m sure you noticed, MVC RC1 is out now. In reading through the release notes I found this: &#8220;The overloads for DropDownList and ListBox helper methods were reworked to fix various usability problems that were reported by customers.&#8221; Thanks for listening!]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;m sure you noticed, <a href="http://haacked.com/archive/2009/01/27/aspnetmvc-release-candidate.aspx">MVC RC1</a> is out now. In reading through the release notes I found this:</p>
<blockquote><p><em>&#8220;The overloads for DropDownList and ListBox helper methods were reworked to fix various usability problems that were reported by customers.&#8221;</em></p></blockquote>
<p><a href="http://www.primedigit.com/2008/12/10/headaches-with-aspmvc-ui-helpers/">Thanks for listening!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2009/01/28/mvc-rc1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GAC Conflicts with ASP MVC</title>
		<link>http://www.primedigit.com/2008/12/24/gac-conflicts-with-asp-mvc/</link>
		<comments>http://www.primedigit.com/2008/12/24/gac-conflicts-with-asp-mvc/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 17:04:58 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/12/24/gac-conflicts-with-asp-mvc/</guid>
		<description><![CDATA[By some strange combination of beta and pre-release candidate installs I managed to create some serious conflicts with ASP.NET MVC. Thereby producing this wonderful little error: Compiler Error Message: CS0433: The type 'System.Web.Mvc.FormMethod' exists in both 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Mvc\1.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll' and 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\06c0b3cf\32bec25d\assembly\dl3\71bfb65b\e11bf5c5_9e55c901\System.Web.Mvc.DLL' I am using my own build of the source from codeplex so that I [...]]]></description>
			<content:encoded><![CDATA[<p>By some strange combination of beta and pre-release candidate installs I managed to create some serious conflicts with ASP.NET MVC. Thereby producing this wonderful little error:</p>
<p><code>Compiler Error Message: CS0433: The type 'System.Web.Mvc.FormMethod' exists in both 'c:\WINDOWS\assembly\GAC_MSIL\System.Web.Mvc\1.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll' and 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\06c0b3cf\32bec25d\assembly\dl3\71bfb65b\e11bf5c5_9e55c901\System.Web.Mvc.DLL'</code></p>
<p>I am using my own build of the source from codeplex so that I can debug through it to improve my knowledge of the codebase. From research online it looks like the assembly version between the latest pre-release and the beta weren&#8217;t incremented, or the web-runner tries to look in the GAC before the /bin, or the evil garden gnomes are at it again, or Microsoft wants to punish me for building THEIR dll files.</p>
<p>To solve this problem, i opened up the AssemblyInfo.cs files of the Mvc project and upped the AssemblyVersion to 1.0.0.9. Then hacked my web config to use that version. As I&#8217;m running in debug mode, and can&#8217;t sign the assembly I also had to remove the PublicKeyToken from the reference.</p>
<p><code>&lt;add assembly="System.Web.Mvc, Version=1.0.0.9, Culture=neutral"/&gt;</code></p>
<p>And suddenly we&#8217;re back in business.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/12/24/gac-conflicts-with-asp-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sql Server DateTime Conversion</title>
		<link>http://www.primedigit.com/2008/12/02/sql-server-datetime-conversion/</link>
		<comments>http://www.primedigit.com/2008/12/02/sql-server-datetime-conversion/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 17:05:29 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/12/02/sql-server-datetime-conversion/</guid>
		<description><![CDATA[Suppose that your model requires selecting over a date range. It is frequently more convenient to store DateTime.MinValue than it is to store a NULL as this allows for easy selecting without doing a bunch of field < value or null or field > value or not null monkey business. The only problem is that [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose that your model requires selecting over a date range. It is frequently more convenient to store DateTime.MinValue than it is to store a NULL as this allows for easy selecting without doing a bunch of <em>field < value or null or field > value or not null</em> monkey business. The only problem is that DateTime.MinValue <a href="http://groups.google.com/group/nhusers/msg/0ec4a377d04de179">doesn&#8217;t fit into SQL server</a>, and DateTime.MaxValue won&#8217;t store correctly either. You can even end up where you get a situation where you are storing DateTime.MaxValue then checking a freshly loaded entity to see if it equals DateTime.MaxValue and it doesn&#8217;t! </p>
<p>One solution is to create an extension method to trim down the DateTime to only include valid values in a deterministic way as follows:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \tab \cf3 public\cf0  \cf3 static\cf0  \cf3 class\cf0  \cf4 DateTimeExtensions\par ??\cf0 \tab \{\par ??\tab \tab \cf3 private\cf0  \cf3 static\cf0  \cf3 readonly\cf0  \cf4 DateTime\cf0  minSqlDateTime = \cf4 DateTime\cf0 .Parse(\cf5 "1/1/1753 12:00:00 AM"\cf0 );\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 static\cf0  \cf4 DateTime\cf0  ToSqlDateTime(\cf3 this\cf0  \cf4 DateTime\cf0  dt)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 if\cf0  (dt &lt; minSqlDateTime)\par ??\tab \tab \tab \tab dt = minSqlDateTime;\par ??\tab \tab \tab \cf3 return\cf0  \cf3 new\cf0  \cf4 DateTime\cf0 (dt.Ticks - (dt.Ticks % 10000000));            \par ??\tab \tab \}\par ??\tab \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">DateTimeExtensions</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">DateTime</span> minSqlDateTime = <span style="color: #2b91af;">DateTime</span>.Parse(<span style="color: #a31515;">"1/1/1753 12:00:00 AM"</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">DateTime</span> ToSqlDateTime(<span style="color: blue;">this</span> <span style="color: #2b91af;">DateTime</span> dt)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dt &lt; minSqlDateTime)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt = minSqlDateTime;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(dt.Ticks - (dt.Ticks % 10000000));&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
</div>
<p>Note that this is designed for SQL Server. Other database types may need different levels of precision. </p>
<p>Instead you could implement a custom IUserType for NHibernate which tells NHibernate how to translate a DateTime that is out of bounds into one that is in bounds such as:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \tab \cf3 public\cf0  \cf3 class\cf0  \cf4 SqlDateTimeUserType\cf0  : \cf4 IEnhancedUserType\par ??\cf0 \tab \{\par ??\tab \tab \cf3 private\cf0  \cf3 static\cf0  \cf3 readonly\cf0  \cf4 DateTime\cf0  minSqlDateTime = \cf4 DateTime\cf0 .Parse(\cf5 "1/1/1753 12:00:00 AM"\cf0 );\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 new\cf0  \cf3 bool\cf0  Equals(\cf3 object\cf0  x, \cf3 object\cf0  y)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  x == \cf3 null\cf0  ? y == \cf3 null\cf0  : x.Equals(y);\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf4 Type\cf0  ReturnedType\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 get\cf0  \{ \cf3 return\cf0  \cf3 typeof\cf0 (\cf4 DateTime\cf0 ); \}\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf4 SqlType\cf0 [] SqlTypes\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 get\cf0  \{ \cf3 return\cf0  \cf3 new\cf0 [] \{ \cf4 NHibernateUtil\cf0 .DateTime.SqlType \}; \}\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 int\cf0  GetHashCode(\cf3 object\cf0  x)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  x.GetHashCode();\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 object\cf0  NullSafeGet(\cf4 IDataReader\cf0  dr, \cf3 string\cf0 [] names, \cf3 object\cf0  owner)\par ??\tab \tab \{\par ??\tab \tab \tab \cf6 //no need to alter values coming out of the database\par ??\cf0 \tab \tab \tab \cf4 DateTime\cf0 ? obj = \cf4 NHibernateUtil\cf0 .DateTime.NullSafeGet(dr, names[0]) \cf3 as\cf0  \cf4 DateTime\cf0 ?;\par ??            \cf3 return\cf0  obj == \cf3 null\cf0  ? ToSqlDateTime(\cf4 DateTime\cf0 .MinValue) : obj.Value;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 void\cf0  NullSafeSet(\cf4 IDbCommand\cf0  cmd, \cf3 object\cf0  obj, \cf3 int\cf0  index)\par ??\tab \tab \{\par ??\tab \tab \tab \cf6 //set it to a safe value going into the database\par ??\cf0 \tab \tab \tab ((\cf4 IDataParameter\cf0 )cmd.Parameters[index]).Value = ToSqlDateTime((\cf4 DateTime\cf0 )obj);\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 object\cf0  DeepCopy(\cf3 object\cf0  value)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  value;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 object\cf0  Replace(\cf3 object\cf0  original, \cf3 object\cf0  target, \cf3 object\cf0  owner)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  original;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 object\cf0  Assemble(\cf3 object\cf0  cached, \cf3 object\cf0  owner)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  cached;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 object\cf0  Disassemble(\cf3 object\cf0  value)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  value;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 bool\cf0  IsMutable\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 get\cf0  \{ \cf3 return\cf0  \cf3 false\cf0 ; \}\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 object\cf0  FromXMLString(\cf3 string\cf0  xml)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  \cf4 DateTime\cf0 .Parse(xml);\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 string\cf0  ObjectToSQLString(\cf3 object\cf0  value)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  value \cf3 as\cf0  \cf3 string\cf0 ;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 public\cf0  \cf3 string\cf0  ToXMLString(\cf3 object\cf0  value)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 return\cf0  value \cf3 as\cf0  \cf3 string\cf0 ;\par ??\tab \tab \}\par ??\par ??\tab \tab \cf3 private\cf0  \cf3 static\cf0  \cf4 DateTime\cf0  ToSqlDateTime(\cf4 DateTime\cf0  dt)\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 if\cf0  (dt &lt; minSqlDateTime)\par ??\tab \tab \tab \tab dt = minSqlDateTime;\par ??\tab \tab \tab \cf3 return\cf0  \cf3 new\cf0  \cf4 DateTime\cf0 (dt.Ticks - (dt.Ticks % 10000000));\par ??\tab \tab \}\par ??\tab \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">SqlDateTimeUserType</span> : <span style="color: #2b91af;">IEnhancedUserType</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">DateTime</span> minSqlDateTime = <span style="color: #2b91af;">DateTime</span>.Parse(<span style="color: #a31515;">"1/1/1753 12:00:00 AM"</span>);</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">new</span> <span style="color: blue;">bool</span> Equals(<span style="color: blue;">object</span> x, <span style="color: blue;">object</span> y)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> x == <span style="color: blue;">null</span> ? y == <span style="color: blue;">null</span> : x.Equals(y);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">Type</span> ReturnedType</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">DateTime</span>); }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">SqlType</span>[] SqlTypes</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">new</span>[] { <span style="color: #2b91af;">NHibernateUtil</span>.DateTime.SqlType }; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> GetHashCode(<span style="color: blue;">object</span> x)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> x.GetHashCode();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> NullSafeGet(<span style="color: #2b91af;">IDataReader</span> dr, <span style="color: blue;">string</span>[] names, <span style="color: blue;">object</span> owner)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//no need to alter values coming out of the database</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DateTime</span>? obj = <span style="color: #2b91af;">NHibernateUtil</span>.DateTime.NullSafeGet(dr, names[0]) <span style="color: blue;">as</span> <span style="color: #2b91af;">DateTime</span>?;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> obj == <span style="color: blue;">null</span> ? ToSqlDateTime(<span style="color: #2b91af;">DateTime</span>.MinValue) : obj.Value;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> NullSafeSet(<span style="color: #2b91af;">IDbCommand</span> cmd, <span style="color: blue;">object</span> obj, <span style="color: blue;">int</span> index)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//set it to a safe value going into the database</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ((<span style="color: #2b91af;">IDataParameter</span>)cmd.Parameters[index]).Value = ToSqlDateTime((<span style="color: #2b91af;">DateTime</span>)obj);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> DeepCopy(<span style="color: blue;">object</span> value)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> value;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Replace(<span style="color: blue;">object</span> original, <span style="color: blue;">object</span> target, <span style="color: blue;">object</span> owner)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> original;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Assemble(<span style="color: blue;">object</span> cached, <span style="color: blue;">object</span> owner)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> cached;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> Disassemble(<span style="color: blue;">object</span> value)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> value;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> IsMutable</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">false</span>; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> FromXMLString(<span style="color: blue;">string</span> xml)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">DateTime</span>.Parse(xml);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> ObjectToSQLString(<span style="color: blue;">object</span> value)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> value <span style="color: blue;">as</span> <span style="color: blue;">string</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> ToXMLString(<span style="color: blue;">object</span> value)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> value <span style="color: blue;">as</span> <span style="color: blue;">string</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">DateTime</span> ToSqlDateTime(<span style="color: #2b91af;">DateTime</span> dt)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (dt &lt; minSqlDateTime)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt = minSqlDateTime;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(dt.Ticks - (dt.Ticks % 10000000));</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
</div>
<p>These user types are mapped as follows:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green0\blue0;}??\fs20 \cf1     &lt;\cf3 property\cf1  \cf4 name\cf1 =\cf0 "\cf1 ImportDate\cf0 "\cf1  \cf4 type\cf1 =\cf0 "\cf1 Framework.NHibernate.SqlDateTimeUserType, Framework\cf0 "\cf1 /&gt;\par ??}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">property</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">ImportDate</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Framework.SqlDateTimeUserType, Framework</span>"<span style="color: blue;">/&gt;</span></pre>
</div>
<p>The only problem with this is that a simple test such as the following will fail:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \tab \tab [\cf3 Test\cf0 ]\par ??\tab \tab \cf4 public\cf0  \cf4 void\cf0  CanSaveBadValuesToImportDate()\par ??\tab \tab \{\par ??\tab \tab \tab \cf3 With\cf0 .AutoRollbackTransaction(() =&gt;\par ??\tab \tab \tab \{\par ??\tab \tab \tab \tab \cf3 Unit\cf0  u = \cf4 new\cf0  \cf3 Unit\cf0 (\cf5 "u"\cf0 );\par ??\tab \tab \tab \tab \cf3 Product\cf0  p = \cf4 new\cf0  \cf3 Product\cf0 (\cf5 "p1"\cf0 , u);\par ??\tab \tab \tab \tab p.ImportDate = \cf3 DateTime\cf0 .MinValue;\par ??\tab \tab \tab \tab Session.SaveAll(u, p);\par ??\tab \tab \tab \tab \cf3 Guid\cf0  id = p.Id;\par ??\tab \tab \tab \tab Session.Flush();\par ??\tab \tab \tab \tab Session.Clear();\par ??\tab \tab \tab \tab \cf3 Product\cf0  p2 = Session.Load&lt;\cf3 Product\cf0 &gt;(id);\par ??\tab \tab \tab \tab \cf3 Assert\cf0 .AreEqual(p.Name,p2.Name);\par ??\tab \tab \tab \tab \cf3 Assert\cf0 .AreEqual(p.ImportDate, p2.ImportDate); \cf6 //fails here\par ??\cf0 \tab \tab \tab \});\par ??\tab \tab \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Test</span>]</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> CanSaveBadValuesToImportDate()</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">With</span>.AutoRollbackTransaction(() =&gt;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Product</span> p = <span style="color: blue;">new</span> <span style="color: #2b91af;">Product</span>(<span style="color: #a31515;">"Apples"</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; p.ImportDate = <span style="color: #2b91af;">DateTime</span>.MinValue;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Session.Save(p);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Session.Flush();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Session.Clear();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Product</span> p2 = Session.Load&lt;<span style="color: #2b91af;">Product</span>&gt;(p.Id);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.AreEqual(p.Name,p2.Name);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Assert</span>.AreEqual(p.ImportDate, p2.ImportDate); <span style="color: green;">//fails here</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; });</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
</div>
<p>We could try and create a bunch of edge conditions on our converter such as:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red0\green128\blue0;}??\fs20 \tab \tab \cf3 public\cf0  \cf3 object\cf0  NullSafeGet(\cf4 IDataReader\cf0  dr, \cf3 string\cf0 [] names, \cf3 object\cf0  owner)\par ??\tab \tab \{\par ??\tab \tab \tab \cf5 //no need to alter values coming out of the database\par ??\cf0 \tab \tab \tab \cf4 DateTime\cf0 ? obj = \cf4 NHibernateUtil\cf0 .DateTime.NullSafeGet(dr, names[0]) \cf3 as\cf0  \cf4 DateTime\cf0 ?;\par ??\tab \tab \tab \cf3 if\cf0  (obj != \cf3 null\cf0  &amp;&amp; obj == minSqlDateTime)\par ??\tab \tab \tab \tab \cf3 return\cf0  \cf4 DateTime\cf0 .MinValue;\par ??            \cf3 return\cf0  obj == \cf3 null\cf0  ? ToSqlDateTime(\cf4 DateTime\cf0 .MinValue) : obj.Value;\par ??\tab \tab \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> NullSafeGet(<span style="color: #2b91af;">IDataReader</span> dr, <span style="color: blue;">string</span>[] names, <span style="color: blue;">object</span> owner)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">DateTime</span>? obj = <span style="color: #2b91af;">NHibernateUtil</span>.DateTime.NullSafeGet(dr, names[0]) <span style="color: blue;">as</span> <span style="color: #2b91af;">DateTime</span>?;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (obj != <span style="color: blue;">null</span> &amp;&amp; obj == minSqlDateTime)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">DateTime</span>.MinValue;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> obj == <span style="color: blue;">null</span> ? ToSqlDateTime(<span style="color: #2b91af;">DateTime</span>.MinValue) : obj.Value;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
</div>
<p>But that could get out of hand in a hurry. It also would cause errors if you stored DateTime.MinValue.AddSeconds(1). If we wanted to go really over the top, we could create our own class that implements operator DateTime (explicit and implicit) which would do the conversion to a SQL range when we first assign a value. This would solve both problems, but you&#8217;d end up with your domain needing a special DateTime class for all of your entities instead of using the built in. Your call&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/12/02/sql-server-datetime-conversion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ModelStateDictionary and ParameterBinding</title>
		<link>http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/</link>
		<comments>http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 18:14:38 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/</guid>
		<description><![CDATA[I laid this out in more detail on the forums here: http://forums.asp.net/p/1317154/2610342.aspx#2610342 I thought I&#8217;d mention it to this group so that people were aware of this difference between the interaction the old ParameterBinders and Rescues in version 4 and the new ModelBinders and HandleErrorAttributes of version 5. The new way of doing parameter binding [...]]]></description>
			<content:encoded><![CDATA[<p>I laid this out in more detail on the forums here:<br />
<a href="http://forums.asp.net/p/1317154/2610342.aspx#2610342">http://forums.asp.net/p/1317154/2610342.aspx#2610342</a></p>
<p>I thought I&#8217;d mention it to this group so that people were aware of this difference between the interaction the old ParameterBinders and Rescues in version 4 and the new ModelBinders and HandleErrorAttributes of version 5.</p>
<p>The new way of doing parameter binding expects all errors to be placed into the ModelStateDictionary specified in the GetValue method.<br />
public override object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState) {</p>
<p>The ModelStateDictionary can then be checked in the controller for errors via ViewData.ModelState.IsValid</p>
<p>I think the idea is that multiple binding errors can then be reported to the user at one time:<br />
foreach (var value in ViewData.ModelState.Values)<br />
{<br />
 value.Errors&#8230;..<br />
}</p>
<p>Unfortunatly our current NameValueDeserializer that is used by the DeserializeAttribute throws exceptions when it encounters errors. These exceptions are NOT caught by Rescues / HandleErrorAttributes. The ControllerActionInvoker (in preview 5) calls</p>
<p>IDictionary<string, object> parameters = GetParameterValues(methodInfo);</p>
<p>before the try/catch block that passes off errors to the HandleErrorAttributes. So if you&#8217;re using any kind of attribute deserialization and expecting your exceptions to be displayed all pretty like in your rescues&#8230; then move line #177 to line #180 of your ControllerActionInvoker.cs :)</p>
<p> -Will</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ISession Extension for Saving Multiple Objects</title>
		<link>http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/</link>
		<comments>http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 22:36:51 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/</guid>
		<description><![CDATA[Incredibly simple, but quite useful: &#160;&#160;&#160; &#160;&#160;&#160; public static void Save(this ISession session, params object [] entities) &#160;&#160;&#160; &#160;&#160;&#160; { &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; foreach (var o in entities) &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; { &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; session.Save(o); &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; } &#160;&#160;&#160; &#160;&#160;&#160; } You can use it like: session.Save(a,b,c,d,&#8230;.); with a list of objects [...]]]></description>
			<content:encoded><![CDATA[<p>Incredibly simple, but quite useful:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20         \cf3 public\cf0  \cf3 static\cf0  \cf3 void\cf0  Save(\cf3 this\cf0  \cf4 ISession\cf0  session, \cf3 params\cf0  \cf3 object\cf0  [] entities)\par ??        \{\par ??            \cf3 foreach\cf0  (\cf3 var\cf0  o \cf3 in\cf0  entities)\par ??            \{\par ??                session.Save(o);\par ??            \}\par ??        \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Save(<span style="color: blue;">this</span> <span style="color: #2b91af;">ISession</span> session, <span style="color: blue;">params</span> <span style="color: blue;">object</span> [] entities)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> o <span style="color: blue;">in</span> entities)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; session.Save(o);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
</div>
<p>You can use it like:</p>
<p>session.Save(a,b,c,d,&#8230;.);</p>
<p>with a list of objects of any type. Useful for unit testing where you want to save multiple items at once.</p>
<p>[Update]</p>
<p>Probably should call it something other than Save, such as SaveAll or SaveList because if you only pass two items to the overload it calls the standard Save(object, int id) version. :(</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CriteriaByLongAlias</title>
		<link>http://www.primedigit.com/2008/06/16/criteriabylongalias/</link>
		<comments>http://www.primedigit.com/2008/06/16/criteriabylongalias/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 16:01:57 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/06/16/criteriabylongalias/</guid>
		<description><![CDATA[This simple function cleans up a lot of the Alias sillyness I have fought with in NHibernate. &#160;&#160;&#160; &#160;&#160;&#160; public static ICriteria CriteriaByLongAlias(this ICriteria criteria, string field) &#160;&#160;&#160; &#160;&#160;&#160; { &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; ICriteria byAlias = criteria.GetCriteriaByAlias(criteria.Alias + &#8220;_&#8221; + field) ?? &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; criteria.CreateCriteria(field, criteria.Alias + &#8220;_&#8221; + [...]]]></description>
			<content:encoded><![CDATA[<p>This simple function cleans up a lot of the Alias sillyness I have fought with in NHibernate.</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20         \cf3 public\cf0  \cf3 static\cf0  \cf4 ICriteria\cf0  CriteriaByLongAlias(\cf3 this\cf0  \cf4 ICriteria\cf0  criteria, \cf3 string\cf0  field)\par ??        \{\par ??            \cf4 ICriteria\cf0  byAlias = criteria.GetCriteriaByAlias(criteria.Alias + \cf5 "_"\cf0  + field) ??\par ??                                criteria.CreateCriteria(field, criteria.Alias + \cf5 "_"\cf0  + field);\par ??            \cf3 return\cf0  byAlias;\par ??        \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ICriteria</span> CriteriaByLongAlias(<span style="color: blue;">this</span> <span style="color: #2b91af;">ICriteria</span> criteria, <span style="color: blue;">string</span> field)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ICriteria</span> byAlias = criteria.GetCriteriaByAlias(criteria.Alias + <span style="color: #a31515;">&#8220;_&#8221;</span> + field) ??</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; criteria.CreateCriteria(field, criteria.Alias + <span style="color: #a31515;">&#8220;_&#8221;</span> + field);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> byAlias;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p>It is used any time you would previously call CreateCriteria and pass an alias.<br />
Before:<br />
session.CreateCriteria(typeof (Product)).CreateCriteria(&#8220;Orders&#8221;,&#8221;Product_Orders&#8221;);<br />
After:<br />
session.CreateCriteria(typeof(Product)).CriteriaByLongAlias(&#8220;Orders&#8221;);</p>
<p>The alias part is done automatically based on the criteria path. The advantage is that it works recursively, allowing for multiple criteria to be created, and then referenced without duplicates.</p>
<p>ICriteria criteria = session.CreateCriteria(typeof(Product)).CriteriaByLongAlias(&#8220;Orders&#8221;).CriteriaByLongAlias(&#8220;Customer&#8221;).Add(Expression.Eq(&#8220;Name&#8221;, &#8220;Ace Hardware&#8221;));<br />
criteria.CriteriaByLongAlias(&#8220;Orders&#8221;).Add(Expression.Eq(&#8220;OrderNumber&#8221;,12354));</p>
<p>etc.</p>
<p>Note that in order for it to work, you have to create all your subcriteria with it.</p>
<p> -Will</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/06/16/criteriabylongalias/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool Code &#8211; Assembly.FindType</title>
		<link>http://www.primedigit.com/2008/06/04/cool-code-assemblyfindtype/</link>
		<comments>http://www.primedigit.com/2008/06/04/cool-code-assemblyfindtype/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 22:06:32 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/06/04/cool-code-assemblyfindtype/</guid>
		<description><![CDATA[Here&#8217;s an extension to Assembly that I found useful for finding a type. I recommend doing typeof(TypeInAssembly).Assembly.FindType(&#8220;typename&#8221;); &#160;&#160;&#160; public static class AssemblyExtensions &#160;&#160;&#160; { &#160;&#160;&#160; &#160;&#160;&#160; public static Type FindType(this Assembly assembly, string typename) &#160;&#160;&#160; &#160;&#160;&#160; { &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; return assembly.FindTypes(typename).FirstOrDefault(); &#160;&#160;&#160; &#160;&#160;&#160; } &#160; &#160;&#160;&#160; &#160;&#160;&#160; public static Type[] FindTypes(this Assembly assembly, string [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an extension to Assembly that I found useful for finding a type. I recommend doing<br />
typeof(TypeInAssembly).Assembly.FindType(&#8220;typename&#8221;);</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20     \cf3 public\cf0  \cf3 static\cf0  \cf3 class\cf0  \cf4 AssemblyExtensions\par ??\cf0     \{\par ??        \cf3 public\cf0  \cf3 static\cf0  \cf4 Type\cf0  FindType(\cf3 this\cf0  \cf4 Assembly\cf0  assembly, \cf3 string\cf0  typename)\par ??        \{\par ??            \cf3 return\cf0  assembly.FindTypes(typename).FirstOrDefault();\par ??        \}\par ??\par ??        \cf3 public\cf0  \cf3 static\cf0  \cf4 Type\cf0 [] FindTypes(\cf3 this\cf0  \cf4 Assembly\cf0  assembly, \cf3 string\cf0  typename)\par ??        \{\par ??            \cf4 Type\cf0 [] types = assembly.GetExportedTypes();\par ??            \cf4 List\cf0 &lt;\cf4 Type\cf0 &gt; found = \cf3 new\cf0  \cf4 List\cf0 &lt;\cf4 Type\cf0 &gt;();\par ??            \cf3 foreach\cf0 (\cf4 Type\cf0  type \cf3 in\cf0  types)\par ??            \{\par ??                \cf3 if\cf0 (type.Name == typename)\par ??                \{\par ??                    found.Add(type);\par ??                \}\par ??            \}\par ??            \cf3 return\cf0  found.ToArray();\par ??        \}\par ??    \}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AssemblyExtensions</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Type</span> FindType(<span style="color: blue;">this</span> <span style="color: #2b91af;">Assembly</span> assembly, <span style="color: blue;">string</span> typename)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> assembly.FindTypes(typename).FirstOrDefault();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Type</span>[] FindTypes(<span style="color: blue;">this</span> <span style="color: #2b91af;">Assembly</span> assembly, <span style="color: blue;">string</span> typename)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Type</span>[] types = assembly.GetExportedTypes();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Type</span>&gt; found = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: #2b91af;">Type</span>&gt;();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span>(<span style="color: #2b91af;">Type</span> type <span style="color: blue;">in</span> types)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span>(type.Name == typename)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; found.Add(type);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> found.ToArray();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/06/04/cool-code-assemblyfindtype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contributing to NHibernate</title>
		<link>http://www.primedigit.com/2008/04/22/contributing-to-nhibernate/</link>
		<comments>http://www.primedigit.com/2008/04/22/contributing-to-nhibernate/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 04:12:35 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/04/22/contributing-to-nhibernate/</guid>
		<description><![CDATA[Having successfully submitted a few patches to NHibernate [NH-1280][NH-1260][NH-1259], Ayende Rahien asked me to comment on the difficulty of adding a totally new feature, and provide some hints for others. I am by no means an expert on NHibernate, just a regular user so here goes&#8230; My most recent change set was updating the Criteria [...]]]></description>
			<content:encoded><![CDATA[<p>
Having successfully submitted a few patches to NHibernate [<a href="http://jira.nhibernate.org:8080/jira/browse/NH-1280">NH-1280</a>][<a href="http://jira.nhibernate.org:8080/jira/browse/NH-1260">NH-1260</a>][<a href="http://jira.nhibernate.org:8080/jira/browse/NH-1259">NH-1259</a>], <a href="http://www.ayende.com/Blog/">Ayende Rahien</a> asked me to comment on the difficulty of adding a totally new feature, and provide some hints for others. I am by no means an expert on NHibernate, just a regular user so here goes&#8230;
</p>
<p>
My most recent change set was updating the Criteria Query API to allow for queries involving <a href="http://msdn2.microsoft.com/en-us/library/ms180199.aspx">HAVING clauses</a>. In performing this change I also discovered a bug whereby complicated queries would get parameters out of order. In this post I will cover:</p>
<ul>
<li>The feature added (need and implementation)</li>
<li>Determining problem need</li>
<li>Hints for getting your patch accepted</li>
</ul>
<h3>The Feature</h3>
<p>The Criteria Query API provides some valuable tools for creating dynamic queries without doing crazy string manipulation (yuck). I had a situation where I wanted to validate entities to check for duplicates prior to the transaction being committed. Because of the temporal nature of the project I&#8217;m working on, I can&#8217;t do it with a table constraint or other simple means and need to check valid dates.
</p>
<p>To solve it with HQL would involved about 10 lines of messy reflection and string concats where solving it with the criteria API took about 4. Here&#8217;s the code:
</p>
<pre><code>ICriteria criteria = session.CreateCriteria(typeof(Person));
criteria.SetProjection(Projections.ProjectionList()
  .Add(Projections.Max("Id"))
  .Add(Projections.GroupProperty("Name")))
  .ProjectionCriteria.Add(Restrictions.Gt(Projections.Count("Name"), 1));</code></pre>
<p>The resulting SQL for this code should be:</p>
<pre><code>"SELECT MAX(Id), Name FROM Person GROUP BY Name HAVING COUNT(Name) > 1"</code></pre>
<p>
Imagine my surprise when this relatively simple Criteria Query failed while the same HQL query succeeded. After examining the SQL it produced I determined that there was no HAVING clause being produced.
</p>
<p>
Fearing that I was doing something wrong I opened up the <a href="http://www.hibernate.org/6.html">source code for NHibernate</a> and performed a textual search for &#8220;having&#8221; as I knew that at some point it must be produced by the SQL generator. I was unable to locate anything in the Criteria API that was even remotely related to HAVING queries.
</p>
<p>
Having exhausted that possibility I posted  to the <a href="http://groups.google.com/group/nhusers/browse_thread/thread/408e293a5557a5ea/2beba87522b9898d?lnk=gst#">google nhusers group</a> to make sure that I wasn&#8217;t missing something. After some discussion it was obvious that this was indeed a lacking feature and that it would be appreciated by the community.
</p>
<div class="pullout">
<h4>Quick Review</h4>
<ol>
<li>Confirm that the problem is with the codebase not with your use of it.
<ol>
<li>Review the code to determine if it is possible to produce the desired result.</li>
<li>Post to the nhusers group to make sure you&#8217;re using the API correctly.</li>
</ol>
</li>
<li>Confirm the problem isn&#8217;t solved in the JAVA version. (If it is, import that fix.)</li>
<li>Migrate the discussion to the NHibernate developer list for help.</li>
</ol>
</div>
<p>
After determining that it was indeed a problem with the codebase I launched into patching it. I wrote test cases to be able to debug into the code in the Criteria interface. I recommend adding the folder for your tests as:<br />
<code>\src\NHibernate.Test\NHSpecificTest\NH1999</code><br />
Then migrating it to the specific issue number after you&#8217;ve made progress enough to report the issue. This prevents nasty collision problems if someone else takes the next number.<br />
<code>\src\NHibernate.Test\NHSpecificTest\NH1280</code><br />
I&#8217;ve also recently taken to adding a description after the folder such as &#8220;NH1291AnonExample&#8221; in my latest patch. This makes it easier to determine which set of tests has failed for other developers.
</p>
<p>
The patch itself is rather complicated and out of the scope of this post as it contained changes to almost 50 files. In writing the patch I tried a couple of different methods for adding my features and even had to revert a couple of files from the source. I worked from the output to the input, backing out from the SQL generator to the individual Criteria and Criterion classes.
</p>
<p>
As I wrote the patch I made sure that I didn&#8217;t negatively impact other use cases. If your patch breaks other tests it won&#8217;t be accepted even if it implements &#8220;teh best featre evar&#8221;! For added tests I merged my changes into both my active work project and the Linq to NHibernate project. Doing so revealed cases that I hadn&#8217;t considered in my initial tests and allowed me to add in those tests and deliver a more solid patch.
</p>
<div class="pullout">
<h4>Quick Review</h4>
<ol>
<li value="4">Write a small set of failing tests first. Step-Into these tests while debugging for where to start coding.</li>
<li>Don&#8217;t fear changing the source, you can always revert!</li>
<li>Start at the interface points and work toward the center where actual work is performed.</li>
<li>Make sure all unit tests, including those in other projects are passing.</li>
</ol>
</div>
<p>
Before submitting any patch I always go through a cleanup of my changed code. This involved going through my patch file by file making sure that I&#8217;m only submitting code that pertains to the patch and only changes things that need to be changed. (Using <a href="http://tortoisesvn.tigris.org/">TortoiseSVN</a> this is quite simple, folder menu -> check for modifications)
</p>
<p>
I try and keep my formatting changes to a minimum. Resist the urge to re-arrange existing code, convert tabs to spaces etc. While NHibernate and many other open source projects could use some cleanup work, that should be performed by dedicated committers. In our case the organization is loosely tied to the Hibernate project and would make it more difficult to port newer features if the NHibernate code is re-arranged.
</p>
<p>
Submit the patch and leave a clear explanation of the problem it solves and how it solves it. Best of luck and thanks for contributing to the project!
</p>
<div class="pullout">
<h4>Quick Review</h4>
<ol>
<li value="8">Clean up your code before you commit by reviewing the changes.</li>
<li>Add your patch the JIRA, documenting the change need and usage.</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/04/22/contributing-to-nhibernate/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MVC UnitTestingFramework to MvcContrib</title>
		<link>http://www.primedigit.com/2008/02/20/mvc-unittestingframework-to-mvccontrib/</link>
		<comments>http://www.primedigit.com/2008/02/20/mvc-unittestingframework-to-mvccontrib/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 04:47:09 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/02/20/mvc-unittestingframework-to-mvccontrib/</guid>
		<description><![CDATA[The Unit Testing Framework I posted a couple of days ago has now been moved into the MvcContrib project, making me an official Developer. Woo Hoo. You can check out the updated documentation.]]></description>
			<content:encoded><![CDATA[<p>The Unit Testing Framework I posted a couple of days ago has now been moved into the <a href="http://www.codeplex.com/MVCContrib">MvcContrib</a> project, making me an official <a href="http://www.codeplex.com/MVCContrib/People/ProjectPeople.aspx">Developer</a>. Woo Hoo. You can check out the <a href="http://www.codeplex.com/MVCContrib/Wiki/View.aspx?title=TestHelper&#038;referringTitle=Documentation">updated documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/02/20/mvc-unittestingframework-to-mvccontrib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IParseable Interface</title>
		<link>http://www.primedigit.com/2008/02/20/iparseable-interface/</link>
		<comments>http://www.primedigit.com/2008/02/20/iparseable-interface/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 02:57:23 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.primedigit.com/2008/02/20/iparseable-interface/</guid>
		<description><![CDATA[[Note: I was reminded by my roommate after posting this that static methods aren't allowed in interfaces....] Today I was working on a JSON to NHibernate bridge when I came across something quite frustrating when trying to convert strings to other types. In this bridge object I&#8217;m persisting an object out of the database with [...]]]></description>
			<content:encoded><![CDATA[<p><strong>[Note: I was reminded by my roommate after posting this that static methods aren't allowed in interfaces....]</strong></p>
<p>Today I was working on a JSON to NHibernate bridge when I came across something quite frustrating when trying to convert strings to other types. In this bridge object I&#8217;m persisting an object out of the database with NHibernate, then assigning the new values from the JSON string to that object. (Or list of objects.) </p>
<p>This seemed simple enough. I was thinking that it should be easy enough to write a loop that looks at the javascript keys {&#8220;Key&#8221;:&#8221;Value&#8221;} and matches them up with the PropertyInfo of the target object. This is trivial when both are strings, but what if the target type is a float, int, bool, date, enum etc? </p>
<p>Most base types have a Parse method of some kind that looks like Parse(string) or Parse(string, IFormatProvider). Here&#8217;s a couple of types that implement some variant of the Parse method. </p>
<div class="columnlist small">
<ul>
<li>System.Boolean</li>
<li>System.Byte</li>
<li>System.Char</li>
<li>System.Data.SqlTypes.SqlBoolean</li>
<li>System.Data.SqlTypes.SqlByte</li>
<li>System.Data.SqlTypes.SqlDateTime</li>
<li>System.Data.SqlTypes.SqlDecimal</li>
<li>System.Data.SqlTypes.SqlDouble</li>
<li>System.Data.SqlTypes.SqlGuid</li>
<li>System.Data.SqlTypes.SqlInt16</li>
<li>System.Data.SqlTypes.SqlInt32</li>
<li>System.Data.SqlTypes.SqlInt64</li>
<li>System.Data.SqlTypes.SqlMoney</li>
<li>System.Data.SqlTypes.SqlSingle</li>
<li>System.DateTime</li>
<li>System.DateTimeOffset</li>
<li>System.Decimal</li>
<li>System.Double</li>
<li>System.Int16</li>
<li>System.Int32</li>
<li>System.Int64</li>
<li>System.SByte</li>
<li>System.Single</li>
<li>System.TimeSpan</li>
<li>System.UInt16</li>
<li>System.UInt32</li>
<li>System.UInt64</li>
<li>System.Windows.Int32Rect</li>
<li>System.Windows.Point</li>
<li>System.Windows.Rect</li>
<li>System.Windows.Size</li>
<li>System.Windows.Vector</li>
<li>System.Net.NetworkInformation.PhysicalAddress</li>
</ul>
<p><br clear="left">
</div>
<p>This leaves me with only one question?</p>
<h3>Where&#8217;s my Interface?</h3>
<p>I can&#8217;t do a test for <code>IParseable</code> on the object, I can&#8217;t even search for the parse method by name and parameters because each object implements them differently. I could go with only the string parameter which they all implement, but I&#8217;ve learned from Civ4 that culture is important, and besides I wouldn&#8217;t want to fail <a href="http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html">the Turkey test</a>.</p>
<p>Of course there&#8217;s a reason for this seemingly lazy design &#8211; interfaces can&#8217;t be declared as static. (That&#8217;s a conversation for a different day.) Ok, so given that they can&#8217;t, where do we go from here? Lets pretend that we still wanted to use an Interface for these classes. </p>
<div class="cf">
<p class="cl"><span class="cb1">interface</span> <span class="cb2">IParseable</span> { }</p>
</div>
<p>So now we&#8217;ve got an <a href="http://msdn2.microsoft.com/en-us/library/ms182128.aspx">Empty Interface</a>. Perhaps this would be better as a custom attribute, and if so please explain to me how to accomplish this next part with said custom attribute.</p>
<p>We then assign our interface to the DataType and implement a number of Parse methods</p>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">class</span> <span class="cb2">DataType</span> : <span class="cb2">IParseable</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb2">DataType</span> Parse(<span class="cb1">string</span> s)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb3">//blah blah, use the string to create a new DataType</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">new</span> <span class="cb2">DataType</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>Then in the System.Convert class we&#8217;d implement some function like this:</p>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> T Parse&lt;T&gt;(<span class="cb1">string</span> s) <span class="cb1">where</span> T : <span class="cb2">IParseable</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">MethodInfo</span> mInfo = <span class="cb1">typeof</span>(T).GetMethod(<span class="cb3">&#8220;Parse&#8221;</span>, <span class="cb2">BindingFlags</span>.Public | <span class="cb2">BindingFlags</span>.Static);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (mInfo != <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; T obj = (T) mInfo.Invoke(<span class="cb1">null</span>, <span class="cb1">new</span> <span class="cb1">object</span>[] { s });</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> obj;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">default</span>(T);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>Other methods could accept CultureInfo, FormatProviders and other params. Someone have a better way of doing this?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.primedigit.com/2008/02/20/iparseable-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
