<?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; Hacks</title>
	<atom:link href="http://blog.ashmind.com/category/hacks/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>
		<item>
		<title>Typed Repeater — ASP.Net Hack</title>
		<link>http://blog.ashmind.com/2007/03/17/typed-repeater-aspnet-hack/</link>
		<comments>http://blog.ashmind.com/2007/03/17/typed-repeater-aspnet-hack/#comments</comments>
		<pubDate>Sat, 17 Mar 2007 00:42:56 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Hacks]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/03/17/typed-repeater-%e2%80%94-aspnet-hack/</guid>
		<description><![CDATA[I wrote an article on Code Project about the way to implement generics in ITemplate. I thought about including the whole article as a blog entry, but I am still fighting with code highlighting in blog. Interesting fact about the Repeater solution is that you can use it to build any kind of generic asp.net [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote <a href="http://www.codeproject.com/aspnet/TypedRepeater.asp">an article</a> on Code Project about the way to implement generics in ITemplate.<br />
I thought about including the whole article as a blog entry, but I am still fighting with code highlighting in blog.</p>
<p>Interesting fact about the Repeater solution is that you can use it to build any kind of generic asp.net control that will be perfectly specifiable in mark-up.<br />
I am writing a GenericeControlBuilder that will not be specific to the single control type, but I didn&#8217;t have enough time to finish it yet.</p>
<p>The interesting question is how to implement fully generic TemplateContainerAttribute. Dynamic creation of the attribute is not a problem at all — solved in the article.<br />
But the question is how to know right  TemplateContainer type in a GenericControlBuilder that has no hardcoded information about the control it creates.<br />
I consider adding another attribute that will specify how control type arguments map to the TemplateContainer type arguments.</p>
<pre style="font-family: Courier New">
public class Repeater<T> {
    …

    [GenericTemplateContainer(typeof(RepeaterItem<>), UseParentTypeArguments=true)]
    public ITemplate ItemTemplate

    …
}
</pre>
<p>This is just to show the basic idea — this syntax is way too limiting.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/03/17/typed-repeater-aspnet-hack/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

