CoolStorage can be used with MonoTouch, without any loss of functionality. At the time of writing, CoolStorage is the only full-featured ORM library for MonoTouch available.
CoolStorage uses Reflection.Emit for creating concrete classes based on abstract mapping classes at runtime. Sadly, this is not supported in MonoTouch because of restrictions imposed by the iPhone OS. This means that mapping classes can't be declared abstract like this:
// Will not work on MonoTouch [MapTo("customer")] public abstract class Customer : CSObject<Customer,int> { public abstract int CustomerID { get; } public abstract string Name { get; set; } public abstract string City { get; set; } }
Instead, mapping classes have to be declared as a concrete class, with implementation for getters and setters:
// MonoTouch version of mapping class [MapTo("customer")] public class Customer : CSObject<Customer,int> { public int CustomerID { get { return (int)GetField("CustomerID"); } } public string Name { get { return (string)GetField("Name"); } set { SetField("Name",value); } } public string City { get { return (string)GetField("City"); } set { SetField("City",value); } } }
The rule is that you should call GetField() and SetField() in the getters and setters, and use the property name as the name parameter (do NOT use the DB column name). The same rule applies to getters and setters for properties mapped to relations.
CoolStorage for MonoTouch only supports the SQLite database. This is pretty obvious, because SQLite is the only supported relational database on the iPhone.
string dbName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "mydb.db3"); // The following line will tell CoolStorage where the database is, // create it if it does not exist, and call a delegate which // creates the necessary tables (only if the database file was // created new) CSConfig.SetDB(dbName, true, () => { CSDatabase.ExecuteNonQuery(@"CREATE TABLE person (PersonID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT(50) NOT NULL, DateOfBirth TEXT(30) NULL)"); }); // Do your thing
From here on, you can use all features of CoolStorage. The walkthrough is the best place to start. It briefly explains how CoolStorage works.