Answer the question
In order to leave comments, you need to log in
NHibernate: why is ISession not working after GenericADOException?
Class for working with domain entities:
public class Repository<T> : IDisposable
where T : BaseEntity
{
private readonly ISession _session; // сессия открыта пока существует репозиторий
public Repository()
{
_session = Db.OpenSession();
}
public T Get(int id)
{
return Transaction(() => _session.Get<T>(id));
}
public IList<T> GetAll()
{
return Transaction(() => _session.CreateCriteria<T>().List<T>());
}
public int Add(T entity)
{
return Transaction(() => (int) _session.Save(entity));
}
public void Remove(T entity)
{
Transaction(() => _session.Delete(entity));
}
public bool Remove(int id)
{
var query = string.Format("from {0} where Id = {1}", typeof (T).Name, id);
return Transaction(() => _session.Delete(query)) == 1;
}
private void Transaction(Action command)
{
using (var transaction = _session.BeginTransaction())
{
command();
transaction.Commit();
}
}
private TOut Transaction<TOut>(Func<TOut> command)
{
using (var transaction = _session.BeginTransaction())
{
var result = command();
transaction.Commit();
return result;
}
}
public void Dispose()
{
_session.Close();
}
}
[Test]
public void EntityRepository()
{
using (var repository = new Repository<Entity>())
{
var time = DateTime.UtcNow;
var entity = new Entity(time);
var badEntity = new Entity(time); // сущность с таким же уникальным полем
var id = repository.Add(entity);
Assert.AreNotEqual(default(int), entity.Id); // success (поле ID присвоено)
var get = repository.Get(id);
Assert.AreEqual(entity, get); // success (в рамках одной сессии это будет выполняться)
Assert.Throws<GenericADOException>(() => repository.Add(badEntity)); // success (при добавлении сущности с таким же значением уникального поля выпало исключение)
var isRemoved = repository.Remove(id); // NHibernate.AssertionFailure : null id in ... (больше с сессией работать невозможно)
Assert.IsTrue(isRemoved);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question