NWorkspace, basic API

OK, if you're going to get a feeling of what I'm talking about when I refer to NWorkspace here I think we need some examples. So, I'd like to show some code for what it could look like when working with NWorkspace from a consumer perspective. If you have seen Valhalla before, you'll probably feel pretty familiar with the basic API.

Please note that I'm leaving out repositories here so as not to distract you from the NWorkspace API. Of course, repositories aren't a necessity, they are just something I happen to like.

For the sake of the example, imagine an adapter implementation of NWorkspace called NWorkspaceNH which transforms from NWorkspace to NHibernate. OK, let's get going.

//--Instantiation of workspace.
//Note that _sessionFactory is an ISessionFactory from
//NHibernate and has been set up before the following line...
//Also note that this is the only variation depending upon
//the adapter implementation.
private IWorkspace _ws = new WorkspaceNH(_sessionFactory);

//--Insert of an instance.
Customer c = new Customer();
//...Do stuff with customer...
//Add() below means "associate" this instance with the Unit of
//Work and decide if INSERT or UPDATE is needed.

//--Reconstitution of an instance.
Customer c = (Customer)_ws.GetById(typeof(Customer), 42);

//--Update of a customer.
//...Do stuff with customer...
//Associate if unknown to Unit of Work…

//--Explicit start of transaction.
//IWorkspace.PersistAll() is always wrapped in a tx,
//but if you want to hold on to readlocks until PersistAll()...

I skipped the querying part as this blog post is already too long as it is. You'll find the querying API proposal in the next blog post in a few days' time.