<?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>Code Hangover &#187; NHibernate</title>
	<atom:link href="http://blog.codehangover.com/tag/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codehangover.com</link>
	<description>Go ahead, have another</description>
	<lastBuildDate>Tue, 22 Mar 2011 15:49:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NHibernate 2 Beginner&#8217;s Guide &#8211; Book Review</title>
		<link>http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/</link>
		<comments>http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 04:31:22 +0000</pubDate>
		<dc:creator>Welzie</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[book review]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blog.codehangover.com/?p=542</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/";</script>Book on Amazon
Finally someone has written an in depth beginners book for nhibernate.  Nhibernate 2.0 Beginners Guide written by Aaron Cure is just that and more.  Wow do I sound like a car salesman.  I was a little disappointed when I read Nhibernate In Action last year, because it was more of a reference than [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/";</script><p><a style="padding: 0 10px 10px 10px; float: right" href="http://www.amazon.com/gp/product/1847198902?ie=UTF8&amp;tag=1410softwarec-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1847198902"><img src="http://img101.imageshack.us/img101/9956/nhibernate2guide.jpg" border="0" alt="" /><br />
<span style="font-size: 8pt;">Book on Amazon</span></a></p>
<p>Finally someone has written an in depth beginners book for nhibernate.  Nhibernate 2.0 Beginners Guide written by Aaron Cure is just that and more.  Wow do I sound like a car salesman.  I was a little disappointed when I read <a id="rkhb" title="Nhiberate In Action" href="../nhibernate-in-action-book-review/" target="_blank">Nhibernate In Action</a> last year, because it was more of a reference than a tutorial.  This book is definitely what the title states, which is a beginners guide.  The book contains step by step examples of how to find, setup, and use nhibernate.  I highly recommend this book to anyone wanting to learn nhibernate.  By the way I think all .netters should do just that.</p>
<p><strong>Pros</strong></p>
<ul>
<li>love the beginning paragraphs
<ul>
<li>In simple terms, NHibernate does all the database work, and we reap all the benefits! Instead of writing reams of SQL statements or creating stored procedures that &#8220;live&#8221; in a different place than our code, we can have all of our data access logic contained within our application.<br />
With a few simple &#8220;tricks&#8221; that we&#8217;ll discuss in Chapter 4, Data Cartography, not only will our queries be effective, but they will also be validated by the compiler. Therefore, if our underlying table structure changes, the compiler will alert us that we need to change our queries!</li>
</ul>
</li>
<li>starts slow, which is perfect for a &#8220;beginners&#8221; book.  lots of hand holding and explanations of basics.  this is truly meant for somehow with no experience with <a id="l6an" title="ORMs" href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank"><span>ORMs</span></a>.</li>
<li>clearly shows you how to use log4net!  great bonus in a nhibernate book.</li>
<li>briefly mentions all the major players in the nhibernate world, which is great for demonstrating options
<ul>
<li>Examples: fluent nhibernate, nhibernate.burrow, castle, spring <span>ioc</span>, etc.</li>
</ul>
</li>
</ul>
<p><strong>Cons</strong></p>
<ul>
<li>book uses basic <span>aspx</span> pages with custom controls in the examples.  I firmly suggest ASP.Net MVC over <span>webforms</span>.  But it may be too much to learn at one time if you aren&#8217;t familiar with either <span>nhib</span> or MVC.  So learn one first and then learn the other.  You won&#8217;t regret it.</li>
<li>most examples are in c# and vb.net.  that&#8217;s good or bad depending on your opinion of vb.net.</li>
<li>No <a id="p5oq" title="HQL" href="http://nhforge.org/doc/nh/en/index.html#queryhql" target="_blank"><span>HQL</span></a> examples.  All query examples use the Criteria class.  This isn&#8217;t totally bad especially for beginners, but <span>HQL</span> should have at least been mentioned.</li>
</ul>
<p><strong>Chapter Notes</strong></p>
<ul>
<li>chapter 1
<ul>
<li>clearly denotes which version of <span>nhib</span> the book uses and where to get it
<ul>
<li>Even explains the <span>nhib</span> release verbiage of &#8220;generally available&#8221;</li>
</ul>
</li>
<li>shows basic examples of mapping file and how it relates to <span>POCOs</span>.  Good stuff for a <a id="b6sw" title="ORM" href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORM</a> newbie.</li>
</ul>
</li>
<li>chapter 2
<ul>
<li>good advice for designing your database in logical way that avoids data duplication.</li>
<li>clear steps to download and setup a database using sql server express
<ul>
<li>Does mention that <span>nhib</span> works in almost any database, however all the book examples use sql server express</li>
</ul>
</li>
<li>nice explanation of how tables relate to your classes</li>
<li>great introduction to <span>OTM</span>, <span>MTO</span>, <span>MTM</span>, and <span>OTO</span> relationships</li>
<li>good summary of left joins and how joins matter in queries for the different relationship types</li>
</ul>
</li>
<li>chapter 3
<ul>
<li>starts creating a simple class library application.  clear steps and good examples.</li>
<li>even addresses the somewhat hated <span>nullable</span> types and how to handle database columns that need to be mapped to types that can be null.</li>
</ul>
</li>
<li>chapter 4
<ul>
<li>nice explanation of what mapping means in terms of <a id="t.5g" title="ORM" href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORM</a> software and how it functions as the glue that binds your objects to the database.</li>
<li>summary and examples of the most common used mapping types</li>
<li>gives examples of two mapping styles
<ul>
<li>xml
<ul>
<li>lists the two main complaints which are.. 1) too much xml 2) xml files are not compiled, so you don&#8217;t find bugs until run time.</li>
<li>provides great tip of adding the hibernate <span>XSD</span> to  your project so that visual studio will provide code completion for you and validate your mapping files.</li>
<li>important info about how to make sure the xml mapping files are compiled into your <span>dll</span></li>
</ul>
</li>
<li>fluent nhibernate
<ul>
<li>short example given that shows how using the &#8220;side&#8221; project <a href="http://fluentnhibernate.org/" target="_blank">http://fluentnhibernate.org/</a> can work without the xml mapping files.</li>
<li>pro is that mappings have to be compiled, so bugs/typos are found earlier</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>chapter 5
<ul>
<li>creates console application to test the <span>nhib</span> code</li>
<li>shows step by step how to add references to the <span>nhib</span> <span>dlls</span> that you need to download</li>
<li>good definition of what a <span>nhib</span> session is and how it relates to an actual database session</li>
</ul>
</li>
<li>chapter 6
<ul>
<li>why you should and how to use log4net with your <span>nhib</span> project</li>
<li>excellent and thorough tutorial for using log4net</li>
</ul>
</li>
<li>chapter 7
<ul>
<li><span>nhib</span> config details</li>
<li>mentions how easy it is to change databases by simply changing one config line</li>
<li>shows how to config <span>nhib</span> in an app.config or web.config</li>
</ul>
</li>
<li>chapter 8
<ul>
<li>provides example of Singleton DOA pattern</li>
<li>shows example of using a structure that holds all the column/property names for a class/entity.  this is done so that the column names in the structure can be used in criteria queries.  all this effort is done to avoid run time exceptions.  seems like a waste of time to me because you if you don&#8217;t update your structure every time your db is updated then you will still get <span>runtime</span> errors.</li>
<li>query examples using Criteria (at this point all examples have been using Criteria, none in <span>HQL</span>)</li>
</ul>
</li>
<li>chapter 9
<ul>
<li>in depth examples showing how to create custom controls to display data retrieved using nhibernate</li>
</ul>
</li>
<li>chapter 10
<ul>
<li>shows how to implement the login controls with nhibernate</li>
</ul>
</li>
<li>chapter 11
<ul>
<li>covers 11 code generation tools used to limit manual boiler plate coding.  I actually hadn&#8217;t heard of many of the ones listed.  Great bonus chapter.</li>
<li><span>nhib</span>-gen, <span>mygeneration</span>, <span>NGen</span> NHibernate Code Generator, and T4 <span>hbm</span>2net seem promising</li>
<li>these tools create everything from <span>POCOs</span> to <span>DAOs</span> to services.</li>
</ul>
</li>
</ul>
<p><img class=" zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl zzvvvhvfecitsllxgenl" style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=1410softwarec-20&amp;l=as2&amp;o=1&amp;a=1847198902" border="0" alt="" width="1" height="1" /><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://blog.codehangover.com/nhibernate-in-action-book-review/" title="NHibernate in Action &#8211; Book Review">NHibernate in Action &#8211; Book Review</a></li>
<li><a href="http://blog.codehangover.com/outliers-a-non-technical-book-every-freelancerdeveloper-should-read/" title="Outliers &#8211; A non technical book every freelancer/developer should read">Outliers &#8211; A non technical book every freelancer/developer should read</a></li>
<li><a href="http://blog.codehangover.com/configuring-nhibernate-in-a-multiple-project-layout/" title="Configuring NHibernate in a Multiple Project Layout">Configuring NHibernate in a Multiple Project Layout</a></li>
</ul>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.codehangover.com%2Fbook-review-nhibernate-2-beginners-guide%2F&amp;linkname=NHibernate%202%20Beginner%26%238217%3Bs%20Guide%20%26%238211%3B%20Book%20Review"><img src="http://blog.codehangover.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate in Action &#8211; Book Review</title>
		<link>http://blog.codehangover.com/nhibernate-in-action-book-review/</link>
		<comments>http://blog.codehangover.com/nhibernate-in-action-book-review/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 16:35:48 +0000</pubDate>
		<dc:creator>Welzie</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[book review]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blog.codehangover.com/?p=435</guid>
		<description><![CDATA[Review of the book "NHibernate In Action".  Pros &#038; cons along with suggestions for who this book would benefit.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/nhibernate-in-action-book-review/";</script><p><a style="float: right" href="http://www.amazon.com/gp/product/1932394923?ie=UTF8&amp;tag=1410softwarec-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1932394923"><img src="http://img94.imageshack.us/img94/9959/nhibernateinaction.jpg" border="0" alt="" /><span style="font-size: small;"><br />
<span style="font-size: 8pt;">Book on Amazon</span></span></a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=1410softwarec-20&amp;l=as2&amp;o=1&amp;a=1932394923" border="0" alt="" width="1" height="1" /><br />
All summer I meant to read the book &#8220;NHibernate In Action&#8221;.  I finally got around to it, better late than never I guess. In my opinion this book is aimed at higher level programmers such as lead developers or architects.  The book does not contain clear step by step examples of how to get NHibernate running.  However it does provide in depth conceptual reasoning why anyone would want to use NHibernate along with reference style examples.  If you are trying to learn NHibernate  you are better off reading a few online tutorials first and after that read this book so you can understand exactly what NHibernate is doing and how it does it.  Below are some book pros &amp; cons, NHibernate links, and chapter summaries.</p>
<h4>Pros</h4>
<ul>
<li> Examples are all in c#</li>
<li> Shows config examples for NHibernate 1.2.1 and 2</li>
<li> Promotes proper design patters often references popular pattern books</li>
<li> Makes note of minor current bugs or missing features that may cause issues or confusion</li>
<li> Provides suggestions for when to use something other than the default NHibernate settings</li>
<li> Mentions how NHibernate can use the features of multiple RDMS, not just SQL Server</li>
</ul>
<h4>Cons</h4>
<ul>
<li> Download and install instructions were vague
<ul>
<li> I downloaded <a href="http://sourceforge.net/projects/nhibernate/files/">NHibernate 2.1.0 QA from SourceForge</a> after finally finding the link on nhforge.org.  From there I followed the install notes in the downloaded zip file.</li>
</ul>
</li>
<li> Examples do not use Generics</li>
<li> Doesn&#8217;t provide a link or even a suggestion to where you can download the NHibernate.Mapping.Attributes library which lets you use features similar to the annotations in Hibernate 3
<ul>
<li>I found them here: http://sourceforge.net/projects/nhcontrib/files/NHibernate.Mapping.Attributes/</li>
</ul>
</li>
<li> Shows attribute and xml mapping examples randomly.  In my opinion it would be better if one or the other was used consistently.</li>
<li> Jumps from definitions to examples. No clear step by step examples
<ul>
<li>I would say this book is more of a reference than tutorial</li>
</ul>
</li>
</ul>
<h4>Helpful NHibernate Links</h4>
<ul>
<li><a href="http://nhforge.org">NHibernate community site</a>
<ul>
<li>Great <a href="http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx">short tutorial</a></li>
<li><a href="http://nhforge.org/doc/nh/en/index.html">NHibernate docs</a></li>
</ul>
</li>
<li><a href="http://groups.google.com/group/nhusers/">NHibernate Google Group</a></li>
<li><a href="http://stackoverflow.com/questions/tagged/nhibernate">Stackoverflow NHibernate</a> questions and answers</li>
<li>How to use <a href="http://iseesharp.blogspot.com/2006/09/connecting-mysql-and-c.html">MySQL with .NET</a></li>
<li><a href="https://www.hibernate.org/361.html">Databases supported</a> by NHibernate</li>
<li><a href="http://swik.net/tag4sree/Hibernate+-+Objects/Common+nHibernate+exceptions+and+a+question/b4013">Common exceptions </a></li>
</ul>
<h4>Chapter 1</h4>
<ul>
<li> Basic definitions of persistence, sql(sql not sql server) databases, and layered development</li>
<li>Doesn&#8217;t mention the testability benefits of layered development</li>
<li> Compares persistence layer choices available with .Net</li>
<li> Makes the case that LINQ and Entity framework will not replace NHibernate</li>
<li> Definition of ORM</li>
<li> Addresses the performance concerns of using ORMs</li>
<li> Author tries to convince people to use ORMs, this part needs a little salesman ship but I guess if someone is open minded enough to read this book then they don&#8217;t need much of a sales pitch.</li>
</ul>
<h4>Chapter 2</h4>
<ul>
<li> Some basoc instructions for how to download and install the NHibernate dll
<ul>
<li>con: Could use a little details in this section about how to use the versions that don&#8217;t have msi installers yet like the 2.1.0.GA release I downloaded.</li>
</ul>
</li>
<li> Shows very simple examples of saving and loading an entity</li>
<li> Then shows you a basic xml mapping document
<ul>
<li>o Shows a mapping document for versions 1.2 and 2.</li>
</ul>
</li>
<li> Quick glance at the NHibernate api and in depth notes on important interfaces</li>
<li> Very short section on logging with log4net</li>
</ul>
<h4>Chapter 3</h4>
<ul>
<li> Good section on the benefits of having a transparent persistence layer</li>
<li> Discusses the anti xml config file movement.
<ul>
<li>However doesn&#8217;t mention the fact that poorly designed and overly complicated schemas are one reason for the back lash.  Also never mentions CoC.</li>
</ul>
</li>
<li> Discusses using attributes as meta data
<ul>
<li>IMHO way more time should have been spent telling the users how to setup/install the mapping dlls needed to use this feature</li>
<li>Does show many examples of how to use the attributes and what the xml version of the meta data would look like</li>
</ul>
</li>
<li> This chapter provides information about the hibernate mapping elements and options
<ul>
<li>It is a mix between a reference and tutorial.</li>
</ul>
</li>
<li> Good section on object identity vs equality</li>
<li> con:  Gives an explanation of what primary keys are.  Surely anyone reading this book knows already knows what primary keys are.</li>
<li> Good short explanation of the different types of primary key creation that NHibernate supports</li>
<li> Long discussion on database and object model design
<ul>
<li>table per concrete class</li>
<li>table per sub class</li>
<li>table per class hierarchy</li>
</ul>
</li>
</ul>
<h4>Chapter 4</h4>
<ul>
<li> Explains object identity scope.  probably a little high level for low level developers</li>
<li> Explains different options for Equals and HashCode implementations</li>
<li> Automatic dirty checking
<ul>
<li>Changing an object in an ISession and committing will result in those changes being automatically reflected in the database</li>
</ul>
</li>
<li> Clearly denotes the different cascading persistence options</li>
<li> Details the different object retrieval options
<ul>
<li>Retrieving by identifier</li>
<li>HQL</li>
<li>NHibernate Criteria API</li>
<li>Native SQL queries</li>
</ul>
</li>
<li> In depth discussion of fetching strategies and how to implement each one</li>
<li> Short section on batching and how it can speed up queries for selecting collections</li>
<li> Provides common sense ideas for analyzing NHibernate&#8217;s performance</li>
</ul>
<h4>Chapter 5</h4>
<ul>
<li> Background on database transactions, unit of work and conversations</li>
<li> Excellent section on transaction isolation</li>
<li> Background on ORM caching</li>
</ul>
<h4>Chapter 6</h4>
<ul>
<li> Starts with more definitions and comparisons of entities and value types</li>
<li> Nice chart showing how NHibernate mapping types relate to .Net types</li>
<li> Give in depth example of creating a custom mapping type</li>
<li> Nice tips on controlling collection sorting with mapping settings</li>
</ul>
<p><a href="http://www.amazon.com/gp/product/1932394923?ie=UTF8&amp;tag=1410softwarec-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1932394923"><img src="http://img94.imageshack.us/img94/9959/nhibernateinaction.jpg" border="0" alt="" width="50" height="75px" /><br />
</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/" title="NHibernate 2 Beginner&#8217;s Guide &#8211; Book Review">NHibernate 2 Beginner&#8217;s Guide &#8211; Book Review</a></li>
<li><a href="http://blog.codehangover.com/configuring-nhibernate-in-a-multiple-project-layout/" title="Configuring NHibernate in a Multiple Project Layout">Configuring NHibernate in a Multiple Project Layout</a></li>
<li><a href="http://blog.codehangover.com/asp-net-mvc-musings-and-book-review/" title="ASP.Net MVC Book Review and musings">ASP.Net MVC Book Review and musings</a></li>
</ul>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.codehangover.com%2Fnhibernate-in-action-book-review%2F&amp;linkname=NHibernate%20in%20Action%20%26%238211%3B%20Book%20Review"><img src="http://blog.codehangover.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.codehangover.com/nhibernate-in-action-book-review/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Configuring NHibernate in a Multiple Project Layout</title>
		<link>http://blog.codehangover.com/configuring-nhibernate-in-a-multiple-project-layout/</link>
		<comments>http://blog.codehangover.com/configuring-nhibernate-in-a-multiple-project-layout/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 03:58:53 +0000</pubDate>
		<dc:creator>Welzie</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://blog.codehangover.com/?p=417</guid>
		<description><![CDATA[I recently had to setup NHibernate in a multiple project c#, asp.net MVC solution. Since most of the tutorials and books only show a basic single project layout I thought I would post an example of how to configure NHiberate with a multi-project solution.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/configuring-nhibernate-in-a-multiple-project-layout/";</script><p>I recently had to setup NHibernate in a multiple project c#, asp.net MVC solution.  Since most of the tutorials and books only show a basic single project layout I thought I would post an example of how to configure NHiberate with a multi-project solution.</p>
<p>The main problem I had was that my model objects or entity objects were in a separate project than my database access code which contained the .hbm.xml mapping files.  When I tried to configure NHibernate with the basic examples I was getting an &#8220;NHibernate.MappingException : No persister for: YourClass&#8221; exception.  I was still getting that exception even after changing the .hbm.xml files to be an &#8220;Embedded Resource&#8221;.  I finally figured out that my .hbm.xml files needed to reference the ModelLayer assebmly and my NHibernate.Cfg.Configuration.AddAssembly() needed to reference the DataAccessLayer assebmly.  See screen shots and code examples below for more details.</p>
<h4>Solution layout</h4>
<div style="float: left;">
<ul>
<li>DataAccessLayer</li>
</ul>
<ul>
<ul>
<li>Duh. Database code</li>
<ul>
<li>References</li>
<ul>
<li>ModelLayer</li>
</ul>
</ul>
</ul>
<li>ModelLayer</li>
<ul>
<li>Model objects</li>
</ul>
<li>MvcAgain (web layer)</li>
<ul>
<li>ASP.Net project</li>
<li>References</li>
<ul>
<li>ModelLayer</li>
<li>ServiceLayer</li>
</ul>
</ul>
<li>ServiceLayer</li>
<ul>
<li>Business logic</li>
<li>Reusable logic</li>
<li>References</li>
<ul>
<li>ModelLayer</li>
<li>DataAccessLayer</li>
</ul>
</ul>
</ul>
</div>
<div style="float: right;">
<img src="http://img15.imageshack.us/img15/3041/projlayout.gif"/>
</div>
<div style="clear: both; padding-bottom: 10px;"></div>
<h4>Model objects Car.cs and CarLot.cs</h4>
<pre class="brush: java;">
using System.ComponentModel.DataAnnotations;
namespace MvcAgain.Models {
    public class Car {
        [Required]
        public virtual int Id {get; set;}
        [StringLength(35), Required]
        public virtual string Manufacturer {get; set;}
        [StringLength(35), Required]
        public virtual string Model {get; set;}
        public virtual CarLot Lot {get; set;}
    }
}//end of Car.cs

using Iesi.Collections;
using System.ComponentModel.DataAnnotations;
namespace MvcAgain.Models {
    public class CarLot {
        [Required]
        public virtual int Id { get; protected set; }
        [StringLength(35), Required]
        public virtual string Name { get; protected set; }
        public virtual ISet Cars { get; protected set; }
    }
}//end of CarLot.cs
</pre>
<p><br/></p>
<h4>Car.hbm.xml and CarLot.hbm.xml</h4>
<p>These .hbm.xml files are in the DataAccessLayer project, but they reference the ModelLayer assembly.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot;
                   assembly=&quot;ModelLayer&quot;
                   namespace=&quot;MvcAgain.Models&quot;&gt;
  &lt;class name=&quot;MvcAgain.Models.Car&quot; lazy=&quot;false&quot; table=&quot;cars&quot;&gt;
    &lt;id name=&quot;Id&quot;&gt;
      &lt;generator class=&quot;native&quot;/&gt;
    &lt;/id&gt;
    &lt;property name=&quot;Model&quot;/&gt;
    &lt;property name=&quot;Manufacturer&quot;/&gt;
    &lt;many-to-one name=&quot;Lot&quot; class=&quot;CarLot&quot; column=&quot;lotid&quot; not-null=&quot;true&quot;&gt;
    &lt;/many-to-one&gt;
  &lt;/class&gt;
&lt;/hibernate-mapping&gt;

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot;
                   assembly=&quot;ModelLayer&quot;
                   namespace=&quot;MvcAgain.Models&quot;&gt;
  &lt;class name=&quot;MvcAgain.Models.CarLot&quot; lazy=&quot;false&quot; table=&quot;carlots&quot;&gt;
    &lt;id name=&quot;Id&quot;&gt;
      &lt;generator class=&quot;native&quot;/&gt;
    &lt;/id&gt;
    &lt;property name=&quot;Name&quot;/&gt;
    &lt;set name=&quot;Cars&quot; inverse=&quot;true&quot;&gt;
        &lt;key column=&quot;lotid&quot;/&gt;
        &lt;one-to-many class=&quot;Car&quot;/&gt;
    &lt;/set&gt;
  &lt;/class&gt;
&lt;/hibernate-mapping&gt;
</pre>
<p><br/></p>
<h4>NHibernateSessionFactory.cs (NHibernate configuration code)</h4>
<pre class="brush: java;">
using NHibernate.Cfg;
using NHibernate;
namespace MvcAgain.DataAccessLayer {

    public class NHibernateSessionFactory {

        private static ISessionFactory sessionFactory = null;

        private static ISessionFactory SessionFactory {
            get {
                if(sessionFactory == null) {
                    Configuration configuration = new Configuration();
                    configuration.Configure();
                    //note: you must use the name of the assembly
                    //that contains the .hbm.xml mapping files.
                    //For this example NHibernate will load all .hbm.xml files it
                    //finds in the DataAccessLayer assembly
                    configuration.AddAssembly(&quot;DataAccessLayer&quot;);
                    sessionFactory = configuration.BuildSessionFactory();
                }
                return sessionFactory;
            }
        }

        public static ISession OpenSession() {
            return SessionFactory.OpenSession();
        }
    }//end of class
}//end of namespace
</pre>
<p></br></p>
<h4>ModelLayer assembly name in the properties pane.</h4>
<p><img src="http://img40.imageshack.us/img40/7961/assemblyname.gif"/><br />
<br/><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://blog.codehangover.com/nhibernate-in-action-book-review/" title="NHibernate in Action &#8211; Book Review">NHibernate in Action &#8211; Book Review</a></li>
<li><a href="http://blog.codehangover.com/book-review-nhibernate-2-beginners-guide/" title="NHibernate 2 Beginner&#8217;s Guide &#8211; Book Review">NHibernate 2 Beginner&#8217;s Guide &#8211; Book Review</a></li>
<li><a href="http://blog.codehangover.com/asp-net-mvc-musings-and-book-review/" title="ASP.Net MVC Book Review and musings">ASP.Net MVC Book Review and musings</a></li>
</ul>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.codehangover.com%2Fconfiguring-nhibernate-in-a-multiple-project-layout%2F&amp;linkname=Configuring%20NHibernate%20in%20a%20Multiple%20Project%20Layout"><img src="http://blog.codehangover.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.codehangover.com/configuring-nhibernate-in-a-multiple-project-layout/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

