This post has been imported from my previous blog. I did my best to parse XML properly, but it might have some errors.
If you find one, send a Pull Request.
You can find a very nice description of Unit of Work in here. It describes a mythological artifact, which can understand what you’re doing with object retrieved from a database and allows you easily persist the whole state back to the database. Typically, clients for NoSQL databases do not provide this high abstraction of the persistence. Deiphobus makes a difference, providing a well known from other object mappers, the mighty unit of work, called Session. Below you can find how simple example of the session usage is:
// sessionFactory created earlier from a configuration
using (var s = sessionFactory.Open())
{
// entity creation makes it tracked, the state will be saved at s.Flush();
var user = s.Create<IUser>(t =>
{
t.Email = "[email protected]";
t.AllowMarketingEmails = true;
});
var secret = s.Create<IPersonalInfo>(t =>
{
t.Type = PersonalInfoType.SecretFact;
t.Body = "I dislike SQL paradigm";
});
var quote = s.Create<IPersonalInfo>(t =>
{
t.Type = PersonalInfoType.Quote;
t.Body = "To be SQL or not to be";
});
user.Infos.Add(secret);
user.Infos.Add(quote );
// Flush to persist all changes at once! Dirty checks, etc. are handled without your coding!
s.Flush();
}
It is simple and not dealing with checking what should be saved. Just create a session and call Flush when you want your changes be persisted. Of course it isn’t transactional as the database Cassandra isn’t, but still, it puts a nice layer over the standard Thrift protocol.