<?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>Andrey Shchekin &#187; NHibernate.Linq</title>
	<atom:link href="http://blog.ashmind.com/category/net/nhibernate/nhibernate-linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ashmind.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Mar 2010 18:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Projecting unmapped Ids with Linq-to-NHibernate</title>
		<link>http://blog.ashmind.com/2009/11/05/projecting-unmapped-ids-with-linq-to-nhibernate/</link>
		<comments>http://blog.ashmind.com/2009/11/05/projecting-unmapped-ids-with-linq-to-nhibernate/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 17:46:50 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[NHibernate.Linq]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/?p=80</guid>
		<description><![CDATA[There are situations when you do not need to get the fully tracked entities using NHibernate &#8212; you know you wouldn&#8217;t ever edit them and need minimum overhead for this specific scenario. One example is AJAX auto-completion. On the other hand, if you are as obsessed with architectural purity as me, you probably do not [...]]]></description>
			<content:encoded><![CDATA[<p>There are situations when you do not need to get the fully tracked entities using NHibernate &#8212; you know you wouldn&#8217;t ever edit them and need minimum overhead for this specific scenario. One example is AJAX auto-completion. On the other hand, if you are as obsessed with architectural purity as me, you probably <a href="http://stackoverflow.com/questions/860200/ddd-primary-keys-ids-and-orms-for-example-nhibernate">do not have Id properties in your entities</a>, since they are artefacts of (relational) DBs.</p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>So there is the question: in UI layer we want to do
<pre class="csharpcode">repository.Query().Select(x =&gt; <span class="kwrd">new</span> ListItem { Key = x.Id, Name = x.Name })</pre>
<p> But there are no &#8220;Id&#8221; properties in our entities. How can we do this (without returning to the dark ages of untyped criteria)?</p>
<p>There is a very simple (and working) answer. Let&#8217;s start with how I do it for individual entities. When I need a key/id in the UI to identify the entity between requests, I use repository.GetKey(entity), which internally calls session.GetIdentifier(entity). Simple and not intrusive into domain logic. Now,
<pre class="csharpcode inline">repository.Query().Select(x =&gt; <span class="kwrd">new</span> ListItem { Key = GetKey(x), Name = x.Name })</pre>
<p> is obviously impossible, since HQL/DB can not understand GetKey call.</p>
<p>Ok, so the solution is to pre-process the call before Linq-to-NHibernate and replace GetKey call with reference to fake property named &#8220;id&#8221;, which is a magic name NHibernate understands as identifier reference. Linq-to-NHibernate even provides public expression visitor, so it was trivial to create KeyMethodToIdRewritingVisitor (the fake PropertyInfo took most effort, which had to have some stuff to fool Expression.Property).</p>
<p>You can get resulting code below.<br />
It is not perfect, but it works and flaws are really easy to polish out.</p>
<ol>
<li><a href='http://blog.ashmind.com/wp-content/uploads/2009/11/Repository.cs'>Repository</a></li>
<li><a href='http://blog.ashmind.com/wp-content/uploads/2009/11/KeyMethodToIdRewritingVisitor.cs'>KeyMethodToIdRewritingVisitor</a></li>
<li><a href='http://blog.ashmind.com/wp-content/uploads/2009/11/KeyEnabledQueryProvider.cs'>KeyEnabledQueryProvider</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2009/11/05/projecting-unmapped-ids-with-linq-to-nhibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

