<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>VibrantCode - OneQuery</title>
    <link>http://blog.andrewnurse.net/</link>
    <description>Oooh...pretty code</description>
    <language>en-us</language>
    <copyright>Andrew Nurse</copyright>
    <lastBuildDate>Tue, 11 Dec 2007 14:21:19 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>andrew@andrewnurse.net</managingEditor>
    <webMaster>andrew@andrewnurse.net</webMaster>
    <item>
      <trackback:ping>http://blog.andrewnurse.net/Trackback.aspx?guid=909ac400-c7d1-4d5f-912e-a74b63555408</trackback:ping>
      <pingback:server>http://blog.andrewnurse.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.andrewnurse.net/PermaLink,guid,909ac400-c7d1-4d5f-912e-a74b63555408.aspx</pingback:target>
      <dc:creator>Andrew Nurse</dc:creator>
      <wfw:comment>http://blog.andrewnurse.net/CommentView,guid,909ac400-c7d1-4d5f-912e-a74b63555408.aspx</wfw:comment>
      <wfw:commentRss>http://blog.andrewnurse.net/SyndicationService.asmx/GetEntryCommentsRss?guid=909ac400-c7d1-4d5f-912e-a74b63555408</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Aside from my school work and my work on the <a href="http://www.dotnetnuke.com/tabid/824/default.aspx">DotNetNuke
Reports Module</a> (very very close to release now), I've been tinkering with another
project over the past few months. It's called OneQuery and its yet another Data Access
Layer tool. OneQuery is designed to provide a "<a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent
interface</a>" to database queries. 
</p>
        <p>
What is a fluent interface? Wikipedia defines it as "an object oriented construct
that defines a behaviour capable of relaying the instruction context of a subsequent
call". Essentially, it means that instead of writing code like this:
</p>
        <pre class="csharpcode">List&lt;<span class="kwrd">int</span>&gt; ints = <span class="kwrd">new</span> List&lt;<span class="kwrd">int</span>&gt;();
ints.Add(1); ints.Add(2); ints.Add(3);</pre>
        <p>
You can write code like this
</p>
        <pre class="csharpcode">List&lt;<span class="kwrd">int</span>&gt; ints = <span class="kwrd">new</span> List&lt;<span class="kwrd">int</span>&gt;().Add(1).Add(2).Add(3);</pre>
        <p>
That's quite a savings and, in my opinion, it's much more readable.
</p>
        <h4>So, what's the point?
</h4>
        <p>
Well, recently I've been thinking about the issue of database agnostic...ness (database
agnostivity? database agnosticism?). I really want to be able to write data access
code that will run on multiple database engines without having to manually translate
it. <a href="http://www.dotnetnuke.com/">DotNetNuke</a> provides a, theoretically,
database agnostic interface through Data Providers. However, to support another database
engine, the core DataProvider must be translated, Stored Procedures must be converted
and all sorts of "Porting" issues arise. Wouldn't it be nice if you could
WORA (Write Once, Run Anywhere... a Java term) your SQL Scripts?
</p>
        <h4>Wait, hasn't this been done?
</h4>
        <p>
Of course it has, and many many times. First, there's the elephant in the room, LINQ.
However, aside from requiring .Net 3.5, if you want to write database agnostic code,
you need to use something like the ADO.Net Entity Framework. OneQuery is designed
to work in .Net 2.0, and I'm planning to write a LINQ layer on top of it so that you
can write LINQ queries and run them on multiple database engines (not just SQL Server).
I think OneQuery could serve as a "migration path" where you can write OneQuery
syntax and then move to LINQ syntax when you're ready to move to .Net 3.5 while still
keeping the database-agnostic OneQuery core.
</p>
        <p>
There are a lot more database libraries out there as well. <a href="http://subsonicproject.com">SubSonic</a> provides
an excellent Object/Relational Mapper (ORM) that allows you to work directly with
objects, but sometimes you want a little more control. There are, of course, many
others such as NHibernate, IBATIS.Net, and EntitySpaces. However, I found that while
working with these tools I would find little things that would annoy me. For example,
SubSonic does provide a way for you to directly query the database and get an IDataReader
back, but it has an interface I found a little awkward (though it is a nice fluent
interface). There are also smaller annoying features such as a lack of support for
certain SQL statements (like "INSERT") and a lack of JOIN support. On the
other side, tools like NHibernate are really designed to allow you to ignore the database
completely, which is a nice feature, but sometimes I need that direct database control.
</p>
        <h4>
        </h4>
        <h4>
        </h4>
        <h4>So, what is OneQuery?
