This blog is where I post about my consulting work with Microsoft Technologies, and other random tidbits that don't fit in my Photo Blog or my Iraq Blog.

Monday, June 27, 2011

ScrappyDB: Code First + Linq for Amazon SimpleDB

For several years I've been working on ScrappyDB, which is my personal open source .NET library for Amazon SimpleDB.

I quietly released version 1.0 on CodePlex (http://scrappydb.codeplex.com/) several years ago, and despite a few downloads I'm not aware of a single production user (other than me). Not to worry... no hard feelings... I wrote this for my own use and released it in hopes that it "might" prove useful for somebody else someday...

This month I released version 2.0 to Nuget (http://nuget.org/List/Packages/ScrappyDB) with slightly higher expectations: "ScrappyDB 2.0 is a code first style object mapping library and Linq provider for Amazon SimpleDB. Also included is an ASP.NET Membership Provider for Amazon SimpleDB."

With support for "Entity Framework style" code first syntax and Linq I'm hopeful that a few other people might actually find this useful. But nobody is going to use it if they can't find it and so I'm taking it upon myself to start writing a few blog posts demonstrating how it works, and eventually to highlight how easy it is to use Amazon SimpleDB as a backend for .NET websites.

To get started here are a few code examples that demonstrate using ScrappyDB to run the example queries found in the excellent SimpleDB Query 101 document from Amazon:

Following EF Code First conventions we create a class to define our schema and then a context class to access the database. The only syntactic difference between EF and ScrappyDB at this point is "SdbContext" in place of "DbContext".
public class AwsSampleContext : SdbContext
public SdbSet Books { get; set; }

public class Book
public string Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Year { get; set; }
public int YearInt { get; set; }
public int NumberOfPages { get; set; }
public List Keywords { get; set; }
public List Rating { get; set; }

To execute the sample query:
select * from mydomain where Title = 'The Right Stuff'
All we would need to do is:
var dbContext = new AwsSampleContext();
var result = from a in dbContext.Books where a.Title == "The Right Stuff" select a;
SimpleDB only supports one datatype which is a string. This means no native numeric types, no datetime. ScrappyDB supports strongly typed data for a subset of common .NET types.

Linq was designed generically and doesn't always map well to underlying database features and implementation oddities. This is particularly true with SimpleDB, where the "simple" feature set can't support many concepts that are typical in Linq to SQL.

Here is another sample query from the Query 101 document, and several different ways to approach implmenting it in ScrappyDB:
select * from mydomain where Year > '1985'
The .NET centric way to implement this in ScrappyDB would be to define the Year field as a numeric type, in which case the Linq query would be as you might expect from Linq to SQL:
var dbContext = new AwsSampleContext();
var result = from a in dbContext.Books where a.YearInt > 1985 select a;
If you defined the field "Year" as a string in your .NET class then you will need to use the ScrappyDB specific extension method "GreaterThan" to implement this query:
var dbContext = new AwsSampleContext();
var result = from a in dbContext.Books where a.Year.GreaterThan("1985") select a;
If you would like to see more you can check out the source code on BitBucket (https://bitbucket.org/scrappydog/scrappydb). The Integration Tests in the source implement almost all the examples from the Query 101 document.

To see ScrappyDB in a Sample MVC 3 application check out the sample code on BitBucket (https://bitbucket.org/scrappydog/scrappydb.mvcsample) or check out the sample application running on AppHarbor (http://sample.scrappydb.com/).

Note: AppHarbor runs on Amazon EC2, and so it is a GREAT place to host applications that use SimpleDB as a backend!

The easiest way to try ScrappyDB for yourself is to add it into your Visual Studio project with Nuget (http://nuget.org/List/Packages/ScrappyDB).