Mean Fiddler: REST-style query service
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.]