<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>mhinze.com &#187; nhibernate</title>
	<atom:link href="http://mhinze.com/tag/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://mhinze.com</link>
	<description>Matt Hinze, learning in public</description>
	<lastBuildDate>Fri, 18 May 2012 15:09:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mhinze.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>mhinze.com &#187; nhibernate</title>
		<link>http://mhinze.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mhinze.com/osd.xml" title="mhinze.com" />
	<atom:link rel='hub' href='http://mhinze.com/?pushpress=hub'/>
		<item>
		<title>Linq to NHibernate in 10 minutes</title>
		<link>http://mhinze.com/2008/07/22/linq-to-nhibernate-in-10-minutes/</link>
		<comments>http://mhinze.com/2008/07/22/linq-to-nhibernate-in-10-minutes/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 12:45:59 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[codecampserver]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://mhinze.com/?p=52</guid>
		<description><![CDATA[If you are curious about Linq to NHibernate and want to quickly check it out, here is an easy way to do it. Check out a copy of the CodeCampServer source (svn).&#160; CodeCampServer is easy to work with because it &#8230; <a href="http://mhinze.com/2008/07/22/linq-to-nhibernate-in-10-minutes/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=52&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are curious about <a href="http://www.ayende.com/Blog/archive/2007/03/17/Implementing-Linq-for-NHibernate-A-How-To-Guide--Part.aspx">Linq to NHibernate</a> and want to quickly check it out, here is an easy way to do it.</p>
<p>Check out a copy of the <a href="http://codecampserver.org">CodeCampServer</a> source (<a href="http://codecampserver.googlecode.com/svn/trunk/">svn</a>).&nbsp; CodeCampServer is easy to work with because it has good tests on an already-working NHibernate implementation.</p>
<p>Check out a copy of <a href="http://sourceforge.net/projects/nhcontrib/">NH Contrib</a> (<a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/">svn</a>), open the NHibernate.Linq solution in Visual Studio and build. You may have to remove the compiler flag that indicates .NET 3.5 sp1 is available.</p>
<p>Open the CodeCampServer.sln and remove the NHibernate.dll reference from the DataAccess and IntegrationTests projects.&nbsp; Add a reference to NHibernate.dll from the NHibernate.Linq bin directory.&nbsp; Build and run tests.&nbsp; I had to play with the <a href="http://codecampserver.googlecode.com/svn/trunk/src/IntegrationTests/DataAccess/DatabaseTesterBase.cs">DatabaseTesterBase.recreateDatabase()</a> method because I think are are still some bugs in Export():</p>
<pre class="code"><span style="color:blue;">public static void </span>recreateDatabase()
{
    <span style="color:blue;">var </span>exporter = <span style="color:blue;">new </span><span style="color:#2b91af;">SchemaExport</span>(<span style="color:blue;">new </span><span style="color:#2b91af;">HybridSessionBuilder</span>().GetConfiguration());
    exporter.Drop(<span style="color:blue;">false</span>, <span style="color:blue;">true</span>);
    exporter.Create(<span style="color:blue;">false</span>, <span style="color:blue;">true</span>);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now add a reference to NHibernate.Linq.dll in the DataAccess project. You can start <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx">writing queries</a> and check your work with the integration tests that already exist. Here are a few simple queries for <a href="http://codecampserver.googlecode.com/svn/trunk/src/DataAccess/Impl/ConferenceRepository.cs">ConferenceRepository</a>:</p>
<p><a href="http://11011.net/software/vspaste"></a>
<pre class="code"><span style="color:blue;">public </span><span style="color:#2b91af;">Conference</span>[] GetAllConferences()
{
    <span style="color:#2b91af;">ISession </span>session = getSession();
    <span style="color:blue;">var </span>query = <span style="color:blue;">from </span>conference <span style="color:blue;">in </span>session.Linq&lt;<span style="color:#2b91af;">Conference</span>&gt;() <span style="color:blue;">select </span>conference;
    <span style="color:blue;">return </span>query.ToList().ToArray();
}

<span style="color:blue;">public </span><span style="color:#2b91af;">Conference </span>GetConferenceByKey(<span style="color:blue;">string </span>key)
{
    <span style="color:#2b91af;">ISession </span>session = getSession();
    <span style="color:blue;">var </span>query = <span style="color:blue;">from </span>conference <span style="color:blue;">in </span>session.Linq&lt;<span style="color:#2b91af;">Conference</span>&gt;()
                <span style="color:blue;">where </span>conference.Key == key
                <span style="color:blue;">select </span>conference;
    <span style="color:blue;">return </span>query.Distinct().Single();
}

<span style="color:blue;">public </span><span style="color:#2b91af;">Conference </span>GetFirstConferenceAfterDate(<span style="color:#2b91af;">DateTime </span>date)
{
    <span style="color:#2b91af;">ISession </span>session = getSession();
    <span style="color:blue;">var </span>query = <span style="color:blue;">from </span>conference <span style="color:blue;">in </span>session.Linq&lt;<span style="color:#2b91af;">Conference</span>&gt;()
                <span style="color:blue;">where </span>conference.StartDate &gt;= date
                <span style="color:blue;">orderby </span>conference.StartDate
                <span style="color:blue;">select </span>conference;
    <span style="color:blue;">return </span>query.First();
}

<span style="color:blue;">public </span><span style="color:#2b91af;">Conference </span>GetMostRecentConference(<span style="color:#2b91af;">DateTime </span>date)
{
    <span style="color:#2b91af;">ISession </span>session = getSession();
    <span style="color:blue;">var </span>query = <span style="color:blue;">from </span>conference <span style="color:blue;">in </span>session.Linq&lt;<span style="color:#2b91af;">Conference</span>&gt;()
            <span style="color:blue;">where </span>conference.EndDate &lt;= date
            <span style="color:blue;">select </span>conference;
    query.OrderByDescending(conference =&gt; conference.EndDate);
    <span style="color:blue;">return </span>query.First();
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mhinze.wordpress.com/52/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mhinze.wordpress.com/52/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mhinze.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mhinze.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mhinze.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mhinze.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mhinze.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mhinze.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mhinze.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mhinze.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=52&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mhinze.com/2008/07/22/linq-to-nhibernate-in-10-minutes/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d6043840ea98542a9c9331c7e7940b1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhinze</media:title>
		</media:content>
	</item>
		<item>
		<title>Audit options with NHibernate</title>
		<link>http://mhinze.com/2008/07/11/audit-options-with-nhibernate/</link>
		<comments>http://mhinze.com/2008/07/11/audit-options-with-nhibernate/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 13:55:27 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[auditing]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://mhinze.com/?p=43</guid>
		<description><![CDATA[In this post I am talking about auditing for the business, not the technical infrastructure.&#160; As an example story, users will want to see certain fields highlighted on a grid if that field has changed in the last week, and &#8230; <a href="http://mhinze.com/2008/07/11/audit-options-with-nhibernate/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=43&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post I am talking about auditing for the business, not the technical infrastructure.&nbsp; As an example story, users will want to see certain fields highlighted on a grid if that field has changed in the last week, and the info about the change (who, when, what) should be a tooltip for the cell.</p>
<p><strong>Database trigger</strong></p>
<ul>
<li>See Jon Galloway&#8217;s post on <a href="http://weblogs.asp.net/jgalloway/archive/2008/01/27/adding-simple-trigger-based-auditing-to-your-sql-server-database.aspx">Auditing with SQL Server</a></li>
<li>To <a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:59412348055">generate an audit trigger in oracle</a> you can do something similar.</li>
</ul>
<p><strong>NH Interceptor</strong> (per NH in action and the <a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/#manipulatingdata-interceptors">NH docs</a>)</p>
<p><strong>NH Listener</strong> (per the <a href="http://www.hibernate.org/318.html">Hibernate docs</a>)</p>
<p><strong>Domain model</strong></p>
<p>Because auditing is a simple application of interceptors there is a lot of guidance online about that approach, and using an interceptor was my first attempt at auditing. </p>
<p>But since the concept of an audit entry is important to the application (and the users!), it makes sense to promote this concept to a domain object.&nbsp; Reporting is simple and the behavior is easier to manage.&nbsp; Create a class called AuditEntry (with supporting mapping file) and hang collections of AuditEntry from the entity. Manage this collection in regular behavior:</p>
<pre class="code"><span style="color:blue;">public virtual void </span>ChangeStatus(<span style="color:#2b91af;">Employee </span>employee, <span style="color:#2b91af;">DateTime </span>time, <span style="color:#2b91af;">OrderStatus </span>status)
{
    <span style="color:blue;">if</span>(<span style="color:blue;">this</span>.Status != status)
    {
        <span style="color:#2b91af;">AuditEntry </span>entry = <span style="color:blue;">new </span><span style="color:#2b91af;">AuditEntry</span>(employee, time, <span style="color:blue;">this</span>.Status, status);
        _auditEntries.Add(entry);
    }
    Status = status;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>After having gone down the database trigger path and the interceptor path and encountering friction I now recommend the domain model approach.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mhinze.wordpress.com/43/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mhinze.wordpress.com/43/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mhinze.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mhinze.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mhinze.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mhinze.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mhinze.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mhinze.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mhinze.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mhinze.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mhinze.com&#038;blog=678824&#038;post=43&#038;subd=mhinze&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mhinze.com/2008/07/11/audit-options-with-nhibernate/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0d6043840ea98542a9c9331c7e7940b1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mhinze</media:title>
		</media:content>
	</item>
	</channel>
</rss>
