Often times, one would find himself in a situation, where you need to temporarily store some data, but don’t wan’t the hassle of configuring a database, a username, etc for it. Maybe even more so, if your development box does not contain SQL Server express or similar database. You can of course use SqlCE, SQLite, BerkeleyDB or something else if your needs are beyond simple prototyping storage. SqlCE, SQLite and BerkeleyDB offer familiar querying interface using SQL to manipulate & query data, whereas PersistentDictionary offers what the name suggests. A persistent (transactions included!) IDictionary<k,v> implementation.
PersistentDictionary implementation is based on Esent, which is a not well-known embedded database included in all major windows version (XP, Vista, Windows Server, etc). From the Microsoft Windows SDK blog:
ESENT is used by the Active Directory, Windows Desktop Search, Windows Mail and several other Windows services and a slightly modified version of the code is used by Microsoft Exchange to store all its mailbox data. The ESENT API is available through the SDK and can be used on all versions of Windows from Windows Server 2000 on up
There is a managed wrapper for Esent, part of which is also an implementation of PersistentDictionary. Out-of-the box, the PersistentDictionary implementation only stores simple structures and primitive data types, but it is very easy to modify it to support also classes, which are marked with the [Serializable] attribute. One just needs to change the ColumnConverter class. For your convenience, here is the compiled version of EsentCollections, which enable just that. The usage is fairly simple.
var store = new PersistentDictionary<Guid, MyClass>("test-database");
// save an instance of your class
store[Guid.NewGuid()] = new MyClass { PropertyA = "SomeValue" };
// to retrieve and display all elements, you can
foreach(var key in store.Keys) {
var retrievedInstance = store[key];
// now do something with this instance
Console.WriteLine(retrievedInstance.PropertyA);
}
You have to ensure, though, that your custom class is marked for serialization. You can achieve this like so:
[Serializable]
public class MyClass {
public string PropertyA { get; set; }
}
You can see that it is very easy to implement simple repositories on top of that — for prototyping needs, where the entities are constantly evolving and changing.
Do note, however, that Esent is not limited to PersistentDictionary. You can query it in different ways, add indexes to it, have many columns, etc. The thing is used by large-scale software like Exchange, nontheless.
I will post a simple example of IRepository implementation shortly. Stay tuned!