Mean Fiddler: REST-style query service

Published 10 May 07 02:36 AM | andersnoras 

Some days ago Ayende wrote Bumbler, a Microsoft Jasper clone, in two hours. In a comment to the post Andres Aguiar asked how long it would take to do a Microsoft Astoria clone as well. Ayende declined to do this, so we were left with the big question; "How long will it take?"
Today I decided to sit down and write a service similar to Astoria and it took me a good four hours (I guess Ayende would have done it in two). The service does not support all of the Astoria features; you cannot do any POST or PUT operations to persist entities and only XML marshalling is supported. Ordering and top / skip operations aren't supported either. These and other marshalling schemes such as JSON should be fairly simple to add. If you feel like it, feel free to adopt the code and develop it further.
A key improvement from the Astoria model is that you don't need to have any mappings defined beforehand. To expose a service, simply add the Mean Fiddler HTTP handler to your web.config file as shown below:

<httpHandlers> 
 <add verb="*" path="*.fiddle" 
type="Noras.MeanFiddler.MeanFiddlerHandler, Noras.MeanFiddler" /> </httpHandlers>

You can query any database that is registered in the connectionStrings section in your web.config file. For my examples I've used the Northwind database.

<connectionStrings>
  <add name="Northwind" 
       connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"/>
</connectionStrings>

When you run the application, the Northwind database will be exposed on the http://localhost/Northwind.fiddle URL. If you navigate to this URL you'll get a list of the entities in the database.

<MeanFiddler>
  <NorthwindEntities uri=".">
    <Regions href="Regions" />
    <Categories href="Categories" />
    <CustomerDemographics href="CustomerDemographics" />
    <Territories href="Territories" />
    <Suppliers href="Suppliers" />
    <Products href="Products" />
    <Orders href="Orders" />
    <Shippers href="Shippers" />
    <Customers href="Customers" />
    <Employees href="Employees" />
  </NorthwindEntities>
</MeanFiddler>

Each entity gets its own URL, for instance you can get hold of the customers by navigating to http://localhost/Northwind.fiddle/Customers

<MeanFiddler>
  <Customers>
    <Customer href="Customers[ALFKI]">
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria Anders</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Berlin</City>
    <Region />
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>
    <Orders href="Customers[ALFKI]/Orders"/> 
</Customer> <Customer href="Customers[ANATR]"> <CompanyName>Ana Trujillo Emparedados y helados</CompanyName> <ContactName>Ana Trujillo</ContactName> <ContactTitle>Owner</ContactTitle> <Address>Avda. de la Constitución 2222</Address> <City>México D.F.</City> <Region /> <PostalCode>05021</PostalCode> <Country>Mexico</Country> <Phone>(5) 555-4729</Phone> <Fax>(5) 555-3745</Fax>
<Orders href="Customers[ALFKI]/Orders"/>
</Customer> <!-- Other Customers eluded for brevity --> </Customers> </MeanFiddler>

As you can see, each entity has a href attribute which you can navigate to to retrieve that sole entity. "Alfreds Futterkiste" lives at http://localhost/Northwind.fiddle/Customers[ALFKI]. To get all of the orders for this "ALFKI" just go to http://localhost/Northwind.fiddle/Customers[ALFKI]/Orders. You can also do more advanced queries, to retrieve all the discontinued products, and their categories, with more than five items in stock navigate to http://localhost/Northwind.fiddle/Categories/Products[Discontinued eq true and UnitsInStock gt 5].

I'm uncertain of whether the behavior of the Mean Fiddler is the same as of the Astoria data service. I haven't installed any of the Mix-bits, so I've based my service entirely on the usage documentation for Astoria.

One question that can up when Ayende did Bumbler was why it had that name. I guess some of you are scratching your head, trying to find out why this one is called Mean Fiddler?
The London Astoria is a famous music venue in Soho. Right next to the Astoria there is another venue called Mean Fiddler. In 2000, the Mean Fiddler Music Group acquired the lease for Astoria - so you can say that the Mean Fiddler runs things. :-)

The code can be downloaded here. Keep in mind that since this was written in four hours, the code quality isn't very good, but then again this isn't meant to show of good coding practices, its to show how easy a service that resembles Microsoft Astoria can be developed with NHibernate.

[Update: I've fixed the issue pointed out by Diego. The fix isn't the most elegant, but it works. If you're interested go grab the updated code.]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Ayende @ Rahien said on May 10, 2007 12:37 PM:

Mean Fiddler: REST-style query service for NHibernate

# Jafin said on May 10, 2007 4:41 PM:

You guys Rock!

# Diego said on May 11, 2007 8:08 AM:

Cool! You forgot to include an href in each customer pointing to its orders.

# andersnoras said on May 11, 2007 11:47 AM:

@Diego;

Thank you for pointing this out. The reason for this is that Bumbler doesn't generate inverse relations. I wrote a patch for this, but I reverted it because it caused some cartesian product issues. Still it should be fairly simple to include a link to the orders for a customer, after all the link generated by Astoria (<Orders href="Customers[ALFKI]/Orders" />) works perfectly with Mean Fiddler.

I'll post an update as soon as I can find the time to solve this problem.

# andersnoras said on May 11, 2007 1:12 PM:

The Customer/Order link issue has been fixed.

# D Rapp said on June 14, 2007 7:18 AM:

I've been eyeing this post since it came out and I was wondering if anyone has taken interest in further developing Mean Fiddler?  I think it would be great if we could develop a viable release before Microsoft.  Where I see Mean Fiddler having the most potential is as a new driver and dialect for NHibernate.  How great of a world would it be if you can have NHibernate on your server talking to your database and NHibernate on your smart clients talking to a Mean Fiddler service?  

# andersnoras said on June 14, 2007 11:57 AM:

@D Rapp;

As far as I know, no one has adopted the project. Since any open source project requires a community and leadership to thrive I did not really expect this to happen either. Even if it is usable, I consider the Mean Fiddler as a demo rather than a framework. Still the bits I have published can be used to build a more robust competitor to Astoria.

If you want to develop it further, feel free to mail or IM me if there is anything I can help you out with.

You can reach me at anders.noras<the at sign>andersnoras.com (Email and Windows Live messenger).

# Anders Norås' Blog said on July 13, 2007 5:29 AM:

A couple of months back I wrote a small demo showing how core features of Microsoft's Astoria could be

# Anders Norås' Blog said on July 19, 2007 3:24 AM:

A couple of days ago, I blogged about Derrick taking over the Mean Fiddler demo to turn it into a real

# Anders Norås' Blog said on January 30, 2008 12:52 PM:

Today it is exactly one year since I pick up on blogging after a long break. To celebrate, I&#8217;ll

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below