存储库与工作单元

发布于 2024-11-28 01:20:51 字数 1459 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-12-05 01:20:51

在 NHibernate 世界中,UnitOfWork 的主要功能是由 ISession 实现的。 ISession 会跟踪已更改的内容,并且您不需要使用 RegisterClean 或 RegisterDirty 等方法。对于较大的项目,通过使用您自己的称为 UnitOfWork 的类来隐藏 ISession 非常有用。例如:

public class UnitOfWork {

    private readonly ISession _session;

    public void BeginTransaction();

    public void Commit();

    public void Rollback();

    public IRepositoryFactory AllRepositories;
}

从应用程序代码中隐藏 ISession 的优点是该代码不会直接引用 NHibernate,从而强制实现更好的分层。换句话说,应用程序层将更难绕过数据访问层直接使用NHibernate API。

存储库本身将包含添加新对象和可能删除的方法:

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);
}
命比纸薄 2024-12-05 01:20:51

这是两种不同的模式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文