

<?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>Plastic/Blog &#187; NHibernate</title>
	<atom:link href="http://blog.codiceplastico.com/index.php/tag/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codiceplastico.com</link>
	<description>Code Elegance</description>
	<lastBuildDate>Sat, 28 Aug 2010 10:27:56 +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>Windsor Container and NHibernate sessions</title>
		<link>http://blog.codiceplastico.com/index.php/2010/05/13/windsor-container-and-nhibernate-sessions/</link>
		<comments>http://blog.codiceplastico.com/index.php/2010/05/13/windsor-container-and-nhibernate-sessions/#comments</comments>
		<pubDate>Thu, 13 May 2010 07:28:46 +0000</pubDate>
		<dc:creator>Emanuele DelBono</dc:creator>
				<category><![CDATA[Emanuele DelBono]]></category>
		<category><![CDATA[CastleWindsor]]></category>
		<category><![CDATA[Container]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blog.codiceplastico.com/?p=299</guid>
		<description><![CDATA[When developing MVVM WPF applications that use NHibernate as ORM one of the problems that you encounter is how to manage the sessions and keep the application fully testable. I like using a Session per “screen” that in the MVVM world is equivalent to use “one Session per ViewModel”. I also like to separate the [...]]]></description>
			<content:encoded><![CDATA[<p>When developing MVVM WPF applications that use NHibernate as ORM one of the problems that you encounter is how to manage the sessions and keep the application fully testable.</p>
<p>I like using a Session per “screen” that in the MVVM world is equivalent to use “one Session per ViewModel”.</p>
<p>I also like to separate the presentation logic from the rest so I also create a Model for each ViewModel (even if this is not strictly necessary)</p>
<p>Finally the Model use one or more repository to access the database.</p>
<p>So, the NHibernate session is tied to the life of the Model and all the repositories that belongs the same model should share the same session so I can build transaction between repositories.</p>
<p>In addition to this I would like to inject the interfaces of the dependencies: the ViewModel will get the IModel, and the Model will get the IRepository1, IRepository2.</p>
<p>This picture should clarify the complete schema:</p>
<p><a href="http://blog.codiceplastico.com/wp-content/uploads/2010/05/NHSessions.png"><img class="aligncenter size-medium wp-image-298" title="NHSessions" src="http://blog.codiceplastico.com/wp-content/uploads/2010/05/NHSessions-284x300.png" alt="NHSessions" width="284" height="300" /></a></p>
<p>To create this scenario I choose <a href="http://www.castleproject.org/container/index.html" target="_blank">Castle Windsor</a> as a container and with the help of <a href="http://blog.schuager.com" target="_blank">German Shuager</a>, I defined this particular registration method:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> RegisterSharedWithFactory&lt;TService&gt;(Function&lt;TService&gt; factory)
{
  _kernel.AddFacility&lt;FactorySupportFacility&gt;()
    .Register(Component.For&lt;TService&gt;()
      .UsingFactoryMethod(factory)
      .LifeStyle.Custom&lt;ResolutionContextLifestyleManager&gt;());
}</pre>
<p><!--</p>
<p>.csharpcode, .csharpcode pre<br />
{<br />
font-size: small;<br />
color: black;<br />
font-family: consolas, "Courier New", courier, monospace;<br />
background-color: #ffffff;<br />
/*white-space: pre;*/<br />
}<br />
.csharpcode pre { margin: 0em; }<br />
.csharpcode .rem { color: #008000; }<br />
.csharpcode .kwrd { color: #0000ff; }<br />
.csharpcode .str { color: #006080; }<br />
.csharpcode .op { color: #0000c0; }<br />
.csharpcode .preproc { color: #cc6633; }<br />
.csharpcode .asp { background-color: #ffff00; }<br />
.csharpcode .html { color: #800000; }<br />
.csharpcode .attr { color: #ff0000; }<br />
.csharpcode .alt<br />
{<br />
background-color: #f4f4f4;<br />
width: 100%;<br />
margin: 0em;<br />
}<br />
.csharpcode .lnum { color: #606060; } -->This method registers a component using a Factory method to create the instance since we cannot create an instance of the ISession but we must use a SessionFactory. Then sets the the lifestyle to a custom ResolutionContextLifestyleManager to be sure that  in the same context the dependencies are created only once.</p>
<p>The implementation of ResolutionContextLifestyleManager is written by German and can be found <a href="http://blog.schuager.com/2008/11/custom-windsor-lifestyle.html" target="_blank">here</a>.</p>
<p>To understand what it does, consider this test:</p>
<pre class="csharpcode">[Fact]
<span class="kwrd">public</span> <span class="kwrd">void</span> Two_Repo_In_The_Same_Model_Should_Have_Same_Session()
{
  Container.Initialize();
  Container.RegisterSharedWithFactory&lt;ISession&gt;(() =&gt; SessionHelper.OpenSession());
  Container.Register&lt;IRepo1, Repo1&gt;();
  Container.Register&lt;IRepo2, Repo2&gt;();
  Container.Register&lt;IModel1, Model1&gt;();

  IModel1 model1 = Container.Resolve&lt;IModel1&gt;();

  Assert.Equal(model1.Repo1.Session, model1.Repo2.Session); // Repo1 and Repo2 shares the same session
}</pre>
<p>The model uses two repositories that uses the same instance of ISession exactly like in the picture above.</p>
<p>This configuration is very useful to manage the session in the right manner with NHibernate and Castle Windsor container and assure us the testability of all the components.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codiceplastico.com/index.php/2010/05/13/windsor-container-and-nhibernate-sessions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Searching with NHibernate</title>
		<link>http://blog.codiceplastico.com/index.php/2007/09/09/17/</link>
		<comments>http://blog.codiceplastico.com/index.php/2007/09/09/17/#comments</comments>
		<pubDate>Sun, 09 Sep 2007 09:19:23 +0000</pubDate>
		<dc:creator>Emanuele DelBono</dc:creator>
				<category><![CDATA[Emanuele DelBono]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blogema.wordpress.com/2007/09/09/17/</guid>
		<description><![CDATA[In these days, while working with NHibernate, I was building a method that load some data using an ICriteria and the Visual Studio  2008 Intellisense had show me a class named Example. After a bit research throught the source code I understand it&#8217;s usage: this class enables your application to use a &#8220;partial-populated object&#8221; to build the where [...]]]></description>
			<content:encoded><![CDATA[<p>In these days, while working with NHibernate, I was building a method that load some data using an ICriteria and the Visual Studio  2008 Intellisense had show me a class named Example.</p>
<p>After a bit research throught the source code I understand it&#8217;s usage: this class enables your application to use a &#8220;partial-populated object&#8221; to build the where clause. Let&#8217;s see an example</p>
<p>Suppose that you need to load all Orders in state accepted at date 09/04/2007. You can write this code:<br />
Order order = new Order();<br />
order.Stato = OrderState.Accepted;<br />
order.Date = new DateTime(2007, 04, 09);<br />
ICriteria criteria = session.CreateCriteria(typeof(Order));<br />
criteria.Add(Example.Create(order));<br />
IList result = criteria.List();</p>
<p>What Nhibernate do is to parse all order&#8217;s property that have a non-default value and use that values to build a filter.<br />
These method seems very useful to implement search functionality in your applcation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codiceplastico.com/index.php/2007/09/09/17/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