</h4>
        <p>
[Wow, I started two sub-headings off with 'so', perhaps that summer at Microsoft is
affecting my patterns of speech. They say Microsofties tend to start there sentences
with "so" and say "drinking from a firehose" and "super"
a lot]
</p>
        <p>
OneQuery is designed to let you focus on writing one query, and allows you to run
the query on multiple database systems. Instead of using strings, which the compiler
ignores, I wanted to provide a fluent interface and use operator overloading to make
the queries as readable as possible.
</p>
        <h4>Enough already, get to the code!
</h4>
        <p>
Ok, so here's a sample query using OneQuery's fluent syntax:
</p>
        <p>
          <a href="http://blog.andrewnurse.net/content/binary/WindowsLiveWriter/OneQueryAFluentInterfacetoDatabaseQuerie_1396C/query_4.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="88" alt="query" src="http://blog.andrewnurse.net/content/binary/WindowsLiveWriter/OneQueryAFluentInterfacetoDatabaseQuerie_1396C/query_thumb_1.png" width="658" border="0" />
          </a>
        </p>
        <p>
(I posted it as a screenshot to make sure the spacing worked out, I'll post the text
at the end if you want to copy it).
</p>
        <p>
There are a few things I want to do to let you clear that up even further. For example,
it would be much easier if you could write ".OrderBy(Product.ListPrice).Desc()",
or ".Where(Product.ListPrice.Between(10).And(100))"
</p>
        <p>
Behind the scenes, the only other code you need to "write" is a helper class
for the products table. I used quotes there because I'm planning on creating a code
generator to do that for you.
</p>
        <h4>CAN HAZ CODE?
</h4>
        <p>
Hopefully I've tantalized you a little bit and you'd like to check it out. You can
download a preview release from our <a href="http://www.codeplex.com/onequery">CodePlex
Project Site</a>, and discuss future features there as well. I'm planning a whole
bunch of new features including (but definitely NOT limited to): 
</p>
        <ul>
          <li>
Support for ALL SQL DML Statements (INSERT, UPDATE, DELETE) 
</li>
          <li>
Full support for SELECT (including JOIN, GROUP BY, HAVING, and sub-queries) 
</li>
          <li>
A Code Generator to create the helper classes 
</li>
          <li>
and more! 
</li>
        </ul>
        <p>
Please let me know what you think! I want to make sure this system solves problems
for as many people as possible, and the best way to do that is to get contributions
from the community as early as possible. The <a href="http://www.codeplex.com/onequery">CodePlex
site</a> has plenty of places to contribute, so please check it out!
</p>
        <h5>Sample Code in Text Form
</h5>
        <pre class="csharpcode">IDataReader rdr = Product.Query()
                         .Where(Product.ListPrice &gt; 10 &amp; Product.ListPrice &lt; 100)
                         .OrderBy(Product.ListPrice, SortDirection.Descending)
                         .ExecuteReader();</pre>
        <p>
        </p>
        <div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:72e27e42-13fc-4276-bae4-35c1cd2093a5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati
Tags: <a href="http://technorati.com/tags/Data%20Access" rel="tag">Data Access</a>,<a href="http://technorati.com/tags/C#" rel="tag">C#</a>,<a href="http://technorati.com/tags/VB" rel="tag">VB</a>,<a href="http://technorati.com/tags/.Net" rel="tag">.Net</a>,<a href="http://technorati.com/tags/Fluent%20Interface" rel="tag">Fluent
Interface</a>,<a href="http://technorati.com/tags/ORM" rel="tag">ORM</a></div>
        <img width="0" height="0" src="http://blog.andrewnurse.net/aggbug.ashx?id=909ac400-c7d1-4d5f-912e-a74b63555408" />
      </body>
      <title>OneQuery - A Fluent Interface to Database Queries</title>
      <guid isPermaLink="false">http://blog.andrewnurse.net/PermaLink,guid,909ac400-c7d1-4d5f-912e-a74b63555408.aspx</guid>
      <link>http://blog.andrewnurse.net/2007/12/11/OneQueryAFluentInterfaceToDatabaseQueries.aspx</link>
      <pubDate>Tue, 11 Dec 2007 14:21:19 GMT</pubDate>
      <description>&lt;p&gt;
