Tokyo is giving me a headache already.. and I haven’t even left !
Least painful way to build a DAL with Monotouch
Data access in monotouch (C# for iphone) is a royal pain in the ass. I know most developers, whether C# or objc, are just stuffing SQL statements directly into their code but is one of my pet hates. Consequently I’ve been looking for a way around it.
As it turns out there’s no nice solution but there’s a half way solution that sucks a bit less.
It’s all Apple’s fault
The iPhone ships with sqlite (good) but it is unfortunately sqlite v3.0 (bad) which lacks the ability to expose schema information (v. bad). Consequently, schema querying which most ORM’s rely on doesn’t work.
Things that don’t work
- NHibernate - reflection at runtime, it just can’t work
- Datasets backed by an sqlite ADO.NET 2 provider, ie System.Data.SQLite - won’t work for several reasons but mainly System.Data.SQLite makes runtime schema queries, which fails
- catnap-orm - this really had me excited. A nice simple but fully fledged ORM for monotouch. The author developed it specifically for iPhone+monotouch, paid Novell his license fee and then discovered it doesn’t currently work on the device due to bugs in the MONO framework. Novell are currently ignoring the problems - more people need to winge on the mailing lists to motivate them. I know I will be.
- MyGeneration code generator + doodads sqlite - this one was a long shot. MyGeneration is a template code generator, and doodads one of the DAL code architectures it can spit out. Unfortunately under the hood it too makes calls to runtime schema queries on the database, leading to failure
At this point I really wanted to put my foot thru my iMac’s screen and snap my iPhone in half. No, really.
Salvation
One I came across early but dismissed was sqlite-net. It’s a very thin wrapper over sqlite, provides basic class mapping but still requires SQL to be embedded in your app. It provides no assistance performing updates. The upside is it handles the task of transferring results into your DTO’s and auto-updating primary keys post insert.
It’s not ideal but it’s still a better than nothing. Personally I really hope Novell address the issues holding back catnap-orm because it is awesome.
I hope this post saves some poor developer their sanity.






