myelin: DbWrappers

Convenient object persistence for .NET

DbWrappers is a code generator for ADO.NET database access.  It takes an XML file that decribes the tables in your database, and spits out a whole heap of C# classes that make your life as a database programmer much easier.

It's free and open source.  Get started now!

Huh?

For some reason, the ADO.NET DataSet class, which everybody else seems to love, just doesn't work very well for me.  C# is a great language; it has many of the features that make Python programming fun, as well as two nice extras: it has a great IDE, and it checks types at compile time, which means I can be much lazier than with Python.  However, when you cast something to an object (as you do for values stored in a DataSet) you lose IntelliSense, which really takes the fun out of things.

Also, DataSets seem to assume that you want to cache the entire table in memory, which often isn't true; if you only want to iterate over the rows, you have to use a DataReader.  If you want to write an app which queries against a fairly big table, you don't get your SQL generated automatically (at least I can't find out how :)  ADO.NET doesn't seem to have a convenient shortcut that guesses types for you - something like this Perl code might do:

	$dbh->do( 'INSERT INTO tblExample (id, title, text) VALUES(?, ?, ?)',
		undef, $id, $title, $text
		);

Using DbWrappers, it's even easier:

	ExampleRow newRow = examples.NewRow();
	newRow.Id = id;
	newRow.Title = title;
	newrow.Text = text;
	newRow.Commit();

However, row objects aren't confined to queries.  You can do a query on a table and populate an ArrayList:

	ArrayList queryResults = new ArrayList();
	using ( ExampleReader r = examples.SelectMany( null, null ) )
	{
		while ( r.Read() )
		{
			queryResults.Add( r.Row );
		}
	}

... and then, later on in the program, change an element and drop it back in the database, all without typing any SQL:

	ExampleRow row = (ExampleRow) queryResults[ n ];
	row.Text = "Here is some new text";
	row.Commit();

Much easier.

Where do I get it?

Right here.  It's just one file: drop it in a directory somewhere, create a file called Tables.xml (here's an example) in the same directory, and run it.  For each table defined in Tables.xml, it will create three files:

Include these files in your project, recompile, and you should have access to a whole heap of very handy objects.

Compatibility

DbWrappers is designed to work with any of the ADO.NET providers.  I use it with OLE DB to provide convenient persistent storage for desktop apps, but should just as easily be able to use it with MySQL, MS SQL Server or anything else that has an ADO.NET driver (*Connection, *Command, *DataReader objects).

It's written in Python, so you can run it on just about any OS you like.  Download it here.  You'll need at least version 2.2.