Object-relational mapping over Amazon SimpleDB using LightSpeed

One of the features we’re adding in LightSpeed 2.2 is support for Amazon SimpleDB, and an early version of this is already in the nightly builds. Here’s a preview of how to use it and what you can do with it.

First, some background. LightSpeed pretends that SimpleDB is a relational database, treating SimpleDB domains as tables and pretending that all items in the domain have the same set of attributes. LightSpeed also assumes, again on the relational model, that an item attribute has at most one value. (We’ll be looking at supporting multiple values for an attribute, but probably not in the 2.2 timeframe.) So all the familiar LightSpeed conventions apply: entity types map to domains, associations are expressed by references to IDs (item names) in other tables, and so on.

Consequently, most of the additional LightSpeed magic also just works: for example, you can do single table inheritance using a discriminator attribute or many-to-many associations using a through-table and a ThroughAssociation, you can make full use of the validation functionality, and you can query SimpleDB using LINQ. (There are some things we can’t do because of limitations in the SimpleDB platform: for example, there’s no eager loading and no querying across tables.)

Let’s get started. If you’re using the designer, you can use SimpleDB Management Tools to drag and drop domains from Server Explorer. The free version will do, or if you’re an Enterprise customer, the retail edition is included with your Enterprise licence. Note that SimpleDB, unlike a relational database, doesn’t have “columns” independent of values to go in them, so if you don’t have existing data, you’ll get an empty entity and will need to add properties by hand. But if you do have existing data, the designer will create the entity properties for you.

LightSpeed model from SimpleDB

(In this example I’ve changed the entity Identity Type from the default String to Guid. This automates assigning item names to new items, at the expense of not being able to work with arbitrary item names.)

You can now work with this just as with any other LightSpeed model:

LightSpeedContext<DemoUnitOfWork> context = new LightSpeedContext<DemoUnitOfWork>
{
  DataProvider = DataProvider.AmazonSimpleDB,
  ConnectionString = "Access Key=...;Secret Access Key=...",
  IdentityMethod = IdentityMethod.Guid
};
 
using (DemoUnitOfWork unitOfWork = context.CreateUnitOfWork())
{
  IList<Game> games = unitOfWork.Find<Game>(Entity.Attribute("Price") < 30.00m);
 
  foreach (Game game in games)
  {
    Console.WriteLine("{0} by {1}: {2:C}", game.Name, game.Designer.Name, game.Price);
  }
 
  Game carcassonne = new Game
  {
    Name = "Carcassonne",
    Price = 20.95m,
    GameType = "1",
    OnlinePlayAvailable = true,
    Designer = new Person { Name = "Klaus-Jurgen Wrede" }
  };
 
  unitOfWork.Add(carcassonne);
  unitOfWork.SaveChanges();
}

As you can see, you can use the normal LightSpeed query API, you can traverse and create associations, and so on. You can also use LINQ to perform the query:

var games = from g in unitOfWork.Games
            where g.Price < 30.00m
            select g;

As a final demonstration, here’s a slightly more advanced model, using single table inheritance to store multiple entity types in a single domain:

LightSpeed model for single table inheritance over SimpleDB

The Game domain now contains both BoardGames and VideoGames, but LightSpeed inheritance takes care of distinguishing the two and materialising them using the correct types:

foreach (BoardGame game in unitOfWork.BoardGames.Where(g => g.Price < 30m))
{
  // VideoGames don't have a Designer, but the query returns only BoardGames
  Console.WriteLine("{0} by {1}: {2:C}", game.Name, game.Designer.Name, game.Price);
}

This demonstrates how LightSpeed adds significant capabilities over the raw SimpleDB API: not just an object-oriented wrapper around the data, but a way to abstract out common patterns and conventions from the relational world and apply them in the SimpleDB environment.

Want to try it out yourself? Get the latest LightSpeed nightly build — the free version is here — and optionally SimpleDB Management Tools and you’re ready to go!

kick it on DotNetKicks.com

4 Responses to “Object-relational mapping over Amazon SimpleDB using LightSpeed”

Archives

Join our mailer

You should join our newsletter! Sent monthly:

Back to Top