Archive for October, 2010

A ‘cool’ ORM goes Mono

October 29, 2010

I have recently started using this simple & elegant ORM called CoolStorage, written by the people from The Vici Project.

What makes CoolStorage great is that it’s been tweaked for MonoTouch and SQLite. All you need to do to start using it is,

  • download the assemblies that have been build for Mono on the Mac
  • reference them in you MonoTouch projects
  • add a few lines of startup/connection code

Once you’ve pointed CoolStorage at your SQLite database, the last piece of the puzzle is (well you may have guessed, as this is an ORM) to create some C# classes that map to you table structure. What I love is the lack of attribute annotation that you need, here is an example,

public class Cups : Vici.CoolStorage.CSObject<Cups, int>
public int ID { get { return (int)GetField(“ID”); } }

// This one maps automatically
public string Type { get { return (string)GetField(“Type”); } set { SetField(“Type”,value); } }

public string Desc { get { return (string)GetField(“Desc”); } set { SetField(“Desc”,value); } }

public int Size { get { return (int)GetField(“Size”); } set { SetField(“Size”,value); } }

Now when you want to create and save a new cup,

Cups newCup = Cups.New();
newCup.Type = “Glass”;
newCup.Desc = “A cup for juice”;
newCup.Size = 250; // ml

Anyway you get the idea. There is much more to learn so make sure you head over to their website!

Last thing to mention. The Novell team have been working feverishly on getting Mono on the Android platform; I’m sure you’ve heard by now about MonoDroid. Well I’ve by messing around with it in its Beta form on Windows/Visual Studio 2010. Since SQLite is the database of choice for this platform, I figured that I would try to get CoolStorage work there too.

It’s not too difficult as it turns out, here are the steps,

  • I created two MonoDroid library projects in VS 2010, one for vici.core.dll & one for vici.coolstorage.dll
  • Downloaded the code for vici.core & vici.coolstorage, see The Vici Project website
  • Added the current source code into the respective projects.
  • Removed the database drivers that come standard in vici.coolstorage, like Access & SQL Server
  • Added the database driver for SQLlite on MonoTouch (CSConfig.cs & CSDataProviderSQLite.cs)
  • Included an assembly reference to the Mono.Data.Sqlite DLL
  • Included a compilation symbol (MONOTOUCH) to both projects
  • Build & you’re done!

Now you can include these MonoDroid assembly versions of CoolStorage into your MonoDroid apps.

There is only one Gotcha! Took me a while to figure it out, but you need to make sure that your MonoDroid app is targeting 2.2 of the Android OS. It seems that previous versions of SQLite on Android didn’t support many of the needed functionality.

Anyway enjoy!