Aside from my school work and my work on the &lt;a href="http://www.dotnetnuke.com/tabid/824/default.aspx"&gt;DotNetNuke
Reports Module&lt;/a&gt; (very very close to release now), I've been tinkering with another
project over the past few months. It's called OneQuery and its yet another Data Access
Layer tool. OneQuery is designed to provide a &amp;quot;&lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent
interface&lt;/a&gt;&amp;quot; to database queries. 
&lt;/p&gt;
&lt;p&gt;
What is a fluent interface? Wikipedia defines it as &amp;quot;an object oriented construct
that defines a behaviour capable of relaying the instruction context of a subsequent
call&amp;quot;. Essentially, it means that instead of writing code like this:
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; ints = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();
ints.Add(1); ints.Add(2); ints.Add(3);&lt;/pre&gt;
&lt;p&gt;
You can write code like this
&lt;/p&gt;
&lt;pre class="csharpcode"&gt;List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; ints = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;().Add(1).Add(2).Add(3);&lt;/pre&gt;
&lt;p&gt;
That's quite a savings and, in my opinion, it's much more readable.
&lt;/p&gt;
&lt;h4&gt;So, what's the point?
&lt;/h4&gt;
&lt;p&gt;
Well, recently I've been thinking about the issue of database agnostic...ness (database
agnostivity? database agnosticism?). I really want to be able to write data access
code that will run on multiple database engines without having to manually translate
it. &lt;a href="http://www.dotnetnuke.com/"&gt;DotNetNuke&lt;/a&gt; provides a, theoretically,
database agnostic interface through Data Providers. However, to support another database
engine, the core DataProvider must be translated, Stored Procedures must be converted
and all sorts of &amp;quot;Porting&amp;quot; issues arise. Wouldn't it be nice if you could
WORA (Write Once, Run Anywhere... a Java term) your SQL Scripts?
&lt;/p&gt;
&lt;h4&gt;Wait, hasn't this been done?
&lt;/h4&gt;
&lt;p&gt;
Of course it has, and many many times. First, there's the elephant in the room, LINQ.
However, aside from requiring .Net 3.5, if you want to write database agnostic code,
you need to use something like the ADO.Net Entity Framework. OneQuery is designed
to work in .Net 2.0, and I'm planning to write a LINQ layer on top of it so that you
can write LINQ queries and run them on multiple database engines (not just SQL Server).
I think OneQuery could serve as a &amp;quot;migration path&amp;quot; where you can write OneQuery
syntax and then move to LINQ syntax when you're ready to move to .Net 3.5 while still
keeping the database-agnostic OneQuery core.
&lt;/p&gt;
&lt;p&gt;
There are a lot more database libraries out there as well. &lt;a href="http://subsonicproject.com"&gt;SubSonic&lt;/a&gt; provides
an excellent Object/Relational Mapper (ORM) that allows you to work directly with
objects, but sometimes you want a little more control. There are, of course, many
others such as NHibernate, IBATIS.Net, and EntitySpaces. However, I found that while
working with these tools I would find little things that would annoy me. For example,
SubSonic does provide a way for you to directly query the database and get an IDataReader
back, but it has an interface I found a little awkward (though it is a nice fluent
interface). There are also smaller annoying features such as a lack of support for
certain SQL statements (like &amp;quot;INSERT&amp;quot;) and a lack of JOIN support. On the
other side, tools like NHibernate are really designed to allow you to ignore the database
completely, which is a nice feature, but sometimes I need that direct database control.
&lt;/p&gt;
&lt;h4&gt;
&lt;/h4&gt;
&lt;h4&gt;
&lt;/h4&gt;
&lt;h4&gt;So, what is OneQuery?
&lt;/h4&gt;
&lt;p&gt;
[Wow, I started two sub-headings off with 'so', perhaps that summer at Microsoft is
affecting my patterns of speech. They say Microsofties tend to start there sentences
with &amp;quot;so&amp;quot; and say &amp;quot;drinking from a firehose&amp;quot; and &amp;quot;super&amp;quot;
a lot]
&lt;/p&gt;
&lt;p&gt;
OneQuery is designed to let you focus on writing one query, and allows you to run
the query on multiple database systems. Instead of using strings, which the compiler
ignores, I wanted to provide a fluent interface and use operator overloading to make
the queries as readable as possible.
&lt;/p&gt;
&lt;h4&gt;Enough already, get to the code!
&lt;/h4&gt;
&lt;p&gt;
Ok, so here's a sample query using OneQuery's fluent syntax:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.andrewnurse.net/content/binary/WindowsLiveWriter/OneQueryAFluentInterfacetoDatabaseQuerie_1396C/query_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="88" alt="query" src="http://blog.andrewnurse.net/content/binary/WindowsLiveWriter/OneQueryAFluentInterfacetoDatabaseQuerie_1396C/query_thumb_1.png" width="658" border="0" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
(I posted it as a screenshot to make sure the spacing worked out, I'll post the text
at the end if you want to copy it).
&lt;/p&gt;
&lt;p&gt;
There are a few things I want to do to let you clear that up even further. For example,
it would be much easier if you could write &amp;quot;.OrderBy(Product.ListPrice).Desc()&amp;quot;,
or &amp;quot;.Where(Product.ListPrice.Between(10).And(100))&amp;quot;
&lt;/p&gt;
&lt;p&gt;
Behind the scenes, the only other code you need to &amp;quot;write&amp;quot; is a helper class
for the products table. I used quotes there because I'm planning on creating a code
generator to do that for you.
&lt;/p&gt;
&lt;h4&gt;CAN HAZ CODE?
&lt;/h4&gt;
&lt;p&gt;
Hopefully I've tantalized you a little bit and you'd like to check it out. You can
download a preview release from our &lt;a href="http://www.codeplex.com/onequery"&gt;CodePlex
Project Site&lt;/a&gt;, and discuss future features there as well. I'm planning a whole
bunch of new features including (but definitely NOT limited to): 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Support for ALL SQL DML Statements (INSERT, UPDATE, DELETE) 
&lt;/li&gt;
&lt;li&gt;
Full support for SELECT (including JOIN, GROUP BY, HAVING, and sub-queries) 
&lt;/li&gt;
&lt;li&gt;
A Code Generator to create the helper classes 
&lt;/li&gt;
&lt;li&gt;
and more! 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Please let me know what you think! I want to make sure this system solves problems
for as many people as possible, and the best way to do that is to get contributions
from the community as early as possible. The &lt;a href="http://www.codeplex.com/onequery"&gt;CodePlex
site&lt;/a&gt; has plenty of places to contribute, so please check it out!
&lt;/p&gt;
&lt;h5&gt;Sample Code in Text Form
&lt;/h5&gt;
&lt;pre class="csharpcode"&gt;IDataReader rdr = Product.Query()
                         .Where(Product.ListPrice &amp;gt; 10 &amp;amp; Product.ListPrice &amp;lt; 100)
                         .OrderBy(Product.ListPrice, SortDirection.Descending)
                         .ExecuteReader();&lt;/pre&gt;
&lt;p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:72e27e42-13fc-4276-bae4-35c1cd2093a5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Data%20Access" rel="tag"&gt;Data Access&lt;/a&gt;,&lt;a href="http://technorati.com/tags/C#" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/VB" rel="tag"&gt;VB&lt;/a&gt;,&lt;a href="http://technorati.com/tags/.Net" rel="tag"&gt;.Net&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Fluent%20Interface" rel="tag"&gt;Fluent
Interface&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ORM" rel="tag"&gt;ORM&lt;/a&gt;
&lt;/div&gt;
&gt;
&lt;img width="0" height="0" src="http://blog.andrewnurse.net/aggbug.ashx?id=909ac400-c7d1-4d5f-912e-a74b63555408" /&gt;</description>
      <comments>http://blog.andrewnurse.net/CommentView,guid,909ac400-c7d1-4d5f-912e-a74b63555408.aspx</comments>
      <category>OneQuery</category>
    </item>
  </channel>
</rss>