I'm using NHibernate in conjunction with Repository Design Pattern. In current task, I need to update an entity and also delete another entity in the same transaction. I though about declare ISession.BeginTransaction()
in Repository's Services
layter and pass it to Repository's method
as following:
public class SomeEntity
{
//entity's properties
}
public class EntityRepository : IEntityRepository
{
public void update(ISession session, Entity entity)
{
session.Update(entity);
}
public void delete(ISession session, Entity entity)
{
session.Delete(entity);
}
}
public class EntityService
{
private IEntityRepository repository;
public EntityService(IEntityRepository repository)
{
//I'm using Ninject for DI here
this.repository = repository;
}
public void DoTask(Entity _updateEntity, Entity _deleteEntity)
{
ISession session = NHibernateHelper.OpenSession();
using(ITransaction transaction = session.BeginTransaction())
{
this.repositoy.update(session, _updateEntity);
this.repositoy.delete(session, _deleteEntity);
transaction.Commit();
}
}
}
What I want to ask are
- Is it a good idea to let
Repository's Service layer
manageISession
as well asITransaction
and pass it aroundAssociate Repository
? - If it's not a good idea, how should I change my design in order to perform those task in a same transaction without letting
Repository's Service layer
know about that transaction but still makeRepository layer
as lightweight as possible?
EDIT
For clarify, my EntityService
is a business logic layer
which depends on Repository
to perform business logic
then pass the result to presentation layer
(a winform in my case). So that I thought that letting EntityService
manage ISession
and ITransaction
will lead to tight coupling
which I want to avoid in the first place.
EDIT 2
According to ptk93
, I made some change to design as follow:
public class SomeEntity
{
//entity's properties
}
public class EntityRepository : IEntityRepository
{
private ISession session;
private ITrasaction trasaction;
public EntityRepository()
{
this.session = NHibernateHelper.OpenSession();
}
public void BeginTransaction()
{
if(this.transaction != null && this.transaciont.isActive)
{
throw new Exception();
}
this.transaction = this.session.BeginTransaction();
}
public void CommitTransaction()
{
if(this.transaction == null || this.transaction.isActive = false)
{
throw new Exception();
}
this.transaction.Commit();
this.transaction.Dispose();
}
public void update(ISession session, Entity entity)
{
if(this.transaction == null || this.transaction.isActive = false)
{
throw new Exception();
}
session.Update(entity);
}
public void delete(ISession session, Entity entity)
{
if(this.transaction == null || this.transaction.isActive = false)
{
throw new Exception();
}
session.Delete(entity);
}
}
public class EntityService
{
private IEntityRepository repository;
public EntityService(IEntityRepository repository)
{
//I'm using Ninject for DI here
this.repository = repository;
}
public void DoTask(Entity _updateEntity, Entity _deleteEntity)
{
this.repository.BeginTransaction()
this.repositoy.update(session, _updateEntity);
this.repositoy.delete(session, _deleteEntity);
this.repository.CommitTransaction();
}
}
Does this design enough for de-coupling Repository
and Repository Service
?