Introducing MuDB

As a quick proof-of-concept, I decided to see how far I could go in designing an embedded, code-first database that can be used with my Netduino. Enter Mu Database, a simple object-oriented database to help you manage your data. Begin overview-by-example!

The example included with the source is for use with Secret Lab's Netduino, and also requires MicroLinq. After creating a new Netduino application and importing the required references, define your table's model with a simple class:

[Serializable]
public class Product  
{
    public int Id;
    public string Name;
    public float Cost;

    public Product(int id, string name, float cost)
    {
        this.Id = id;
        this.Name = name;
        this.Cost = cost;
    }

    public override string ToString()
    {
        return string.Format(
            "Id:{0} Name:{1} Cost:${2}", 
            this.Id, this.Name this.Cost
        );
    }
}

In MuDB, public fields correspond to table columns. Notice that the class is also marked Serializable. To start the process, create a new instance of MuDB and insert a new table into it by passing the type of our model (Product):

Database db = new Database(null);  
var Products = db.Insert(typeof(Product));  

Inserting records into the table:

Products.Insert(new Product(1, "Apple", 1.50f));  
Products.Insert(new Product(2, "Orange", 1.75f));  
Products.Insert(new Product(3, "Banana", 1.30f));  

Note that attempting to create two tables of the same type, or trying to insert a different type will result in a DatabaseException. After inserting our data, we can query it using MicroLinq:

    var product = Products.Data.FirstOrDefault(p => ((Product)p).Name == "Apple");
    Debug.Print(product.ToString());

Which yields in debugger: Id:1, Name:Apple, Cost:$1.5

So there are a few issues right now, like that it takes up a whole bunch o' memory. You can also serialize/deserialize tables with a caveat: it's not supported by the Netduino firmware. Still, it's not bad for a few hours of coding.

You can grab the source at Codeplex. If you use it, let me know how you went.