<?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; NHibernate</title>
	<atom:link href="http://www.primedigit.com/category/nhibernate/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>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>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>
	</channel>
</rss>
