I have done some research about intentions of UnitOfWork pattern. In short, what I understood is that UoW is needed for transactions. But I saw some code examples where people use UoW as factory for Repository objects as well, one example is DbContext from EF. My first thought about it doesn't it break single responsibility principle? In my current project I am using both UoW and Repository patterns. Here are my interfaces:
UnitOfWork:
public interface IUnitOfWork : IDisposable
{
void Commit();
void Rollback();
IRepository<T> GetRepository<T>() where T : class;
}
Repository interface:
public interface IRepository<T> where T : class
{
IQueryable<T> FindAll();
IQueryable<T> Find(Expression<Func<T, bool>> predicate);
T FindById(Id id);
void Add(T newEntity);
void Remove(T entity);
}
My concern is GetRepository<T>
method in IUnitOfWork
interface. Should this method be here at all? Or should I move it somewhere else? What would you suggest?
PS I am using NHibernate as my ORM tool, but I think this should not make any difference