Skip to content

Instantly share code, notes, and snippets.

@dealproc
Created March 17, 2019 07:19
Show Gist options
  • Save dealproc/06eeecaba8731c48f7e3dce44b73fb25 to your computer and use it in GitHub Desktop.
Save dealproc/06eeecaba8731c48f7e3dce44b73fb25 to your computer and use it in GitHub Desktop.
Multi-Tenant Solutions - Repository<T>.cs
public abstract class Repository<T> : IRepository<T> {
protected readonly ISessionSource _SessionSource;
public Repository(ISessionSource sessionSource) {
if (sessionSource == null) {
throw new ArgumentNullException("sessionFactory");
}
_SessionSource = sessionSource;
}
public override Customer Get(int id) {
using (var session = _SessionSource.CreateSession()) {
var query = session.QueryOver<Customer>()
.Where(x => x.Id == id)
.Future();
SetupSession(session);
return query.Single();
}
}
public override Customer GetFirst(Expression<Func<Customer, bool>> condition) {
using (var session = _SessionSource.CreateSession()) {
var query = session.QueryOver<Customer>()
.Where(condition)
.Future();
SetupSession(session);
return query.FirstOrDefault();
}
}
public override void SetupSession(ISession session) {
using (var session = _SessionSource.CreateSession()) {
var addresses = session.QueryOver<Customer>()
.Fetch(x => x.Addresses).Eager
.Future();
//other code removed to keep this somewhat brief
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment