public class UnitOfWork {
private readonly ISession _session;
public void BeginTransaction();
public void Commit();
public void Rollback();
public IRepositoryFactory AllRepositories;
}
public interface IOrdersRepository {
public IList<Order> FindPending();
public void AddNew(Order order);
public void Delete(Order order);
}
In NHibernate world main functionality of UnitOfWork in implemented by ISession. ISession keeps track of what has changed and you do not need to have methods like RegisterClean or RegisterDirty. For a larger projects it is useful to hide ISession by using your own class that can be called UnitOfWork. For example:
public class UnitOfWork {
private readonly ISession _session;
public void BeginTransaction();
public void Commit();
public void Rollback();
public IRepositoryFactory AllRepositories;
}
The advantage of hiding ISession from application code is that this code will not reference NHibernate directly which enforces better layering. In other words it will be harder for application layer to start using NHibernate API directly, bypassing data access layer.
The repository itself will contain methods for adding new objects and possibly deleting:
public interface IOrdersRepository {
public IList<Order> FindPending();
public void AddNew(Order order);
public void Delete(Order order);
}
Repository is responsible for abstracting the data storage from upper layers, whilst Unit Of Work is responsible for business transaction management. Repository allows you to substitute storage without changing the code - you can work with text file, XML, database - whatever. UoW gives you an opportunity to control and trace changes to the entities and persist all of the changes of one business transaction at once.
Both EF and NH support UoW by default so you don't need to reimplement it yourself. Still you would want to write your own repository pattern, as these ORMs do not provide it out of the box.
UoW on top of NH and EF is unnecessary level of abstraction and may be viewed as an antipattern.
In that article Fowler does not actually suggest to add responsibility of data manipulations to UoW. What he is saying is you can track changes to objects and flush/rollback them as needed.
发布评论
评论(2)
在 NHibernate 世界中,UnitOfWork 的主要功能是由 ISession 实现的。 ISession 会跟踪已更改的内容,并且您不需要使用 RegisterClean 或 RegisterDirty 等方法。对于较大的项目,通过使用您自己的称为 UnitOfWork 的类来隐藏 ISession 非常有用。例如:
从应用程序代码中隐藏 ISession 的优点是该代码不会直接引用 NHibernate,从而强制实现更好的分层。换句话说,应用程序层将更难绕过数据访问层直接使用NHibernate API。
存储库本身将包含添加新对象和可能删除的方法:
In NHibernate world main functionality of UnitOfWork in implemented by ISession. ISession keeps track of what has changed and you do not need to have methods like RegisterClean or RegisterDirty. For a larger projects it is useful to hide ISession by using your own class that can be called UnitOfWork. For example:
The advantage of hiding ISession from application code is that this code will not reference NHibernate directly which enforces better layering. In other words it will be harder for application layer to start using NHibernate API directly, bypassing data access layer.
The repository itself will contain methods for adding new objects and possibly deleting:
这是两种不同的模式。
Repository负责从上层抽象数据存储,而Unit Of Work负责业务事务管理。存储库允许您在不更改代码的情况下替换存储 - 您可以使用文本文件、XML、数据库 - 等等。 UoW 让您有机会控制和跟踪实体的更改,并一次性保留一项业务事务的所有更改。
EF 和 NH 都默认支持 UoW,因此您无需自己重新实现。您仍然希望编写自己的存储库模式,因为这些 ORM 并未提供开箱即用的功能。
NH 和 EF 之上的 UoW 是不必要的抽象级别,可能被视为反模式。
在那篇文章中,福勒实际上并没有建议将数据操作的责任添加到 UoW。他的意思是,您可以跟踪对象的更改并根据需要刷新/回滚它们。
These are two different patterns.
Repository is responsible for abstracting the data storage from upper layers, whilst Unit Of Work is responsible for business transaction management. Repository allows you to substitute storage without changing the code - you can work with text file, XML, database - whatever. UoW gives you an opportunity to control and trace changes to the entities and persist all of the changes of one business transaction at once.
Both EF and NH support UoW by default so you don't need to reimplement it yourself. Still you would want to write your own repository pattern, as these ORMs do not provide it out of the box.
UoW on top of NH and EF is unnecessary level of abstraction and may be viewed as an antipattern.
In that article Fowler does not actually suggest to add responsibility of data manipulations to UoW. What he is saying is you can track changes to objects and flush/rollback them as needed.