Problems with Delete statement in NHRepository

3 views
Skip to first unread message

Action Jackson

unread,
Sep 24, 2009, 11:22:38 PM9/24/09
to ncommon
I'm having issues trying to test the Delete method in the NHRepository
class. I'm using SQLLite and I'm first inserting a couple of entities
and verifying the count, which works perfectly. When I try to delete,
though, the delete statement isn't even being ran according to the
results in NHibernateProfiler. However, if I try to delete without
using NCommon's UnitOfWorkScope, but rather just using NHibernate's
basic constructs, things work perfectly. Here's my code:

[Test]
public void CanDeletePersonObjects()
{
var person1 = new Person("Johnny", "Walker");
var person2 = new Person("Chris", "Jackson");
var repository = GetPersonRepository();

using (var scope = new UnitOfWorkScope
(IsolationLevel.ReadCommitted,
UnitOfWorkScopeTransactionOptions.CreateNew))
{
repository.Add(person1);
repository.Add(person2);

scope.Commit();
}

Assert.That(repository.Count(), Is.EqualTo(2));

using (var scope = new UnitOfWorkScope())
{
// This next line doesn't seem to do anything
repository.Delete(person2);
scope.Commit();
}

Assert.That(repository.Count(), Is.EqualTo(1));
}

Any ideas? Thank for any advice.

Ritesh Rao

unread,
Sep 24, 2009, 11:41:52 PM9/24/09
to ncommon
Could you please post the implementation of GetPersonRepository()? I'd
like to create a test that would replicate your scenario as close as
possible.

Thanks,
Ritesh

Action Jackson

unread,
Sep 25, 2009, 12:29:42 AM9/25/09
to ncommon
Sure, it's a simple method:

IRepository<Person> GetPersonRepository()
{
return new NHRepository<Person>(_session);
}

The "_session" object is initialized in my Setup method (using NUnit)
and is set to the return value of CreateSession().

public ISession CreateSession()
{
var openSession = _factory.OpenSession();
(new SchemaExport(_configuration))
.Execute(true, true, false, openSession.Connection,
new StringWriter(new StringBuilder()));

return openSession;
}

I am using the same session throughout a test run due to the dynamics
of SQLite and the fact that the database will be "deleted" otherwise.
> >   Any ideas? Thank for any advice.- Hide quoted text -
>
> - Show quoted text -

Ritesh Rao

unread,
Sep 29, 2009, 11:03:28 PM9/29/09
to nco...@googlegroups.com
Sorry for the late response. The problem I see is the GetPersonRepository(). Since in GetPersonRepository you are manually specifying the session that the repository will use you are then responsible for the lifetime and management of the session. Even though the repository is being used within a UnitOfWorkScope, that doesn't matter because instead of using relying on the UnitOfWorkScope to manage and provide the ISession to use, the repository will use the private instance of ISession.

When relying on UnitOfWorkScope don't explicitly provide the ISession to the repository. That will then force the repository to resolve the session from the current running UnitOfWorkScope and allow the UnitOfWorkScope to manage the session. 

Let me know if that resolves the issue.

- Ritesh
Reply all
Reply to author
Forward
0 new messages