Created
March 17, 2019 07:19
-
-
Save dealproc/06eeecaba8731c48f7e3dce44b73fb25 to your computer and use it in GitHub Desktop.
Multi-Tenant Solutions - Repository<T>.cs
This file contains 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 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