且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何实现适用于 EF 和 NHibernate 的工作单元

更新时间:2023-02-13 15:36:34

如果不构建支持在 linq 之上的不同提供的解决方案,这将导致灾难.Linq 和 IQueryable 是有漏洞的抽象——每个 Linq 提供者都可以有自己的特性"和限制.此外,EF 本身通过IQueryable 的自定义扩展方法添加了一些逻辑(如EFv4.1 中的IncludeAsNoTracking).这些方法在内部将 IQueryable 转换为 ORM 特定的类.

As a side not building solution supporting different provides on top of the linq is way to disaster. Linq and IQueryable are leaky abstractions - each Linq provider can have its own "features" and limitations. Moreover EF itselfs adds some logic via custom extension methods for IQueryable (like Include or AsNoTracking in EFv4.1). These methods internally converts IQueryable to ORM specific classes.

如果你想有通用的解决方案,你必须放弃Linq并添加第三种模式来形成抽象.除了存储库和工作单元模式之外,您还需要自定义规范模式.通常,您将重新实现 NHibernate 的 Criteria API.

If you want to have universal solution you must abandon Linq and add third pattern to form the abstraction. In addition to Repository and Unit of Work patterns you need custom Specification pattern. Generally you will reimplement NHibernate's Criteria API.