<?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; Linq</title>
	<atom:link href="http://www.jeremyskinner.co.uk/category/linq/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>Linq to Sql and ASP.NET MVC &#8211; DataLoadOptions per Request</title>
		<link>http://www.jeremyskinner.co.uk/2010/02/02/linq-to-sql-and-asp-net-mvc-dataloadoptions-per-request/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/02/02/linq-to-sql-and-asp-net-mvc-dataloadoptions-per-request/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 13:47:30 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=329</guid>
		<description><![CDATA[
This is the third in a series of posts on using ASP.NET MVC with Linq to Sql:


Part 1 &#8211; DataContext Per Request
Part 2 &#8211; AutoCommit and the RoutePreParser
Part 3 &#8211; DataLoadOptions per Request


Code for this series is available here.


One common problem when using an ORM is the issue of &#8220;select n+1&#8243;. For example, take the [...]]]></description>
			<content:encoded><![CDATA[<p>
This is the third in a series of posts on using ASP.NET MVC with Linq to Sql:
</p>
<ul>
<li>Part 1 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/">DataContext Per Request</a></li>
<li>Part 2 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/02/01/linq-to-sql-and-asp-net-mvc-autocommit-and-the-routepreparser/">AutoCommit and the RoutePreParser</a></li>
<li>Part 3 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/02/02/linq-to-sql-and-asp-net-mvc-dataloadoptions-per-request/">DataLoadOptions per Request</a></li>
</ul>
<p>
Code for this series is <a href="http://cloud.github.com/downloads/JeremySkinner/Experiments/LinqToSqlWithMvc.zip">available here</a>.
</p>
<p>
One common problem when using an ORM is the issue of &#8220;select n+1&#8243;. For example, take the following Linq to Sql query:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var context <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BlogDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var posts <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Posts</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var post <span style="color: #0600FF;">in</span> posts<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var comment <span style="color: #0600FF;">in</span> post.<span style="color: #0000FF;">Comments</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     var commenter <span style="color: #008000;">=</span> comment.<span style="color: #0000FF;">Commenter</span>.<span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
     Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;User {0} commented on post {1}&quot;</span>, commenter, post.<span style="color: #0000FF;">Title</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>
This might look fine on the surface, but by default Linq to Sql will lazily-load all associations. This means that for each post loaded from the database Linq to Sql will issue a query to load the comments, and for each comment Linq to Sql will issue another query to load the commenter. So if you have 10 posts, and each post has 5 comments then you&#8217;ll end up making 61 queries to the database (1 for the posts, 1 for each post to get the comments, and 1 for each comment to get the user)
</p>
<p>
This can be mitigated by using DataLoadOptions to eagerly load all of the data in a single query:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var context <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BlogDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var options <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataLoadOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
options.<span style="color: #0000FF;">LoadWith</span><span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Comments</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
options.<span style="color: #0000FF;">LoadWith</span><span style="color: #008000;">&lt;</span>Comment<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Commenter</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
context.<span style="color: #0000FF;">LoadOptions</span> <span style="color: #008000;">=</span> options<span style="color: #008000;">;</span>
&nbsp;
var posts <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Posts</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var post <span style="color: #0600FF;">in</span> posts<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
 <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>The Problem</h3>
<p>
However, once a query has been executed, the LoadOptions property of a DataContext is frozen &#8211; you cannot then specify any additional eager loading paths.*
</p>
<p>
If you&#8217;re using a DataContext per request <a href="http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/">as per my previous post</a> then this can be a problem. For example, imagine you have the following controller action:
</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> PostsController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> BlogDataContext context<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> PostsController<span style="color: #000000;">&#40;</span>BlogDataContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">context</span> <span style="color: #008000;">=</span> context<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//injected via our IoC container</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> ActionResult Show<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    var options <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataLoadOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    options.<span style="color: #0000FF;">LoadWith</span><span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Comments</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    options.<span style="color: #0000FF;">LoadWith</span><span style="color: #008000;">&lt;</span>Comment<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Commenter</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    context.<span style="color: #0000FF;">LoadOptions</span> <span style="color: #008000;">=</span> options<span style="color: #008000;">;</span>
&nbsp;
    var post <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Id</span> <span style="color: #008000;">==</span> id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>post<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;then this will work as expected. But now imagine your action is decorated by an ActionFilter:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>LoadCurrentUser<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Show<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 
  <span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
&#8230;and let&#8217;s assume that our LoadCurrentUser filter attempts to load the details of the current user from the database and store them in ViewData:
</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> LoadCurrentUserAttribute <span style="color: #008000;">:</span> ActionFilterAttribute <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext filterContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var context <span style="color: #008000;">=</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>BlogDataContext<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		var identity <span style="color: #008000;">=</span> filterContext.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">User</span>.<span style="color: #0000FF;">Identity</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>identity.<span style="color: #0000FF;">IsAuthenticated</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			var currentUser <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Users</span>.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">UserName</span> <span style="color: #008000;">==</span> identity.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			filterContext.<span style="color: #0000FF;">Controller</span>.<span style="color: #0000FF;">ViewData</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CurrentUser&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> currentUser<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Now when the controller is executed we will get the following error:
</p>
<p>
<em><br />
 Setting load options is not allowed after results have been returned from a query.<br />
</em>
</p>
<p>
This happens because we&#8217;re trying to set the LoadOptions for the Post after the LoadCurrentUser filter already executed a query.
</p>
<h3>The Solution</h3>
<p>
The approach I use to work around this problem is to use a custom AuthorizationFilter in conjunction with an eager loading specification.
</p>
<p>
First, we define an interface for eager loading specifications:
</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> IEagerLoadingSpecification <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">void</span> Build<span style="color: #000000;">&#40;</span>DataLoadOptions options<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Next, we can write some classes that implement IEagerLoadingSpecification that define particular specifications for eager loading. Using the example above, we have PostWithComments and CommentWithCommenter:
</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> PostWithComments <span style="color: #008000;">:</span> IEagerLoadingSpecification <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Build<span style="color: #000000;">&#40;</span>DataLoadOptions options<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    options.<span style="color: #0000FF;">LoadWith</span><span style="color: #008000;">&lt;</span>Post<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Comments</span><span style="color: #000000;">&#41;</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> <span style="color: #FF0000;">class</span> CommentWithCommenter <span style="color: #008000;">:</span> IEagerLoadingSpecification  <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Build<span style="color: #000000;">&#40;</span>DataLoadOptions options<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     options.<span style="color: #0000FF;">LoadWith</span><span style="color: #008000;">&lt;</span>Comment<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c.<span style="color: #0000FF;">Commenter</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>
Next, we write an EagerlyLoadAttribute which implements IAuthorizationFilter and takes an array of Types in its constructor:
</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> EagerlyLoadAttribute <span style="color: #008000;">:</span> FilterAttribute, IAuthorizationFilter <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">private</span> Type<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> types<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> EagerlyLoadAttribute<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">params</span> Type<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> types<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">types</span> <span style="color: #008000;">=</span> types<span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> OnAuthorization<span style="color: #000000;">&#40;</span>AuthorizationContext filterContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    var loadOptions <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataLoadOptions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var context <span style="color: #008000;">=</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>BlogDataContext<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var type <span style="color: #0600FF;">in</span> types<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
       <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IEagerLoadingSpecification<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">IsAssignableFrom</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Type {0} does not implement IEagerLoadingSpecification&quot;</span>, type<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #000000;">&#125;</span>
&nbsp;
      var spec <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IEagerLoadingSpecification<span style="color: #000000;">&#41;</span>Activator.<span style="color: #0000FF;">CreateInstance</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      spec.<span style="color: #0000FF;">Build</span><span style="color: #000000;">&#40;</span>loadOptions<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
   context.<span style="color: #0000FF;">LoadOptions</span> <span style="color: #008000;">=</span> loadOptions<span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
So what does this filter do? Firstly, because it implements IAuthorizationFilter this means it will be invoked before any action filters that decorate your controller action. When OnAuthorization is invoked, it loops over each of the Types that we&#8217;ve passed to its constructor, instantiates them and casts them to IEagerLoadingSpecification. Next, a single DataLoadOptions instance is passed to each of the specifications in turn so they can build up the required eager loading paths.
</p>
<p>
The end result is that you can now define your actions like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #000000;">&#91;</span>EagerlyLoad<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>PostWithComments<span style="color: #000000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>CommentWithCommenter<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
  <span style="color: #000000;">&#91;</span>LoadCurrentUser<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> ActionResult Show<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    var post <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p.<span style="color: #0000FF;">Id</span> <span style="color: #008000;">==</span> id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>post<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Because the EagerlyLoadAttribute is an IAuthorizationFilter, it will be invoked before anything else meaning that by the time both our LoadCurrentUser filter and the Show action are invoked the DataLoadOptions have already been set on our DataContext.
</p>
<p>
* Note that other ORMs like NHibernate, LLBLGen and the Entity Framework don&#8217;t have this problem because they allow eager loading paths to be specified at the query level.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/02/02/linq-to-sql-and-asp-net-mvc-dataloadoptions-per-request/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq to Sql and ASP.NET MVC &#8211; AutoCommit and the RoutePreParser</title>
		<link>http://www.jeremyskinner.co.uk/2010/02/01/linq-to-sql-and-asp-net-mvc-autocommit-and-the-routepreparser/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/02/01/linq-to-sql-and-asp-net-mvc-autocommit-and-the-routepreparser/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 12:28:53 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=298</guid>
		<description><![CDATA[
This is the second in a series of posts on using ASP.NET MVC with Linq to Sql:


Part 1 &#8211; DataContext Per Request
Part 2 &#8211; AutoCommit and the RoutePreParser
Part 3 &#8211; DataLoadOptions per Request


Code for this series is available here.


In my previoust post, I demonstrated how you could scope a Linq to Sql DataContext to a [...]]]></description>
			<content:encoded><![CDATA[<p>
This is the second in a series of posts on using ASP.NET MVC with Linq to Sql:
</p>
<ul>
<li>Part 1 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/">DataContext Per Request</a></li>
<li>Part 2 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/02/01/linq-to-sql-and-asp-net-mvc-autocommit-and-the-routepreparser/">AutoCommit and the RoutePreParser</a></li>
<li>Part 3 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/02/02/linq-to-sql-and-asp-net-mvc-dataloadoptions-per-request/">DataLoadOptions per Request</a></li>
</ul>
<p>
Code for this series is <a href="http://cloud.github.com/downloads/JeremySkinner/Experiments/LinqToSqlWithMvc.zip">available here</a>.
</p>
<p>
In my previoust post, I demonstrated how you could scope a Linq to Sql DataContext to a single HTTP Request by using StructureMap to manage the lifetime of the DataContext instance. This often works well, but has a couple of gotchas. Let&#8217;s take a look at an example:
</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> PostController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> BlogDataContext context<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> PostController<span style="color: #000000;">&#40;</span>BlogDataContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">context</span> <span style="color: #008000;">=</span> context<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> View<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: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span>, AutoCommit<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span>Post post<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>post<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Show&quot;</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#123;</span> id <span style="color: #008000;">=</span> post.<span style="color: #0000FF;">Id</span> <span style="color: #000000;">&#125;</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> ActionResult Show<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var post <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">SingleOrDefault</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Id</span> <span style="color: #008000;">==</span> id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>post <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> HttpException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">404</span>, <span style="color: #666666;">&quot;The post could not be found.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>post<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>
In this example, we have a Post controller for our fictional Blog database. The Create action renders a view where the user can create a new Post and then sends an HTTP POST to the other overload to the Create action (decorated with AcceptVerbs(HttpVerbs.Post)).
</p>
<p>
This action is decorated with the AutoCommit attribute from my <a href="http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/">previous post</a> so that SubmitChanges will automatically be called on our DataContext. We then redirect to the &#8220;Show&#8221; action passing the Id of the newly created post in the route data.
</p>
<p>
The Show action loads the Post from the database with the corresponding Id and displays it to the user. If a post with the specified Id could not be found, it will throw a 404 exception.
</p>
<p>
There is a bug here that may not be immediately obvious. Assume that the Post&#8217;s Id property is generated by an auto-incrementing Identity field in the database. If you were to run this application and create a new Post, the Show action <strong>will always throw 404 even though the Post has been successfully created.</strong></p>
<p>This happens because of when SubmitChanges is called. Our AutoCommit filter is invoked <strong>after the action has finished executing</strong> this means that at the time we call RedirectToAction(&#8220;Show&#8221;, new{ id = post.Id }) the new post <strong>has not yet been saved, so its Id will be 0</strong>.</p>
<p>Linq to Sql will not update the Id property until after SubmitChanges is called, which is too late in the process. The user will end up being redirected to Post/Show/0 instead of using the Id of the newly created post.
</p>
<p>
The simple workaround is to call SubmitChanges directly inside the action:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span>Post post<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>post<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ModelState.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var transaction <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TransactionScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			context.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			transaction.<span style="color: #0000FF;">Complete</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>
&nbsp;
	<span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Show&quot;</span>, <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> id <span style="color: #008000;">=</span> post.<span style="color: #0000FF;">Id</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
&#8230;but this then makes our AutoCommit attribute useless and also makes the controller action significantly more complex.
</p>
<p>
However, there is another approach we could use which still allows us to use the AutoCommit attribute. To understand this, we first need to look at the order in which things are happening when our action is invoked:
</p>
<ol>
<li>The IoC container creates our DataContext</li>
<li>The controller is instantiated and the DataContext is passed to its constructor</li>
<li>The Create action is invoked with a Post instance created by MVC&#8217;s DefaultModelBinder</li>
<li>The post is attached to the DataContext by calling InsertOnSubmit</li>
<li>A RedirectToRouteResult is created by calling &#8220;RedirectToAction&#8221; with a dictionary of route values</li>
<li>The AutoCommit attribute calls SubmitChanges to our DataContext</li>
<li>The Post is written to the database</li>
<li>ExecuteResult on RedirectToRouteResult is invoked</li>
<li>The Redirect URL is generated by the RouteCollection</li>
<li>The user&#8217;s browser is redirected to this URL</li>
</ol>
<p>
Instead of passing the Post Id (which will be 0) to RedirectToAction, <strong>we could pass the entire post instance.</strong> After the post has been saved we can then *replace* the post instance in the RouteValueDictionary with the post&#8217;s Id before the URL is generated. This can be done by intercepting the RouteValueDictionary just before the URL is generated.
</p>
<h3>Introducing the RoutePreParser</h3>
<p>
The first thing we need is a way to identify how an object (in this case, our Post) should be converted to a route value. To do this, we can create an interface, IUrlRoutable:
</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> IUrlRoutable <span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">object</span> GetRouteParameter<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>
&#8230;and we can implement this interface in our Post class:
</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;">partial</span> <span style="color: #FF0000;">class</span> Post <span style="color: #008000;">:</span> IUrlRoutable <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> GetRouteParameter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> Id<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Next we can create a &#8220;fake&#8221; route. This route never generates a URL or handles a request &#8211; it is merely used to intercept the RouteValueDictionary before a URL is generated:
</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> RoutePreParser <span style="color: #008000;">:</span> RouteBase <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> RouteData GetRouteData<span style="color: #000000;">&#40;</span>HttpContextBase httpContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> VirtualPathData GetVirtualPath<span style="color: #000000;">&#40;</span>RequestContext requestContext, RouteValueDictionary values<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var query <span style="color: #008000;">=</span> from pair <span style="color: #0600FF;">in</span> values
				where pair.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span>
				let routable <span style="color: #008000;">=</span> pair.<span style="color: #0000FF;">Value</span> <span style="color: #0600FF;">as</span> IUrlRoutable
				where routable <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span>
				select <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> pair.<span style="color: #0000FF;">Key</span>, Value <span style="color: #008000;">=</span> routable.<span style="color: #0000FF;">GetRouteParameter</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var pair <span style="color: #0600FF;">in</span> query.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			values<span style="color: #000000;">&#91;</span>pair.<span style="color: #0000FF;">Key</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> pair.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Here we loop through each key-value pair in the RouteValueDictionary. If the value implements IUrlRoutable then we call GetRouteParameter on that object and <strong>replace the original value in the RouteValueDictionary with the result of this method.</strong>
</p>
<p>
Next, we have to add the fake route to the RouteCollection in Application_Start. Note that this <strong>must be the first route added so that it gets a chance to intercept the RouteValueDictionary.</strong>
</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: #0600FF;">void</span> RegisterRoutes<span style="color: #000000;">&#40;</span>RouteCollection routes<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//Add our fake route first</span>
	routes.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> RoutePreParser<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	routes.<span style="color: #0000FF;">IgnoreRoute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{resource}.axd/{*pathInfo}&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	routes.<span style="color: #0000FF;">MapRoute</span><span style="color: #000000;">&#40;</span>
		<span style="color: #666666;">&quot;Default&quot;</span>, <span style="color: #008080; font-style: italic;">// Route name</span>
		<span style="color: #666666;">&quot;{controller}/{action}/{id}&quot;</span>, <span style="color: #008080; font-style: italic;">// URL with parameters</span>
		<span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span> controller <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Home&quot;</span>, action <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Index&quot;</span>, id <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: #008080; font-style: italic;">// Parameter defaults</span>
	<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Now, in our controller action we change the Create action to store the Post instance in the route values:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span>, AutoCommit<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span>Post post<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>post<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Show&quot;</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#123;</span> id <span style="color: #008000;">=</span> post <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
&#8230;and everything now works as expected.
</p>
<p>
To recap, this is the new chain of events:
</p>
<ol>
<li>The IoC container creates our DataContext</li>
<li>The controller is instantiated and the DataContext is passed to its constructor</li>
<li>The Create action is invoked with a Post instance created by MVC&#8217;s DefaultModelBinder</li>
<li>The post is attached to the DataContext by calling InsertOnSubmit</li>
<li>A RedirectToRouteResult is created by calling &#8220;RedirectToAction&#8221; with a dictionary of route values. (The &#8220;Id&#8221; parameter is our new Post instance.)
</li>
<li>The AutoCommit attribute calls SubmitChanges to our DataContext</li>
<li>The Post is written to the database</li>
<li>The Post instance stored in the RouteValueDictionary automatically has its Id property updated</li>
<li>ExecuteResult on RedirectToRouteResult is invoked</li>
<li>Our RoutePreParser inspects the RouteValueDictionary.</li>
<li>GetRouteParameter on Post is invoked, returning the now-populated Post Id</li>
<li>The RoutePreParser removes the Post instance from the RouteValueDictionary</li>
<li>The RoutePreParser inserts the Post Id as the &#8220;id&#8221; in the RouteValueDictionary</li>
<li>The (now correct) redirect URL is generated by the RouteCollection</li>
<li>The user&#8217;s browser is redirected to this URL</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/02/01/linq-to-sql-and-asp-net-mvc-autocommit-and-the-routepreparser/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linq to Sql and ASP.NET MVC &#8211; DataContext Per Request</title>
		<link>http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 11:45:23 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=292</guid>
		<description><![CDATA[
This is the first in a series of posts about using Linq to Sql with ASP.NET MVC.


Part 1 &#8211; DataContext Per Request
Part 2 &#8211; AutoCommit and the RoutePreParser
Part 3 &#8211; DataLoadOptions per Request


Code for this series is available here.


When using an ORM tool within a web application, it&#8217;s often common to scope a unit of [...]]]></description>
			<content:encoded><![CDATA[<p>
This is the first in a series of posts about using Linq to Sql with ASP.NET MVC.
</p>
<ul>
<li>Part 1 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/">DataContext Per Request</a></li>
<li>Part 2 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/02/01/linq-to-sql-and-asp-net-mvc-autocommit-and-the-routepreparser/">AutoCommit and the RoutePreParser</a></li>
<li>Part 3 &#8211; <a href="http://www.jeremyskinner.co.uk/2010/02/02/linq-to-sql-and-asp-net-mvc-dataloadoptions-per-request/">DataLoadOptions per Request</a></li>
</ul>
<p>
Code for this series is <a href="http://cloud.github.com/downloads/JeremySkinner/Experiments/LinqToSqlWithMvc.zip">available here</a>.
</p>
<p>
When using an ORM tool within a web application, it&#8217;s often common to scope a unit of work to the lifetime of an HTTP Request. If you&#8217;re using Linq to Sql and ASP.NET MVC, you can achieve this by using an Inversion of Control container in conjunction with an ActionFilter.
</p>
<p>
For this example, I&#8217;m going to be using the <a href="http://structuremap.sourceforge.net/Default.htm">StructureMap</a> IoC container alongside a fictional &#8220;Blog&#8221; database.
</p>
<p>
Firstly, you&#8217;ll need to configure StructureMap by calling ObjectFactory.Configure inside your Global.asax passing in a custom Registry instance:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	RegisterRoutes<span style="color: #000000;">&#40;</span>RouteTable.<span style="color: #0000FF;">Routes</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	ObjectFactory.<span style="color: #0000FF;">Configure</span><span style="color: #000000;">&#40;</span>cfg <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
		cfg.<span style="color: #0000FF;">AddRegistry</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MyRegistry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The code for MyRegistry looks like this:</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> MyRegistry <span style="color: #008000;">:</span> Registry <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> MyRegistry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>BlogDataContext<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">HttpContextScoped</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			.<span style="color: #0000FF;">Use</span><span style="color: #000000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> BlogDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		Scan<span style="color: #000000;">&#40;</span>scan <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
			scan.<span style="color: #0000FF;">AddAllTypesOf</span><span style="color: #008000;">&lt;</span>Controller<span style="color: #008000;">&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;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Here I&#8217;m telling StructureMap to create one instance of my BlogDataContext per HTTP Request as well as registering each Controller instance with the container.
</p>
<p>
Next, we need to tell MVC to use StructureMap to instantiate our controllers. This can be done by creating a custom ControllerFactory:
</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> StructureMapControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> IController GetControllerInstance<span style="color: #000000;">&#40;</span>RequestContext requestContext, Type controllerType<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IController<span style="color: #000000;">&#41;</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #000000;">&#40;</span>controllerType<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>
We can then replace the DefaultControllerFactory with the StructureMapControllerFactory in our Application_Start:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Application_Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	RegisterRoutes<span style="color: #000000;">&#40;</span>RouteTable.<span style="color: #0000FF;">Routes</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	ObjectFactory.<span style="color: #0000FF;">Configure</span><span style="color: #000000;">&#40;</span>cfg <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
		cfg.<span style="color: #0000FF;">AddRegistry</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MyRegistry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	ControllerBuilder.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">SetControllerFactory</span><span style="color: #000000;">&#40;</span>
		<span style="color: #008000;">new</span> StructureMapControllerFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Now, if we create a PostController (for creating and editing Posts in our fictional blog), we can now take a BlogDataContext in the constructor (for better testability, you&#8217;d probably want to hide the DataContext behind an interface):
</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> PostController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> BlogDataContext context<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> PostController<span style="color: #000000;">&#40;</span>BlogDataContext context<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">context</span> <span style="color: #008000;">=</span> context<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> ActionResult Edit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> id<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var post <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">Single</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Id</span> <span style="color: #008000;">==</span> id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>post<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>
Now, visiting http://mysite/Post/Edit/1 would return a view to display a post with the Id of 1 (assuming the appropriate Post exists in the database).
</p>
<h3>Automatically Submitting Changes</h3>
<p>
We can take this a stage further by adding an ActionFilter to our application that will automatically call SubmitChanges on our DataContext at the end of the HTTP Request:
</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> AutoCommitAttribute <span style="color: #008000;">:</span> ActionFilterAttribute <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuted<span style="color: #000000;">&#40;</span>ActionExecutedContext filterContext<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>filterContext.<span style="color: #0000FF;">Controller</span>.<span style="color: #0000FF;">ViewData</span>.<span style="color: #0000FF;">ModelState</span>.<span style="color: #0000FF;">IsValid</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			var currentDataContext <span style="color: #008000;">=</span> ObjectFactory.<span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>BlogDataContext<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</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>var transaction <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TransactionScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				currentDataContext.<span style="color: #0000FF;">SubmitChanges</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
				transaction.<span style="color: #0000FF;">Complete</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>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Here we call into StructureMap to retrieve our current DataContext and commit any changes back to the database inside a transaction. Note that this will only happen if the ModelState is valid (ie there are no validation errors). </p>
<p>Another thing to keep in mind that using an IoC container as a Service Locator (as we&#8217;re doing in this filter) is generally considered bad practice. There are ways <a href="http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/">around</a> <a href="http://www.iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Action-Filters.aspx">this</a> but these are outside the scope of this post.
</p>
<p>
Now, whenever we make a change to one of our entities the change will automatically be committed to the database provided the action is decorated with the AutoCommit attribute:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span>, AutoCommit<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> ActionResult Create<span style="color: #000000;">&#40;</span>Post post<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	context.<span style="color: #0000FF;">Posts</span>.<span style="color: #0000FF;">InsertOnSubmit</span><span style="color: #000000;">&#40;</span>post<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Index&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/01/31/linq-to-sql-and-asp-net-mvc-datacontext-per-request/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Testing your Linq to Sql Mappings</title>
		<link>http://www.jeremyskinner.co.uk/2009/04/21/testing-your-linq-to-sql-mappings/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/04/21/testing-your-linq-to-sql-mappings/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 09:45:28 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=130</guid>
		<description><![CDATA[Here&#8217;s a quick way to verify that all of your Linq to Sql entities are mapped to their tables correctly:

namespace DbTests &#123;
  using System.Linq;
  using NUnit.Framework;
&#160;
  &#91;TestFixture&#93;
  public class MappingTester &#123;
    &#91;Test&#93;
    public void Verify_Mappings&#40;&#41; &#123;
      using &#40;var context = [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick way to verify that all of your Linq to Sql entities are mapped to their tables correctly:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> DbTests <span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF;">using</span> <span style="color: #008080;">NUnit.Framework</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MappingTester <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Verify_Mappings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var context <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyDataContext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var mappedTable <span style="color: #0600FF;">in</span> context.<span style="color: #0000FF;">Mapping</span>.<span style="color: #0000FF;">GetTables</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">RowType</span>.<span style="color: #0000FF;">Type</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          context.<span style="color: #0000FF;">GetTable</span><span style="color: #000000;">&#40;</span>mappedTable<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Cast</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>.<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>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</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/2009/04/21/testing-your-linq-to-sql-mappings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
