I am looking into the UoW pattern and have 3 questions.
public class UnitofWork : unitofwork.Models.IUnitofWork
{
private readonly ITransaction transaction;
private readonly ISession session;
public UnitofWork(ISession session)
{
this.session = session;
session.FlushMode = FlushMode.Auto;
transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void Commit()
{
if (!transaction.IsActive)
{
throw new InvalidOperationException("Oops! We don't have an active transaction");
}
transaction.Commit();
}
public void Rollback()
{
if (transaction.IsActive)
{
transaction.Rollback();
}
}
public void Dispose()
{
if (session.IsOpen)
{
session.Close();
}
}
}
-
I just learning about “IsolationLevels” and I am wondering which one should you be using? What happens if you need to make use of multiple “IsolationLevels” for different transactions? How would you configure your UoW(would you make multiple implementations of the above class?)
-
Other then Rollback and Commit was usually goes into the UoW? I know stuff like creating,updating,getting queries would go into a repository(if your using this pattern) So what else would you typicall see in it?
-
I copied this UoW from some site(don’t have it on hand right now) and made changes it to fit my needs(for instance I am using ninject so I felt there was no point in the UoW taking in the sessionFactory and opening a session in the UoW)
I am wondering what is the Dispose for? I seen this a few times before(some seem to implement IDispose).
I don’t actually use it right now in any of my code. I am wondering if it is necessary for me as I mentioned I am using ninject and that handles the session(ie closes it once I am done)
public void Dispose()
{
if (session.IsOpen)
{
session.Close();
}
}
Edit
I added this to my unit of work
public void BeginTransaction()
{
transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void BeginTransaction(IsolationLevel level)
{
transaction = session.BeginTransaction(level);
}
I removed it from the constructor
The ISession itself is a unit-of-work implementation, so implementing your own is entirely optional.
usingblock or disposed of manually. It means that the class has clean up operations that need to run, such as closing the ISession. Here, the Dispose method should just call ISession.Dispose instead of just closing the session.