Rob West


Published 1 Oct 2014

For quick projects I wanted a lightweight persistence solution that had no reliance on a relational database. There are options out there like RavenDB but I wanted something even quicker so I created SimpleDatastore. It uses attributes on domain entities to control persistence and stores the data in XML or JSON files and is available on NuGet.

Getting Started

In order to use SimpleDatastore to persist your classes you simply need to apply DataMember attributes to those properties you wish to be persisted and entities need to inherit from PersistentObject. This gives them a Guid property called Id which is used as the primary key.

The repository pattern is used for data access using the IRepository<> interface. This provides the following operations:

The LoadList() method applies sorting which you can control by applying the IComparable<> interface. The LoadListUnsorted() will output the entities in the order they are stored in the XML file.

void Delete(Guid id);
T Load(Guid id);
IList<T> LoadList();
IList<T> LoadListUnsorted();
void Save(T instance);

The implementation of this interface is handled by the BaseRepository<> class. This can be initialised using an IoC container. Here is an example using StructureMap:


The only other step is to give SimpleDatastore its configuration. This controls caching, dependency resolution and the storage location. Use the default as follows:


Note that the DefaultConfiguration class requires a IDependencyResolver which allows you to have entities which themselves have dependencies.

With these simple steps you are now good to go. Data is persisted in XML files on a per entity basis and the files are stored in the App_Data folder, although this is configurable.

Richer Entities

SimpleDatastore allows you to create properties which are themselves entities. When you do this it stores the identifier for the child entity on the record and will then retrieve it when the parent entity is loaded. At present there is no lazy loading although I may look to add this feature. Given that it is aimed at simple projects I think this is good enough for now.

There is no need for any additional annotation on child entities, the system will handle anything that inherits from PersistentObject.

© 2023 Rob West. All Rights Reserved. Built using Kontent and Gatsby.