<?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>Nelson Montalvo&#039;s Blog</title>
	<atom:link href="http://lostechies.com/nelsonmontalvo/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/nelsonmontalvo</link>
	<description>Just another LosTechies site</description>
	<lastBuildDate>Sat, 26 Mar 2011 02:44:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>NHibernate Query Example using CreateCriteria</title>
		<link>http://lostechies.com/nelsonmontalvo/2008/04/13/nhibernate-query-example-using-createcriteria/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2008/04/13/nhibernate-query-example-using-createcriteria/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 13:18:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2008/04/13/nhibernate-query-example-using-createcriteria.aspx</guid>
		<description><![CDATA[I&#8217;m a big fan of NHibernate. I love how it abstracts out the data portion of your application and allows you to work from the perspective of the domain rather than the database. So in building my domain repositories, I&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2008/04/13/nhibernate-query-example-using-createcriteria/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of NHibernate. I love how it abstracts out the data portion of your application and allows you to work from the perspective of the domain rather than the database.</p>
<p>So in building my domain repositories, I have several options to query the database using NHibernate:</p>
<ul>
<li>I can use HQL in CreateQuery.</li>
<li>I can use named queries in GetNamedQuery.</li>
<li>I can use SQL directly in CreateSqlQuery.</li>
<li>I can use the type of object in a CreateCriteria.</li>
</ul>
<p>Short of using Ayende&#8217;s NHibernate Query Generator to strongly type my HQL queries, I like using the CreateCriteria call.</p>
<h3>The Example</h3>
<p>
To begin with, I&#8217;m not a fan of generic repository interfaces with a ton of generics-based CRUD operations (IRepository&lt;T&gt;). I&#8217;d rather just limit my repository code to what is needed for operating with that aggregate root (driven by the domain, of course).</p>
<p>For this example, I have an interface in my domain project called <i>CommissionPeriodRepository</i> (yes, no &#8216;I&#8217; in front of my interfaces &#8211; again, personal preference&#8230; lol). The implementation class, <i>NHibernateCommissionPeriodRepository</i>, resides in an NHibernate specific project implementing the repository interfaces. Finally, I&#8217;m assuming that the NHibernate session (Unit of Work is used here, but is NHibernate specific) is managed by the calling application, committing and rolling back changes as necessary.&nbsp;</p>
<div style="background: white none repeat scroll 0% 50%;font-size: 12pt;color: black;font-family: inconsolata">
<pre style="margin: 0pt">    <span style="color: blue">public</span> <span style="color: blue">class</span> <span>NHibernateCommissionPeriodRepository</span> : <span>CommissionPeriodRepository</span></pre>
<pre style="margin: 0pt">    {</pre>
<pre style="margin: 0pt"><span style="color: blue">        #region</span> CommissionPeriodRepository Members</pre>
<pre style="margin: 0pt"></pre>
<pre style="margin: 0pt">        <span style="color: blue">public</span> <span>CommissionPeriod</span> GetCommissionPeriodFor(<span>DateTime</span> date)</pre>
<pre style="margin: 0pt">        {</pre>
<pre style="margin: 0pt">            <span style="color: blue">return</span> <span>UnitOfWork</span>.GetCurrentSession()</pre>
<pre style="margin: 0pt">                .CreateCriteria(<span style="color: blue">typeof</span> (<span>CommissionPeriod</span>))</pre>
<pre style="margin: 0pt">                .Add(<span>Expression</span>.Lt(<span>"Startdate"</span>, date))</pre>
<pre style="margin: 0pt">                .Add(<span>Expression</span>.Ge(<span>"Enddate"</span>, date))</pre>
<pre style="margin: 0pt">                .UniqueResult&lt;<span>CommissionPeriod</span>&gt;();</pre>
<pre style="margin: 0pt">        }</pre>
<pre style="margin: 0pt"></pre>
<pre style="margin: 0pt"><span style="color: blue">        #endregion</span></pre>
<pre style="margin: 0pt">    }</pre>
</div>
<p>Pretty much all the code is doing is:</p>
<ol>
<li>Pulling the current NHibernate session.</li>
<li>Creating a criteria object for a CommissionPeriod domain object.</li>
<li>Pulling the unique CommissionPeriod object that has a start date less than that passed in date and an end date greater than or equal to it.</li>
</ol>
<p>That&#8217;s it. Simple stuff. Gotta love it.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2008/04/13/nhibernate-query-example-using-createcriteria/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ReSharper Keymappings</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/10/28/resharper-keymappings/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/10/28/resharper-keymappings/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 07:51:54 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/10/28/resharper-keymappings.aspx</guid>
		<description><![CDATA[As part of the install of ReSharper 3.0 install into Visual Studio 2008, I &#34;accidentally&#34; selected the Visual Studio keymappings to try it out. Yikes&#8230; what a mistake. I couldn&#8217;t stand it. It turned out to be some form of&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/10/28/resharper-keymappings/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As part of the install of ReSharper 3.0 install into Visual Studio 2008, I &quot;accidentally&quot; selected the Visual Studio keymappings to try it out. </p>
<p>Yikes&#8230; what a mistake. I couldn&#8217;t stand it. It turned out to be some form of tough love.</p>
<p>I&#8217;m afraid that I&#8217;m too used to ReSharper and none of the Visual Studio keymappings are worth losing the ReSharper keymappings for (come on&#8230; no Ctrl-B?!?!). lol</p>
<p>So I changed it back:</p>
<p><a href="http://www.lostechies.com/blogs/nelson_montalvo/WindowsLiveWriter/ReSharperKeymappings_3669/image_2.png"><img style="border-top-width: 0px;border-left-width: 0px;border-bottom-width: 0px;border-right-width: 0px" height="324" alt="image" src="http://www.lostechies.com/blogs/nelson_montalvo/WindowsLiveWriter/ReSharperKeymappings_3669/image_thumb.png" width="644" border="0" /></a></p>
<p>As somebody on the team once said, Visual Studio is just a glorified form of Notepad without ReSharper. lol</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/10/28/resharper-keymappings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Interesting Silverlight demo site</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/10/11/interesting-silverlight-demo-site/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/10/11/interesting-silverlight-demo-site/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 11:17:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/10/11/interesting-silverlight-demo-site.aspx</guid>
		<description><![CDATA[Interesting demo of Silverlight and AJAX for eCommerce. Video on the demo site and a product image on the demo site. &#160;The site isn&#8217;t anything particularly revolutionary, but a good example of Silverlight in action.&#160;]]></description>
			<content:encoded><![CDATA[<p>Interesting <a href="http://catalogsample.members.winisp.net/" target="_blank">demo </a>of Silverlight and AJAX for eCommerce.</p>
<p><a href="http://catalogsample.members.winisp.net/Browse.aspx?CID=Apparel" target="_blank">Video</a> on the demo site and a <a href="http://catalogsample.members.winisp.net/ProductDetails.aspx?SKU=SunGlasses2&amp;CID=Sunglasses" target="_blank">product image </a>on the demo site.</p>
<p>&nbsp;<br />The site isn&#8217;t anything particularly revolutionary, but a good example of <a href="http://silverlight.net/" target="_blank">Silverlight</a> in action.<br />&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/10/11/interesting-silverlight-demo-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Please contribute to Team Hanselman&#8217;s Diabetes Walk 2007</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/10/07/please-contribute-to-team-hanselman-s-diabetes-walk-2007/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/10/07/please-contribute-to-team-hanselman-s-diabetes-walk-2007/#comments</comments>
		<pubDate>Sun, 07 Oct 2007 08:12:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/10/07/please-contribute-to-team-hanselman-s-diabetes-walk-2007.aspx</guid>
		<description><![CDATA[After&#160;watching Scott Hanselman&#160;participate in several of the open spaces at Alt.Net this weekend, I have a different level of appreciation for Scott&#8217;s blog. He is not only prolific in his blogging and very knowledgeable, but &#8220;in the real world&#8221; he&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/10/07/please-contribute-to-team-hanselman-s-diabetes-walk-2007/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After&nbsp;watching <a href="http://www.hanselman.com/blog/" target="_blank">Scott Hanselman</a>&nbsp;participate in several of the open spaces at Alt.Net this weekend, I have a different level of appreciation for Scott&#8217;s blog. He is not only prolific in his blogging and very knowledgeable, but &#8220;in the real world&#8221; he is well spoken,&nbsp;has a&nbsp;good sense of humor, and can hold his own&nbsp;in an open space format with good points, good&nbsp;questions and tactful responses.<a href="http://www.hanselman.com/fightdiabetes" target="_blank"><img src="http://www.hanselman.com/fightdiabetes/HanselmanBadge150.png" style="margin: 0px 0px 5px 10px" align="right" border="0"></a></p>
<p>On his blog, Scott has a link to&nbsp;his <a href="http://www.hanselman.com/fightdiabetes" target="_blank">Team Hanselman&#8217;s Diabetes Walk 2007</a> page. On it, you can contribute. I would like to say that I contributed a small amount&nbsp;simply because Scott&#8217;s tips have been so dang useful over the past few years &#8211; his tools list alone has been priceless -&nbsp;but my dad is type II and it does suck. So please contribute what you can.</p>
<p>Thanks.</p>
<div style="height: 50px"></div>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/10/07/please-contribute-to-team-hanselman-s-diabetes-walk-2007/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The &quot;Domain Model&quot;</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/09/27/the-quot-domain-model-quot/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/09/27/the-quot-domain-model-quot/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 01:02:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[Domain Driven Design]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/09/26/the-quot-domain-model-quot.aspx</guid>
		<description><![CDATA[There has been some recent discussion on User Stories, the Ubiquitous Language of a domain, and Domain Models. I have been fairly confused because I am not clear on what folks have meant by the &#8220;Domain Model.&#8221; Some folks seem&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/09/27/the-quot-domain-model-quot/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There has been some recent discussion on User Stories, the Ubiquitous Language of a domain, and Domain Models. I have been fairly confused because I am not clear on what folks have meant by the &#8220;Domain Model.&#8221; Some folks seem to be referring to the true code construct that results from 1) having a user story define some desired behavior, 2) discussion of this behavior, and 3) refinement of the concepts into the code construct.</p>
<p>From the beginning of Evan&#8217;s book, I think of the domain model as:</p>
<p>&nbsp;&#8221;A <span class="docTextHighlight">domain</span><a class="" name="the diagram"></a> model is not a particular diagram; it is the idea that the diagram is intended to convey. It is not just the knowledge in a <span class="docTextHighlight">domain</span> expert&#8217;s head; <span class="docEmphasis"><a class="" name="of that"></a>it is a rigorously organized and selective abstraction of that knowledge</span><a class="" name="can carefully"></a>. A diagram can represent and communicate a model, as can carefully written code, as can an English sentence.&#8221;</p>
<p>Yes, the domain language does drive the model. And user stories drive that original discussion. But the model is not just code. Once discussed, refined, and hammered out, it is these concepts of the domain model that enter the ubiquitous language, whether or not those concepts end up in code.</p>
<p>My $2. I don&#8217;t disagree with other folks out there &#8211; just explaining what I&#8217;ve always thought of the model.</p>
<p>What do you guys think?<br />&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/09/27/the-quot-domain-model-quot/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Castle&#8217;s ActiveRecord: Not for the Domain Purist in you&#8230;</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/04/17/castle-s-activerecord-not-for-the-domain-purist-in-you/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/04/17/castle-s-activerecord-not-for-the-domain-purist-in-you/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 00:45:26 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/04/16/castle-s-activerecord-not-for-the-domain-purist-in-you.aspx</guid>
		<description><![CDATA[&#160;I finally &#8220;broke down&#8221; and began using ActiveRecord just to try it out. Here&#8217;s the tutorial that I used. Now, don&#8217;t get me wrong -&#160;ActiveRecord is built on top of NHibernate and it&#8217;s one of the fastest ways to start&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/04/17/castle-s-activerecord-not-for-the-domain-purist-in-you/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;I finally &#8220;broke down&#8221; and began using ActiveRecord just to try it out. <a href="http://www.castleproject.org/monorail/gettingstarted/ar.html" target="_blank">Here&#8217;s the tutorial</a> that I used. Now, don&#8217;t get me wrong -&nbsp;ActiveRecord is built on top of NHibernate and it&#8217;s one of the fastest ways to start working with your persistence layer.</p>
<p>So what&#8217;s the problem?</p>
<p>Well, it&#8217;s this:</p>
<div style="font-size: 10pt;background: white;color: black;font-family:">
<p style="margin: 0px"><span style="color: blue">using</span> Castle.ActiveRecord;</p>
<p style="margin: 0px"><span style="color: blue">namespace</span> Foo.Domain</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">ActiveRecord</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Dealer</span> : <span style="color: #2b91af">ActiveRecordBase</span>&lt;<span style="color: #2b91af">Dealer</span>&gt;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">int</span> id;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">string</span> contactFirstName;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">string</span> contactLastName;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">string</span> businessName;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">Address</span> address;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">string</span> email;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">PhoneNumber</span> contactPhoneNumber;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: #2b91af">PhoneNumber</span> faxNumber;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> Dealer()</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; address = <span style="color: blue">new</span> <span style="color: #2b91af">Address</span>();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; contactPhoneNumber = <span style="color: blue">new</span> <span style="color: #2b91af">PhoneNumber</span>();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; faxNumber = <span style="color: blue">new</span> <span style="color: #2b91af">PhoneNumber</span>();</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: #2b91af">PrimaryKey</span>(<span style="color: #2b91af">PrimaryKeyType</span>.Identity, <span style="color: #a31515">"DealerId"</span>, Access=<span style="color: #2b91af">PropertyAccess</span>.NosetterCamelcase)]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">int</span> Id</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">get</span> { <span style="color: blue">return</span> id; }</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: #2b91af">Property</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> BusinessName</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">get</span> { <span style="color: blue">return</span> businessName; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { businessName = <span style="color: blue">value</span>; }</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: #2b91af">Property</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> ContactFirstName</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">get</span> { <span style="color: blue">return</span> contactFirstName; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { contactFirstName = <span style="color: blue">value</span>; }</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: #2b91af">Property</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> ContactLastName</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">get</span> { <span style="color: blue">return</span> contactLastName; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { contactLastName = <span style="color: blue">value</span>; }</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: #2b91af">Property</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> Email</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">get</span> { <span style="color: blue">return</span> email; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { email = <span style="color: blue">value</span>; }</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: #2b91af">Nested</span>(<span style="color: #a31515">"ContactPhoneNumber"</span>)]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: #2b91af">PhoneNumber</span> ContactPhoneNumber</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">get</span> { <span style="color: blue">return</span> contactPhoneNumber; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { contactPhoneNumber = <span style="color: blue">value</span>; }</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: #2b91af">Nested</span>(<span style="color: #a31515">"FaxNumber"</span>)]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: #2b91af">PhoneNumber</span> FaxNumber</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">get</span> { <span style="color: blue">return</span> faxNumber; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { contactPhoneNumber = <span style="color: blue">value</span>; }</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: #2b91af">Nested</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: #2b91af">Address</span> Address</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">get</span> { <span style="color: blue">return</span> address; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">set</span> { address = <span style="color: blue">value</span>; } </p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">}</p>
</div>
<p>Those attributes are tagging persistence concerns all over my domain object!!&nbsp;The class also inherits&nbsp;from ActiveRecordBase&lt;T&gt;, a persistence focused class having nothing to do with&nbsp;the domain itself.</p>
<p>Finally,&nbsp;the class if forced to expose a setter for&nbsp;the value objects: ContactPhoneNumber, FaxNumber, and Address.&nbsp;The class&nbsp;should not have to allow external objects to directly set properties that access value objects. An external object can directly overwrite the value object references managed by my Dealer aggregate. (Please tell me I&#8217;m wrong on this.)</p>
<p>Of course,&nbsp;these are&nbsp;known&nbsp;drawbacks to using AR&nbsp;and it&#8217;s really only a concern for domain purists out there (who me?). All in all, ActiveRecord is great for getting started with simple domains that map fairly easily to their relevant database tables.</p>
<p>Here&#8217;s all I had to do in my test to get AR Unit Testing working.</p>
<p>1. I borrowed and slightly modified the unit testing base class, <strong>AbstractModelTestCase,</strong> from <a href="http://wiki.castleproject.org/index.php/ActiveRecord:How_to:Unit_testing" target="_blank">this article</a>. I modified it to support usage of NDbUnit (the original, not my modified version):</p>
<div style="font-size: 10pt;background: white;color: black;font-family:">
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">SetUp</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> Init()</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; sqlDbUnit = <span style="color: blue">new</span> <span style="color: #2b91af">SqlDbUnitTest</span>(GetConnectionString());</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PrepareSchema();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CreateScope();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p>&nbsp;2. I wrote my&nbsp;specifications utilizing an NDbUnit schema and data file. It was as easy as:</p>
<div style="font-size: 10pt;background: white;color: black;font-family:">
<p style="margin: 0px"><span style="color: blue">using</span> Foo.Domain;</p>
<p style="margin: 0px"><span style="color: blue">using</span> NUnit.Framework;</p>
<p style="margin: 0px"><span style="color: blue">using</span> NDbUnit.Core;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">namespace</span> Specifications.Foo.Domain.ActiveRecord</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">TestFixture</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ADealerListWithOneDealer</span> : <span style="color: #2b91af">AbstractModelTestCase</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> PrepareSchema()</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; sqlDbUnit.ReadXmlSchema(<span style="color: #a31515">@&#8221;&#8230;.SchemasDealerSchema.xsd&#8221;</span>);</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlDbUnit.ReadXml(<span style="color: #a31515">@&#8221;&#8230;.TestDataTestDataForADealerListWithOneDealer.xml&#8221;</span>);</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlDbUnit.PerformDbOperation(<span style="color: #2b91af">DbOperationFlag</span>.CleanInsert);</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: #2b91af">Test</span>]</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> ShouldHaveASizeOfOne()</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; FlushAndRecreateScope();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Dealer</span>[] dealers = <span style="color: #2b91af">Dealer</span>.FindAll();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Assert</span>.AreEqual(1, dealers.Length, <span style="color: #a31515">&#8220;Should have a length of 1, given the test data.&#8221;</span>);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">}</p>
</div>
<p>As you can see, almost no persistence code was written to accomplish this trivial bit of work.</p>
<p><u>Repositories</u></p>
<p>Repositories are not explicit using the AR method of persisting entities, as shown above. Persistence concerns are directly associated with the entity (via Find(), FindAll(), etc). Ayende has an Active Record repository implementation in his Rhino Commons library. The SVN url is <a title="https://svn.sourceforge.net/svnroot/rhino-tools/trunk/" href="https://svn.sourceforge.net/svnroot/rhino-tools/trunk/"><strong>https://svn.sourceforge.net/svnroot/rhino-tools/trunk/</strong></a>&nbsp;and the code can be found under <strong>rhino-commons/Rhino.Commons/Repositories</strong>.</p>
<p>Ayende&#8217;s ARRepository&nbsp;implementation appears to be a way around inheriting from ActiveRecordBase&lt;T&gt;, but I&#8217;m not sure. Does anybody else know?</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/04/17/castle-s-activerecord-not-for-the-domain-purist-in-you/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Stories in TargetProcess</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/04/05/stories-in-targetprocess/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/04/05/stories-in-targetprocess/#comments</comments>
		<pubDate>Thu, 05 Apr 2007 01:02:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[Agile]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/04/04/stories-in-targetprocess.aspx</guid>
		<description><![CDATA[TargetProcess is fantastic tool for managing projects using Agile techniques. Gone are the days of 6 month Gantt charts, not truly knowing where you&#8217;re at on a daily basis and less effective communication of status. Stories and task breakdowns are&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/04/05/stories-in-targetprocess/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.targetprocess.com" target="_blank">TargetProcess</a> is fantastic tool for managing projects using <a href="http://www.targetprocess.com/agile_tour_20/iterative_development.asp" target="_blank">Agile</a> techniques. Gone are the days of 6 month Gantt charts, not truly knowing where you&#8217;re at on a daily basis and less effective communication of status.</p>
<p>Stories and task breakdowns are the essential elements in this tool, but TargetProcess is incredibly flexible in how <a href="http://www.targetprocess.com/usage_patterns/usage_patterns.asp" target="_blank">you can use it</a> &#8211; from usage by one man shops to enterprise level teams.</p>
<p>I use&nbsp;TargetProcess on my personal projects by <a href="http://www.amazon.com/User-Stories-Applied-Development-Addison-Wesley/dp/0321205685/ref=pd_bbs_sr_1/103-6647027-6345414?ie=UTF8&amp;s=books&amp;qid=1175741257&amp;sr=8-1" target="_blank">first creating stories</a>, the equivalent of requirements, and having conversations with the end user to hash out the details of the story and beginnings of a domain model:</p>
<p><a href="http://www.lostechies.com/blogs/nelson_montalvo/WindowsLiveWriter/TargetProcessReview_12E76/StoryPicInTargetProcess[1][4].jpg"><img src="http://www.lostechies.com/blogs/nelson_montalvo/WindowsLiveWriter/TargetProcessReview_12E76/StoryPicInTargetProcess[1]_thumb[2].jpg" style="border: 0px none" border="0" height="250" width="640"></a>&nbsp;</p>
<p>The beauty of stories is that they are simple to create and track.&nbsp;What is&nbsp;difficult is thinking about going back to utilizing other requirement methods because stories provide significant value by just&nbsp;being so easy to use.&nbsp;</p>
<p>With stories, you&nbsp;do not try to get it all right upfront, but you do try to write as many stories as possible. From here, you can break down stories further, come up with tasks, and/or roughly estimate the story.</p>
<p><a href="http://www.lostechies.com/blogs/nelson_montalvo/WindowsLiveWriter/TargetProcessReview_12E76/ListStoriesPicInTargetProcess[1][3].jpg"><img src="http://www.lostechies.com/blogs/nelson_montalvo/WindowsLiveWriter/TargetProcessReview_12E76/ListStoriesPicInTargetProcess[1]_thumb[1].jpg" style="border: 0px none" border="0" height="159" width="650"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/04/05/stories-in-targetprocess/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple NHibernate Example, Part 5: Repository Testing</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/04/04/simple-nhibernate-example-part-5-repository-testing/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/04/04/simple-nhibernate-example-part-5-repository-testing/#comments</comments>
		<pubDate>Wed, 04 Apr 2007 00:20:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/04/03/simple-nhibernate-example-part-5-repository-testing.aspx</guid>
		<description><![CDATA[Bill McCafferty has released his updates to his NHibernate Best Practices article. The article is fantastic. It is listed as an advanced topic, but if you follow every link and study the topics in his article, the article makes for&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/04/04/simple-nhibernate-example-part-5-repository-testing/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://devlicio.us/blogs/billy%5Fmccafferty/" target="_blank">Bill McCafferty</a> has released his updates to his NHibernate Best Practices article. <a href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp" target="_blank">The article</a> is fantastic. It is listed as an advanced topic, but if you follow every link and study the topics in his article, the article makes for as great&nbsp;a comprehensive introduction as any&#8230; Go <a href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp" target="_blank">read it</a> now. </p>
<p>In the last few posts, we explored very simple concepts from the point of view of DDD and&nbsp;BDD/TDD. In this post, I will put together a very simple testing harness to show how to roll together these concepts&nbsp;in the context of&nbsp;NHibernate.</p>
<p>Begin by reviewing the interface created in <a href="http://www.lostechies.com/blogs/nelson_montalvo/archive/2007/03/30/simple-nhibernate-example-part-3-initial-repository-implementation.aspx" title="Simple NHibernate Example, Part 3" target="_blank">Part 3</a>. In that example, the Repository interface is placed at the domain level (Foo.Domain.Repositories.DealerRepository &#8211; and no, <a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/04/26/143489.aspx" target="_blank">I don&#8217;t use the I in my interfaces</a>). Again, the idea is that a <i>Repository</i> is a domain level concept for managing the lifecycle of <i>Entities</i>. However, the implementation of&nbsp;the persistence mechanism&nbsp;is abstracted from the domain&#8217;s point of view. </p>
<p>We will create an implementation of the repository&nbsp;interface&nbsp;for&nbsp;NHibernate.&nbsp;I know there are <a href="http://www.ayende.com/Blog/archive/2007/03/20/Repositories-101.aspx" target="_blank">many ways to implement repositories</a>, but I&#8217;m keeping it easy for this example.</p>
<p>One other side note &#8211; this example is copying&nbsp;over the same&nbsp;specs/tests from Part 3 for my NHibernate tests, but you could imagine reusing them somehow by injecting in mock or NHibernate implementations of the interface. I really haven&#8217;t explored an idea like this, but if somebody has, let me know.</p>
<p>Everything needed for this repository example is in the sample zip file. Go <a href="http://groups.google.com/group/lostechies/web/Foo.Domain.zip" target="_blank">download the zip file</a>&nbsp;now. You will want to focus on the Foo.Data.NHibernate and Tests.Foo.Data.NHibernate projects to see how it all works. Post your questions here.</p>
<p>&nbsp;</p>
<p><u>Supporting Items</u></p>
<p>The&nbsp;database schema is included in the solution if you would like to run that to build the FooDomain database and Dealer table.</p>
<p>This Dealer domain object to table mapping file is located in the&nbsp;Foo.Data.NHibernate project under the DomainMappings folder.&nbsp;The file also&nbsp;contains a component mapping for a phone number&nbsp;Va<i>lue</i> object (the fax number and contact phone number). Let&#8217;s just say that it is important for us to store the phone number broken down into its constituent parts (area code, exchange and SLID) for this example. </p>
<p>In the mapping file,&nbsp;I&nbsp;use of the&nbsp;<span style="color: red">access</span><span style="color: blue">=</span><span style="color: black">&#8220;</span><span style="color: blue">nosetter.camelcase</span><span style="color: black">&#8220;</span> access attribute. This tells NHibernate to set the field values directly rather than through the properties (since no setter is available, as we would like to keep IDs and value object references immutable,&nbsp;as you will see in a moment).</p>
<p>I use a customized version of <a href="http://www.ndbunit.org/" target="_blank">nDbUnit</a> to allow for rollbacks of transactions rather than explicit delete. My customized version is included in the project file (which is what makes&nbsp;the&nbsp;zip file&nbsp;pretty big) and could use some cleanup if you want to take that on. <img src='http://lostechies.com/nelsonmontalvo/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  The project, Tests.Foo.Data.NHibernate contains the xsd used by nDbUnit and you can review the tests to see how it is read into. </p>
<p>My customizations also reset the identity value on the Dealer table<br />
after a DeleteAll is called. </p>
<p>I did not explicitly call flush after the NHibernate save() calls, which<br />
would immediately write the data to the database. The flush is done automatically since<br />
NHibernate has to go to the database to pull the identity value and set<br />
it on the domain object. This is an important point since NDbUnit needs<br />
to have some data to read from the database. <img src='http://lostechies.com/nelsonmontalvo/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>Finally, the tests show how to extract the<br />
NHibernate transaction and pass it to NDbUnit in the GetTransaction()<br />
method.<br />&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/04/04/simple-nhibernate-example-part-5-repository-testing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Bill McCafferty&#8217;s NHibernate Best Practices</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/03/30/bill-mccafferty-s-nhibernate-best-practices/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/03/30/bill-mccafferty-s-nhibernate-best-practices/#comments</comments>
		<pubDate>Fri, 30 Mar 2007 16:46:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/03/30/bill-mccafferty-s-nhibernate-best-practices.aspx</guid>
		<description><![CDATA[Bill McCafferty is coming out with an update to his article NHibernate Best Practices. In it, he corrects the problem with HttpContext vs CallContext.]]></description>
			<content:encoded><![CDATA[<p><A href="http://devlicio.us/blogs/billy_mccafferty/">Bill McCafferty</A> is coming out with an update to his article <A href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp?df=100&amp;forumid=278860&amp;noise=5&amp;exp=0&amp;mpp=50&amp;select=1966264#xx1966264xx">NHibernate Best Practices</A>. In it, he <A href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp?df=100&amp;forumid=278860&amp;noise=5&amp;amp;exp=0&amp;mpp=50&amp;select=1963724#xx1963724xx">corrects the problem</A> with HttpContext vs CallContext.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/03/30/bill-mccafferty-s-nhibernate-best-practices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple NHibernate Example, Part 4 : Session Management</title>
		<link>http://lostechies.com/nelsonmontalvo/2007/03/30/simple-nhibernate-example-part-4-session-management/</link>
		<comments>http://lostechies.com/nelsonmontalvo/2007/03/30/simple-nhibernate-example-part-4-session-management/#comments</comments>
		<pubDate>Fri, 30 Mar 2007 16:37:00 +0000</pubDate>
		<dc:creator>Nelson Montalvo</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">/blogs/nelson_montalvo/archive/2007/03/30/simple-nhibernate-example-part-4-session-management.aspx</guid>
		<description><![CDATA[Before continuing with the implementation, let&#8217;s talk briefly about NHibernate session management. This will be a short discussion, as you can get more details from chapter 8 of the book, Hibernate In Action. Also, please review the article, NHibernate Best&#160;&#8230; <a href="http://lostechies.com/nelsonmontalvo/2007/03/30/simple-nhibernate-example-part-4-session-management/">Continue&#160;reading&#160;<span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Before continuing with the implementation, let&#8217;s talk briefly about NHibernate session management. This will be a short discussion, as you can get more details from chapter 8 of the book, <a href="http://www.amazon.com/Hibernate-Action-Christian-Bauer/dp/193239415X/ref=pd_bbs_sr_1/104-7167080-3547947?ie=UTF8&amp;s=books&amp;qid=1174402169&amp;sr=8-1" target="_blank">Hibernate In Action</a>. Also, please review the article, <a href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp" target="_blank">NHibernate Best Practices</a>, for further explanation of the Session In View.</p>
<p>One of the potential problems with using NHibernate is the intermingling of managing the NHibernate sessions and transactions with the basic interactions between the domain and NHibernate. Ideally, we&#8217;d like the client using NHibernate to manage transactions outside of the context of the business workflow, committing or rolling back entire transactions depending on the results of the workflow (this is the unit of work pattern).</p>
<p>We&#8217;d like to be able to manage a single session across a single persistence context (or transaction context). We&#8217;ll utilize the thread safe CallContext object in an NHibernate SessionManager class. An upcoming post will show how this session manager works. In the meantime, here is the code (borrowed from <a href="http://www.codeproject.com/aspnet/NHibernateBestPractices.asp" target="_blank">NHibernate Best Practices</a>):</p>
<div style="background: white none repeat scroll 0% 50%;font-size: 10pt;color: black">
<p style="margin: 0px"><span style="color: blue">using</span> System.Configuration;</p>
<p style="margin: 0px"><span style="color: blue">using</span> System.Runtime.Remoting.Messaging;</p>
<p style="margin: 0px"><span style="color: blue">using</span> System.Web;</p>
<p style="margin: 0px"><span style="color: blue">using</span> NHibernate;</p>
<p style="margin: 0px"><span style="color: blue">using</span> NHibernate.Cache;</p>
<p style="margin: 0px"><span style="color: blue">using</span> <span>Configuration</span>=NHibernate.Cfg.<span>Configuration</span>;</p>
<p style="margin: 0px"><span style="color: blue">using</span> NHibernateConfiguration = NHibernate.Cfg;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">namespace</span> DealerMatrix.Data.NHibernate.Session</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> Handles creation and management of sessions and transactions.&nbsp; It is a singleton because </span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> building the initial session factory is very expensive. Inspiration for this class came </span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> from Chapter 8 of Hibernate in Action by Bauer and King.&nbsp; Although it is a sealed singleton</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> you can use TypeMock (http://www.typemock.com) for more flexible testing.</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">sealed</span> <span style="color: blue">class</span> <span>NHibernateSessionManager</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span>ISessionFactory</span> sessionFactory;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #region</span> Thread-safe, lazy Singleton</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> This is a thread-safe, lazy singleton.&nbsp; See http://www.yoda.arachsys.com/csharp/singleton.html</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> for more details about its implementation.</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">static</span> <span>NHibernateSessionManager</span> Instance</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">get</span> { <span style="color: blue">return</span> <span>Nested</span>.nHibernateNHibernateSessionManager; }</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: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> Initializes the NHibernate session factory upon instantiation.</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> NHibernateSessionManager()</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; InitSessionFactory();</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: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> Assists with ensuring thread-safe, lazy singleton</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">class</span> <span>Nested</span></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">static</span> Nested()</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; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">internal</span> <span style="color: blue">static</span> <span style="color: blue">readonly</span> <span>NHibernateSessionManager</span> nHibernateNHibernateSessionManager = <span style="color: blue">new</span> <span>NHibernateSessionManager</span>();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px"><span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #endregion</span></p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">void</span> InitSessionFactory()</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>Configuration</span> cfg = <span style="color: blue">new</span> <span>Configuration</span>();</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">// The following makes sure the the web.config contains a declaration for the HBM_ASSEMBLY appSetting</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (<span>ConfigurationManager</span>.AppSettings[<span>"HBM_ASSEMBLY"</span>] == <span style="color: blue">null</span> ||</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span>ConfigurationManager</span>.AppSettings[<span>"HBM_ASSEMBLY"</span>] == <span>&#8220;&#8221;</span>)</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">throw</span> <span style="color: blue">new</span> <span>ConfigurationErrorsException</span>(<span>&#8220;NHibernateManager.InitSessionFactory: &#8220;HBM_ASSEMBLY&#8221; must be &#8220;</span> +</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; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span>&#8220;provided as an appSetting within your config file. &#8220;HBM_ASSEMBLY&#8221; informs NHibernate which assembly &#8220;</span> +</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; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span>&#8220;contains the HBM files. It is assumed that the HBM files are embedded resources. An example config &#8220;</span> +</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; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span>&#8220;declaration is &lt;add key=&#8221;HBM_ASSEMBLY&#8221; value=&#8221;MyProject.Core&#8221; /&gt;&#8221;</span>);</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cfg.AddAssembly(<span>ConfigurationManager</span>.AppSettings[<span>"HBM_ASSEMBLY"</span>]);</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sessionFactory = cfg.BuildSessionFactory();</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: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> Allows you to register an interceptor on a new session.&nbsp; This may not be called if there is already</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> an open session attached to the HttpContext.&nbsp; If you have an interceptor to be used, modify</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> the HttpModule to call this before calling BeginTransaction().</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">void</span> RegisterInterceptor(<span>IInterceptor</span> interceptor)</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>ISession</span> session = threadSession;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (session != <span style="color: blue">null</span> &amp;&amp; session.IsOpen)</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">throw</span> <span style="color: blue">new</span> <span>CacheException</span>(<span>&#8220;You cannot register an interceptor once a session has already been opened&#8221;</span>);</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GetSession(interceptor);</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>ISession</span> GetSession()</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> GetSession(<span style="color: blue">null</span>);</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: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> Gets a session with or without an interceptor.&nbsp; This method is not called directly; instead,</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> it gets invoked from other public methods.</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span>ISession</span> GetSession(<span>IInterceptor</span> interceptor)</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>ISession</span> session = threadSession;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (session == <span style="color: blue">null</span>)</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> (interceptor != <span style="color: blue">null</span>)</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; session = sessionFactory.OpenSession(interceptor);</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; <span style="color: blue">else</span></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; session = sessionFactory.OpenSession();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; threadSession = session;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> session;</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">void</span> CloseSession()</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>ISession</span> session = threadSession;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; threadSession = <span style="color: blue">null</span>;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (session != <span style="color: blue">null</span> &amp;&amp; session.IsOpen)</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; session.Close();</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; }</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">void</span> BeginTransaction()</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>ITransaction</span> transaction = threadTransaction;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (transaction == <span style="color: blue">null</span>)</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; transaction = GetSession().BeginTransaction();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; threadTransaction = transaction;</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; }</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">void</span> CommitTransaction()</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>ITransaction</span> transaction = threadTransaction;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">try</span></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> (transaction != <span style="color: blue">null</span> &amp;&amp; !transaction.WasCommitted &amp;&amp; !transaction.WasRolledBack)</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; transaction.Commit();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; threadTransaction = <span style="color: blue">null</span>;</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">catch</span> (<span>HibernateException</span> ex)</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; RollbackTransaction();</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">throw</span> ex;</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; }</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">void</span> RollbackTransaction()</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>ITransaction</span> transaction = threadTransaction;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">try</span></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; threadTransaction = <span style="color: blue">null</span>;</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (transaction != <span style="color: blue">null</span> &amp;&amp; !transaction.WasCommitted &amp;&amp; !transaction.WasRolledBack)</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; transaction.Rollback();</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">catch</span> (<span>HibernateException</span> ex)</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">throw</span> ex;</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">finally</span></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; CloseSession();</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; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> If within a web context, this uses </span><span style="color: gray">&lt;see cref=&#8221;HttpContext&#8221; /&gt;</span><span style="color: green"> instead of the WinForms </span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> specific </span><span style="color: gray">&lt;see cref=&#8221;CallContext&#8221; /&gt;</span><span style="color: green">.&nbsp; Discussion concerning this found at </span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> http://forum.springframework.net/showthread.php?t=572.</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span>ITransaction</span> ThreadTransaction</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">get</span></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> (IsInWebContext())</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; <span style="color: blue">return</span> (<span>ITransaction</span>) <span>HttpContext</span>.Current.Items[TRANSACTION_KEY];</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; <span style="color: blue">else</span></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; <span style="color: blue">return</span> (<span>ITransaction</span>) <span>CallContext</span>.GetData(TRANSACTION_KEY);</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">set</span></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> (IsInWebContext())</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; <span>HttpContext</span>.Current.Items[TRANSACTION_KEY] = <span style="color: blue">value</span>;</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; <span style="color: blue">else</span></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; <span>CallContext</span>.SetData(TRANSACTION_KEY, <span style="color: blue">value</span>);</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; }</p>
<p style="margin: 0px">&nbsp;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> If within a web context, this uses </span><span style="color: gray">&lt;see cref=&#8221;HttpContext&#8221; /&gt;</span><span style="color: green"> instead of the WinForms </span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> specific </span><span style="color: gray">&lt;see cref=&#8221;CallContext&#8221; /&gt;</span><span style="color: green">.&nbsp; Discussion concerning this found at </span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> http://forum.springframework.net/showthread.php?t=572.</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray">///</span><span style="color: green"> </span><span style="color: gray">&lt;/summary&gt;</span></p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span>ISession</span> ThreadSession</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">get</span></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> (IsInWebContext())</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; <span style="color: blue">return</span> (<span>ISession</span>) <span>HttpContext</span>.Current.Items[SESSION_KEY];</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; <span style="color: blue">else</span></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; <span style="color: blue">return</span> (<span>ISession</span>) <span>CallContext</span>.GetData(SESSION_KEY);</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">set</span></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> (IsInWebContext())</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; <span>HttpContext</span>.Current.Items[SESSION_KEY] = <span style="color: blue">value</span>;</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; <span style="color: blue">else</span></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; <span>CallContext</span>.SetData(SESSION_KEY, <span style="color: blue">value</span>);</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; }</p>
<p style="margin: 0px">&nbsp;</p>
<p 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">bool</span> IsInWebContext()</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> <span>HttpContext</span>.Current != <span style="color: blue">null</span>;</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">private</span> <span style="color: blue">const</span> <span style="color: blue">string</span> TRANSACTION_KEY = <span>&#8220;CONTEXT_TRANSACTION&#8221;</span>;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">string</span> SESSION_KEY = <span>&#8220;CONTEXT_SESSION&#8221;</span>;</p>
<p style="margin: 0px">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px">}</p>
</div>
<ul>
<li>Have a look at the last two properties to see how the CallContext is used. These maintain the transaction and session across a single request thread.</li>
</ul>
<ul>
<li>InitSessionFactory() requires that HBM_ASSEMBLY be defined in the config file.</li>
</ul>
<ul>
<li>Finally, be sure to call CommitTransaction(), RollbackTransaction() and/or CloseSession() at the end of the request to clear out the CallContext at the end of the thread request.</li>
</ul>
<p>Again, I will show you how to put all these pieces together in an upcoming post.</p>
]]></content:encoded>
			<wfw:commentRss>http://lostechies.com/nelsonmontalvo/2007/03/30/simple-nhibernate-example-part-4-session-management/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

