Introduction to MemoDb
I’m working on an internal framework on the company I work (it will be open source soon I expect to post a lot in the future about it) that needs the ability of mocking a repository with a memory database. Since this repository works with Linq, an implementation with a List inside was enough.
The time passed and we realize that it will be really usefull to have transactions in that repository (ok, not so usefull, but at least funny
). So I started to work on MemoDb.
The source code of the first version is in http://memodb.codeplex.com.
Let’s show some examples:
var memo = new Memo()
.Map<City>();
using (var s = memo.CreateSession())
{
s.Insert(new City
{
Id = 1,
Name = "Buenos Aires"
});
s.Insert(new City
{
Id = 2,
Name = "New York"
});
s.Insert(new City
{
Id = 3,
Name = "Montevideo"
});
s.Insert(new City
{
Id = 4,
Name = "Bogotá"
});
s.Flush();
s.Insert(new City
{
Id = 5,
Name = "Bangkok"
});
}
using (var s = memo.CreateSession())
{
var cities = s.Query<City>().Where(x => x.Name.StartsWith("B"));
Assert.AreEqual(2, cities.Count());
}
Note that it doesn’t store Bankgok.
var memo = new Memo()
.Map<City>();
var buenosAires = new City
{
Id = 1,
Name = "Buenos Aires"
};
using (var s = memo.CreateSession())
{
s.Insert(buenosAires);
s.Flush();
}
City buenosAires2;
using (var s = memo.CreateSession())
{
var test = s.GetById<City>(buenosAires.Id);
test.Name = "Bs As"; //Not flushed!
}
using (var s = memo.CreateSession())
{
buenosAires2 = s.GetById<City>(buenosAires.Id);
}
Assert.AreEqual("Buenos Aires", buenosAires2.Name);
Right now it supports:
- Inserts
- Updates of dirty objects
- Many-to-one relationships
- Queries
Next steps:
- Delete
- Collection management
- Id generation strategies
- Cascade
In future posts I will be talking about the implementation details.
Advertisement
