<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Christopher Bennage : C#, Silverlight, Raven</title><link>http://devlicio.us/blogs/christopher_bennage/archive/tags/C_2300_/Silverlight/Raven/default.aspx</link><description>Tags: C#, Silverlight, Raven</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Disabling Certain Linq Operations</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2011/01/21/disabling-certain-linq-operations.aspx</link><pubDate>Fri, 21 Jan 2011 15:30:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:64743</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I’ve been working on the Silverlight client for the &lt;a href="http://www.ravendb.net/" target="_blank"&gt;RavenDB&lt;/a&gt;. 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&amp;#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 &lt;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"&gt;this issue&lt;/a&gt; a few months ago. &lt;/p&gt;  &lt;p&gt;To demonstrate how easy it is to make a mistake, you could very easily write this code:&lt;/p&gt;  &lt;pre class="c#:nogutter:nocontrols" name="code"&gt;using (var session = documentStore.OpenAsyncSession())
{
    var companies = session.Query&amp;lt;Company&amp;gt;()
                    .Where(x =&amp;gt; x.Name == &amp;quot;Blue Spire&amp;quot;)
                    .ToList();
}&lt;/pre&gt;

&lt;p&gt;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 &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank"&gt;Task&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;Unfortunately, the technique that Ayende used in his post won’t work for us. At least, not without some tweaking. Why not?&lt;/p&gt;

&lt;p&gt;Well, if we examine the &lt;em&gt;session&lt;/em&gt; above. we’ll see the Query method returns an IRavenQueryable&amp;lt;T&amp;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&amp;lt;T&amp;gt;, instead we have an IQueryable&amp;lt;T&amp;gt;. This is because all of the Linq methods are extensions methods living on &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.queryable.aspx" target="_blank"&gt;Queryable&lt;/a&gt;. (&lt;em&gt;Well, at least the ones we’d be using in this discussion so far. There are other implementations such as the ones on &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx" target="_blank"&gt;&lt;em&gt;Enumerable&lt;/em&gt;&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Once a link in the chain returns an IQueryable&amp;lt;T&amp;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&amp;lt;T&amp;gt; and thus explicitly control what is available to developer.&lt;/p&gt;

&lt;p&gt;It turns out that this is easy to do, but (like all things in implementing Linq) rather tedious.&lt;/p&gt;

&lt;p&gt;We have to provide our own implementation of &lt;em&gt;every&lt;/em&gt; extension method for IQueryable. In most cases, the new implementation is just a matter of casting back to our desired type:&lt;/p&gt;

&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public static IRavenQueryable&amp;lt;T&amp;gt; Where&amp;lt;T&amp;gt;(this IRavenQueryable&amp;lt;T&amp;gt; source, Expression&amp;lt;Func&amp;lt;T, bool&amp;gt;&amp;gt; prediate)
{
    return (IRavenQueryable&amp;lt;T&amp;gt;)Queryable.Where(source, prediate);
}&lt;/pre&gt;

&lt;p&gt;Now that we can always return an IRavenQueryable&amp;lt;T&amp;gt; we can use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx" target="_blank"&gt;Obsolete&lt;/a&gt; attribute to disable the Linq operations that don’t make sense for our context.&lt;/p&gt;

&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;[Obsolete(&amp;quot;You cannot execute a query synchronously from the Silverlight client. Instead, use ToListAsync().&amp;quot;, true)]
public static IList&amp;lt;T&amp;gt; ToList&amp;lt;T&amp;gt;(this IRavenQueryable&amp;lt;T&amp;gt; source)
{
    throw new NotSupportedException();
}&lt;/pre&gt;

&lt;p&gt;This also means that we are able to make ToListAsync specific to IRavenQueryable&amp;lt;T&amp;gt; (the only place where it makes sense).&lt;/p&gt;

&lt;pre class="c#:nogutter:nocontrols" name="code"&gt;public static Task&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt; ToListAsync&amp;lt;T&amp;gt;(this IRavenQueryable&amp;lt;T&amp;gt; source)
{
    // if you really want to see the implementation, check out the source on github
}&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=64743" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/Raven/default.aspx">Raven</category></item></channel></rss>