Created
June 4, 2012 22:58
-
-
Save jmarnold/2871352 to your computer and use it in GitHub Desktop.
RavenDb stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ISessionBoundary : IDisposable | |
{ | |
IDocumentSession Session(); | |
bool WithOpenSession(Action<IDocumentSession> action); | |
void SaveChanges(); | |
void Start(); | |
void MakeReadOnly(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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