<?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>Technical Jargon &#187; LLBLGen Pro</title>
	<atom:link href="http://www.jeremyskinner.co.uk/category/llblgen-pro/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremyskinner.co.uk</link>
	<description>Did you notice the information bar?</description>
	<lastBuildDate>Mon, 08 Mar 2010 20:17:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>LLBLGen Pro 2.6 Available</title>
		<link>http://www.jeremyskinner.co.uk/2008/06/08/llblgen-pro-26-available/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/06/08/llblgen-pro-26-available/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 12:05:53 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[LLBLGen Pro]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://jeremyskinner.wordpress.com/?p=34</guid>
		<description><![CDATA[Version 2.6 of my favourite Object Relational Mapper, LLBLGen Pro, is now available.

The main addition to V2.6 is support for a full Linq provider, so writing queries in LLBLGen just became a lot easier. V2.6 also includes a new Prefetching API which I wrote during the 2.6 beta period, which Frans then included in the [...]]]></description>
			<content:encoded><![CDATA[<p>Version 2.6 of my favourite Object Relational Mapper, <a href="http://llblgen.com">LLBLGen Pro</a>, is now available.</p>
<p>
The main addition to V2.6 is support for a full Linq provider, so writing queries in LLBLGen just became a lot easier. V2.6 also includes a new Prefetching API which I wrote during the 2.6 beta period, which <a href="http://weblogs.asp.net/fbouma">Frans</a> then included in the product.</p>
<p>
For example, to eagerly load a Customer -&gt; Orders relationship prior to v2.6, you&#8217;d have to do something like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">EntityCollection<span style="color: #008000;">&lt;</span>CustomerEntity<span style="color: #008000;">&gt;</span> customers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EntityCollection<span style="color: #008000;">&lt;</span>CustomerEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> CustomerEntityFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
PrefetchPath2 prefetcher <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PrefetchPath2<span style="color: #000000;">&#40;</span>EntityType.<span style="color: #0000FF;">Customer</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
prefetcher.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>CustomerEntity.<span style="color: #0000FF;">PrefetchPathOrders</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>DataAccessAdapter adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	adapter.<span style="color: #0000FF;">FetchEntityCollection</span><span style="color: #000000;">&#40;</span>customers, <span style="color: #0600FF;">null</span>, prefetcher<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, with the lambda prefetching API you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>var adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	var linq <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinqMetaData<span style="color: #000000;">&#40;</span>adapter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var customers <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from c <span style="color: #0600FF;">in</span> linq.<span style="color: #0000FF;">Customers</span> select c<span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">WithPath</span><span style="color: #000000;">&#40;</span>path <span style="color: #008000;">=&gt;</span> path.<span style="color: #0000FF;">Prefetch</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Orders</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Likewise, if you wanted to filter the prefetched orders (eg, only return orders costing more than £10) and prefetch each order&#8217;s OrderDetail, you&#8217;d need to do something like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">EntityCollection<span style="color: #008000;">&lt;</span>CustomerEntity<span style="color: #008000;">&gt;</span> customers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EntityCollection<span style="color: #008000;">&lt;</span>CustomerEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> CustomerEntityFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
PrefetchPath2 prefetcher <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PrefetchPath2<span style="color: #000000;">&#40;</span>EntityType.<span style="color: #0000FF;">Customer</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
prefetcher.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>CustomerEntity.<span style="color: #0000FF;">PrefetchPathOrders</span>, <span style="color: #FF0000;">0</span>, <span style="color: #008000;">new</span> PredicateExpression<span style="color: #000000;">&#40;</span>OrderFields.<span style="color: #0000FF;">TotalCost</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	.<span style="color: #0000FF;">SubPath</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>OrderEntity.<span style="color: #0000FF;">PrefetchPathOrderDetail</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>DataAccessAdapter adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	adapter.<span style="color: #0000FF;">FetchEntityCollection</span><span style="color: #000000;">&#40;</span>customers, <span style="color: #0600FF;">null</span>, prefetcher<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230;while now you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>var adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	var linq <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinqMetaData<span style="color: #000000;">&#40;</span>adapter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	var customers <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from c <span style="color: #0600FF;">in</span> linq.<span style="color: #0000FF;">Customers</span> select c<span style="color: #000000;">&#41;</span>
				.<span style="color: #0000FF;">WithPath</span><span style="color: #000000;">&#40;</span>path <span style="color: #008000;">=&gt;</span>
					path.<span style="color: #0000FF;">Prefetch</span><span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Orders</span><span style="color: #000000;">&#41;</span>
						.<span style="color: #0000FF;">FilterOn</span><span style="color: #000000;">&#40;</span>o <span style="color: #008000;">=&gt;</span> o.<span style="color: #0000FF;">TotalCost</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span>
						.<span style="color: #0000FF;">SubPath</span><span style="color: #000000;">&#40;</span>orderPath <span style="color: #008000;">=&gt;</span> orderPath.<span style="color: #0000FF;">Prefetch</span><span style="color: #000000;">&#40;</span>o <span style="color: #008000;">=&gt;</span> o.<span style="color: #0000FF;">OrderDetail</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Lambdas rock <img src='http://www.jeremyskinner.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/06/08/llblgen-pro-26-available/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Linq Repositories</title>
		<link>http://www.jeremyskinner.co.uk/2008/03/24/linq-repositories/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/03/24/linq-repositories/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 19:49:22 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[LLBLGen Pro]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://blog.jeremyskinner.me.uk/?p=26</guid>
		<description><![CDATA[
Recently, the beta of Linq to LLBLGen Pro was announced, which adds linq-querying capabilities to LLBLGen, the Object Relational Mapper that I use in most of my projects.


LLBLGen&#8217;s querying API is very powerful, but also somewhat complex. For example, to retrieve a list of orders from all customers in the Northwind database for customers in [...]]]></description>
			<content:encoded><![CDATA[<p>
Recently, the beta of <a href="http://weblogs.asp.net/fbouma/archive/2008/03/12/beta-of-linq-to-llblgen-pro-released.aspx">Linq to LLBLGen Pro</a> was announced, which adds linq-querying capabilities to LLBLGen, the Object Relational Mapper that I use in most of my projects.
</p>
<p>
LLBLGen&#8217;s querying API is very powerful, but also somewhat complex. For example, to retrieve a list of orders from all customers in the Northwind database for customers in the UK, you would write something like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">EntityCollection<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span> orders <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EntityCollection<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> OrderEntityFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
RelationPredicateBucket bucket <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RelationPredicateBucket<span style="color: #000000;">&#40;</span>CustomerFields.<span style="color: #0000FF;">Country</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;UK&quot;</span><span style="color: #000000;">&#41;</span>
bucket.<span style="color: #0000FF;">Relations</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>OrderEntity.<span style="color: #0000FF;">Relations</span>.<span style="color: #0000FF;">CustomerEntityUsingCustomerId</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>DataAccessAdapter adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	adapter.<span style="color: #0000FF;">FetchEntityCollection</span><span style="color: #000000;">&#40;</span>orders, bucket<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Linq support maintains the type safety, whilst also allowing this query to be expressed in a more SQL-like fashion (which I personally find to be more intuitive):
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>DataAccessAdapter adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	var meta <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LinqMetaData<span style="color: #000000;">&#40;</span>adapter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var query <span style="color: #008000;">=</span> from o <span style="color: #0600FF;">in</span> meta.<span style="color: #0000FF;">Order</span>
			where o.<span style="color: #0000FF;">Customer</span>.<span style="color: #0000FF;">Country</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;UK&quot;</span>
			select o<span style="color: #008000;">;</span>
&nbsp;
	var results <span style="color: #008000;">=</span> query.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This got me thinking&#8230;now that there are several ORMs that have linq support, wouldn&#8217;t it be nice if there was a consistent method for performing linq-based queries, independent of the underlying ORM implementation. And thus the linq-repository was born.</p>
<p>
The majority of the work is done by the BaseRepository class which acts as a wrapper around the underlying linq provider. There&#8217;s also an IRepository interface which the BaseRepository implements:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IQueryable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T toSave<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T toSave, <span style="color: #FF0000;">bool</span> isNew<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">void</span> Delete<span style="color: #000000;">&#40;</span>T toDelete<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> BaseRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> IQueryable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> source<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> IQueryable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Source
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> source<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
		set <span style="color: #000000;">&#123;</span> source <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> BaseRepository<span style="color: #000000;">&#40;</span>IQueryable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> source<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">source</span> <span style="color: #008000;">=</span> source<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	IEnumerator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> source.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IEnumerator GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> source.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Expression Expression
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> source.<span style="color: #0000FF;">Expression</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Type ElementType
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> source.<span style="color: #0000FF;">ElementType</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IQueryProvider Provider
	<span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> source.<span style="color: #0000FF;">Provider</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T toSave<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> Delete<span style="color: #000000;">&#40;</span>T toDelete<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T toSave, <span style="color: #FF0000;">bool</span> isNew<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
Note that the constructor for BaseRepository takes an IQueryable representing the underlying linq provider. Now, the LLBLGen-specific subclass:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LLBLGenRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> BaseRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> EntityBase2, IEntity2, <span style="color: #008000;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> IDataAccessAdapter Adapter <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> LLBLGenRepository<span style="color: #000000;">&#40;</span>IDataAccessAdapter adapter, IElementCreator2 elementCreator<span style="color: #000000;">&#41;</span>
			<span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>CreateQuery<span style="color: #000000;">&#40;</span>adapter, elementCreator<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Adapter</span> <span style="color: #008000;">=</span> adapter<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IQueryable CreateQuery<span style="color: #000000;">&#40;</span>IDataAccessAdapter adapter, IElementCreator2 elementCreator<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> DataSource2<span style="color: #000000;">&#40;</span>adapter, elementCreator, functionMappings, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T toSave<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>toSave <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;toSave&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		Adapter.<span style="color: #0000FF;">SaveEntity</span><span style="color: #000000;">&#40;</span>toSave<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T toSave, <span style="color: #FF0000;">bool</span> isNew<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>toSave <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;toSave&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isNew<span style="color: #000000;">&#41;</span> toSave.<span style="color: #0000FF;">IsNew</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
		Adapter.<span style="color: #0000FF;">SaveEntity</span><span style="color: #000000;">&#40;</span>toSave<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Delete<span style="color: #000000;">&#40;</span>T toDelete<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Adapter.<span style="color: #0000FF;">DeleteEntity</span><span style="color: #000000;">&#40;</span>entity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that the constructor for the LLBLGenRepository takes instances of an IDataAccessAdapter and an IElementCreator (the two objects necessary for running linq-queries against LLBLGen) and creates a DataSource2 object (the LLBLGen query provider) which is then passed to the BaseRepository&#8217;s constructor.
</p>
<p>So, I can now write code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">IRepository<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span> orders <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LLBLGenRepository<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> DataAccessAdapter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #008000;">new</span> ElementCreator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var query <span style="color: #008000;">=</span> from o <span style="color: #0600FF;">in</span> orders
		where o.<span style="color: #0000FF;">Customer</span>.<span style="color: #0000FF;">Country</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;UK&quot;</span>
		select o<span style="color: #008000;">;</span></pre></div></div>

<p>
To remove the calls to <i>new DataAccessAdapter()</i> and <i>new ElementCreator()</i>, I moved the responsibility for instantiating the repository over to an IoC container:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//in my application startup routine:</span>
IoC.<span style="color: #0000FF;">Initialise</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WindsorContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
IoC.<span style="color: #0000FF;">Container</span>.<span style="color: #0000FF;">AddComponent</span><span style="color: #008000;">&lt;</span>IDataAccessAdapter, DataAccessAdapter<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
IoC.<span style="color: #0000FF;">Container</span>.<span style="color: #0000FF;">AddComponent</span><span style="color: #008000;">&lt;</span>IElementCreator2, ElementCreator<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
IoC.<span style="color: #0000FF;">Container</span>.<span style="color: #0000FF;">AddComponent</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Repository&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IRepository<span style="color: #008000;">&lt;&gt;</span><span style="color: #000000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>LLBLGenRepository<span style="color: #008000;">&lt;&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
The IoC static class is simply a wrapper for the <a href="http://www.castleproject.org/container/index.html">Windsor container</a>.
</p>
<p>Now I can instantiate repositories like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">IRepository<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span> orders <span style="color: #008000;">=</span> IoC.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IRepository<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
I don&#8217;t really like this, so I wrap it in a static gateway:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Repository
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&amp;</span>t<span style="color: #008000;">;</span> <span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> IoC.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>&#8230;and now I can write queries like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var query <span style="color: #008000;">=</span> from o <span style="color: #0600FF;">in</span> Repository.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span>
		where o.<span style="color: #0000FF;">Customer</span>.<span style="color: #0000FF;">Country</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;UK&quot;</span>
		select o<span style="color: #008000;">;</span></pre></div></div>

<p>
Alternatively, now that the repository is registered with Windsor, I can inject the repository directly into my ASPNET MVC Controllers:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OrdersController <span style="color: #008000;">:</span> ConventionController
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> IRepository<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span> ordersRepository<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> OrdersController<span style="color: #000000;">&#40;</span>IRepository<span style="color: #008000;">&lt;</span>OrderEntity<span style="color: #008000;">&gt;</span> repository<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ordersRepository</span> <span style="color: #008000;">=</span> repository<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> OrdersFromCustomersInTheUk<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		ViewData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;orders&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> from o <span style="color: #0600FF;">in</span> ordersRepository
						where o.<span style="color: #0000FF;">Customer</span>.<span style="color: #0000FF;">Country</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;UK&quot;</span>
						select o<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/03/24/linq-repositories/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
