I'd like to find the difference in using these three ways of getting data from the database when doing something like:
public T GetById(int id) {
using (var db = Database.Session) {
using (var t = db.BeginTransaction()) {
try {
return db.Get<T>(id);
}
catch (Exception) {
if (!t.WasCommitted) {
t.Rollback();
}
throw;
}
}
}
}
or
public T GetById(int id) {
using (var db = Database.Session) {
using (var t = db.BeginTransaction()) {
try {
return Query<T>().First(x=>x.Id == id);
//or something like
//return Query<T>().Where(x=>x.Id == id).FirstOrDefault();
//or
//return QueryOver<T>().Where(x=>x.Id == id).FirstOrDefault;
}
catch (Exception) {
if (!t.WasCommitted) {
t.Rollback();
}
throw;
}
}
}
}
or even this way:
public T GetById(int id) {
using (var db = Database.Session) {
using (var t = db.BeginTransaction()) {
try {
return db.Load<T>(id);
}
catch (Exception) {
if (!t.WasCommitted) {
t.Rollback();
}
throw;
}
}
}
}
And adding another question to this one, how the Query()
differs from QueryOver()
?
I've read some answers here in stackoverflow but since the majority of them was about the starting of using Linq, and NHibernate 3, I'd like to know how is the scenario today.