Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created June 4, 2012 22:58
Show Gist options
  • Save jmarnold/2871352 to your computer and use it in GitHub Desktop.
Save jmarnold/2871352 to your computer and use it in GitHub Desktop.
RavenDb stuff
public interface ISessionBoundary : IDisposable
{
IDocumentSession Session();
bool WithOpenSession(Action<IDocumentSession> action);
void SaveChanges();
void Start();
void MakeReadOnly();
}
public class SessionBoundary : ISessionBoundary
{
private readonly IDocumentStore _store;
private Lazy<IDocumentSession> _session;
public SessionBoundary(IDocumentStore store)
{
_store = store;
reset();
}
private void reset()
{
Dispose();
_session = new Lazy<IDocumentSession>(_store.OpenSession);
}
public void Dispose()
{
WithOpenSession(s => s.Dispose());
}
public IDocumentSession Session()
{
return _session.Value;
}
public bool WithOpenSession(Action<IDocumentSession> action)
{
if (_session != null && _session.IsValueCreated)
{
action(_session.Value);
return true;
}
return false;
}
public void SaveChanges()
{
WithOpenSession(s => s.SaveChanges());
}
public void Start()
{
reset();
}
public void MakeReadOnly()
{
// this doesn't do anything yet
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment