Christopher Bennage : C#, Linq, Ravenhttp://devlicio.us/blogs/christopher_bennage/archive/tags/C_2300_/Linq/Raven/default.aspxTags: C#, Linq, RavenenCommunityServer 2008.5 SP1 (Build: 31106.3070)Disabling Certain Linq Operationshttp://devlicio.us/blogs/christopher_bennage/archive/2011/01/21/disabling-certain-linq-operations.aspxFri, 21 Jan 2011 18:30:00 GMT40756a8b-6212-4073-9d98-6c26781577de:64743Christopher Bennage0<p>I’ve been working on the Silverlight client for the <a href="http://www.ravendb.net/" target="_blank">RavenDB</a>. In Raven, you can write a Linq query in the Silverlight client that will ultimately be executed against a Raven server somewhere. The execution of the query is asynchronous and we want that to be explicit in the api. It should be obvious to the&#160; developer that its execution is an asynchronous operation. By that I mean, we don’t want the api to give the consuming developer a nasty surprise. Ayende wrote about <a title="it very easy to do some really stupid things" href="http://ayende.com/Blog/archive/2010/10/10/disabling-linq.aspx" target="_blank">this issue</a> a few months ago. </p> <p>To demonstrate how easy it is to make a mistake, you could very easily write this code:</p> <pre class="c#:nogutter:nocontrols" name="code">using (var session = documentStore.OpenAsyncSession())
{
var companies = session.Query&lt;Company&gt;()
.Where(x =&gt; x.Name == &quot;Blue Spire&quot;)
.ToList();
}</pre>
<p>The problem here is that the execution of the query is going to be asynchronous and we need to provide a mechanism for a callback after the results have been returned from the server. We have added ToListAynsc() which will initiate the request and provide a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank">Task</a> for handling the continuation. That’s great, but now the problem is that calling ToList() doesn’t make any sense. We’d like to prevent a developer from calling ToList(), preferably at compile time.</p>
<p>Unfortunately, the technique that Ayende used in his post won’t work for us. At least, not without some tweaking. Why not?</p>
<p>Well, if we examine the <em>session</em> above. we’ll see the Query method returns an IRavenQueryable&lt;T&gt;. This interface inherits from the built-in interfaces necessary for supporting Linq; which ultimately is IQueryable. Now the nature of Linq is to have a series of chained method calls. After the first link in the Linq, we no longer have an IRavenQueryable&lt;T&gt;, instead we have an IQueryable&lt;T&gt;. This is because all of the Linq methods are extensions methods living on <a href="http://msdn.microsoft.com/en-us/library/system.linq.queryable.aspx" target="_blank">Queryable</a>. (<em>Well, at least the ones we’d be using in this discussion so far. There are other implementations such as the ones on </em><a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx" target="_blank"><em>Enumerable</em></a>.)</p>
<p>Once a link in the chain returns an IQueryable&lt;T&gt; then any method defined on Queryable is valid for compilation, even though it might not make sense. What we’d like to do is override these Linq methods so that we always return an IRavenQueryable&lt;T&gt; and thus explicitly control what is available to developer.</p>
<p>It turns out that this is easy to do, but (like all things in implementing Linq) rather tedious.</p>
<p>We have to provide our own implementation of <em>every</em> extension method for IQueryable. In most cases, the new implementation is just a matter of casting back to our desired type:</p>
<pre class="c#:nogutter:nocontrols" name="code">public static IRavenQueryable&lt;T&gt; Where&lt;T&gt;(this IRavenQueryable&lt;T&gt; source, Expression&lt;Func&lt;T, bool&gt;&gt; prediate)
{
return (IRavenQueryable&lt;T&gt;)Queryable.Where(source, prediate);
}</pre>
<p>Now that we can always return an IRavenQueryable&lt;T&gt; we can use the <a href="http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx" target="_blank">Obsolete</a> attribute to disable the Linq operations that don’t make sense for our context.</p>
<pre class="c#:nogutter:nocontrols" name="code">[Obsolete(&quot;You cannot execute a query synchronously from the Silverlight client. Instead, use ToListAsync().&quot;, true)]
public static IList&lt;T&gt; ToList&lt;T&gt;(this IRavenQueryable&lt;T&gt; source)
{
throw new NotSupportedException();
}</pre>
<p>This also means that we are able to make ToListAsync specific to IRavenQueryable&lt;T&gt; (the only place where it makes sense).</p>
<pre class="c#:nogutter:nocontrols" name="code">public static Task&lt;IList&lt;T&gt;&gt; ToListAsync&lt;T&gt;(this IRavenQueryable&lt;T&gt; source)
{
// if you really want to see the implementation, check out the source on github
}</pre><div style="clear:both;"></div><img src="http://devlicio.us/aggbug.aspx?PostID=64743" width="1" height="1">SilverlightC#LinqRaven