SQLite Persist

I recently discovered SQLite and had an immediate appreciation of its single-file-based database system that still supported SQL.  This morning, I thought I would try to start using it on a redo of an old project, but was slightly discouraged by the idea of writing a bunch of DACs or making another adaptation of SQLHelper for this new data provider.

What I had in my head was, “I just want to send an object to a method and have it saved.  And I want to be able to load an object by an ID value or be able to create a bunch of objects with some criteria.”  Basically, what I wanted was to eliminate the specific Load and Save methods on my classes, which are always the biggest headache for me when writing.

This kind of stuff would be a piece of cake with XML serialization.  Just serialize the object and stick it in a table.  But then you lose the flexibility of querying, or at least, it gets really difficult to implement.  Additionally, Serialization works with the public properties of a class, which could have logic and might be read-only.  Instead, I chose to use Reflection on the private fields of the class.

One other goal I wanted was “zero-configuration”.  You just use it.  The result is that the database is named after your assembly name, database tables are named after your class names, and database fields are named after your private variables in your classes.  You don’t even have to think about it.