如何在 n 层架构中解决这个 NHibernate 查询? [英] How can I solve this NHibernate Querying in an n-tier architecture?
问题描述
我在尝试将 NHibernate 与我的服务层分离时遇到了障碍.我的架构如下所示:
I've hit a wall with trying to decouple NHibernate from my services layer. My architecture looks like this:
web -> services -> repositories -> nhibernate -> db
web -> services -> repositories -> nhibernate -> db
我希望能够从我的服务层和可能是我的 web 层生成 nhibernate 查询,而这些层不知道它们正在处理什么orm.目前,我的所有存储库都有一个 find 方法,它接受 IList
.这允许我从架构中的任何位置传入一个标准列表,例如 new object() {"Username", usernameVariable};
.NHibernate 接受它并创建一个新的 Criteria 对象并添加传入的标准.这适用于我的服务层的基本搜索,但我希望能够传入我的存储库转换为 NHibernate 标准的查询对象.
I want to be able to spawn nhibernate queries from my services layer and possibly my web layer without those layers knowing what orm they are dealing with. Currently, I have a find method on all of my repositories that takes in IList<object[]> criteria
. This allows me to pass in a list of criteria such as new object() {"Username", usernameVariable};
from anywhere in my architecture. NHibernate takes this in and creates a new Criteria object and adds in the passed in criteria. This works fine for basic searches from my service layer, but I would like to have the ability to pass in a query object that my repository translates into an NHibernate Criteria.
真的,我很想实现类似于这个问题中描述的内容:抽象nhibernate标准是否有价值.我只是没有找到关于如何实现这样的东西的任何好的资源.该问题中描述的方法是一种好方法吗?如果是这样,任何人都可以提供一些有关如何实施此类解决方案的指示吗?
Really, I would love to implement something like what is described in this question: Is there value in abstracting nhibernate criterion. I'm just not finding any good resources on how to implement something like this. Is the method described in that question a good approach? If so, could anyone provide some pointers on how to implement such a solution?
推荐答案
抽象掉 ORM 将:
- 为重新定义其 API 带来了大量工作
- 无法优化/批量数据库访问
- 让理解所执行的查询变得更加困难
- 将导致大量的 SELECT N+1
而且所有的价值都很少:交换 ORM 框架的模糊选项,很可能会有很多其他问题
and all for very little value: the vague option to exchange the ORM framework which will most probably have a lot of other problems
- 缺少功能
- 实现上的细微差别
- 学习曲线
更新:经验
我曾经参与实现现有 DAL 抽象的新提供者.它最终表现不佳,引入了很多错误,错误处理是一团糟,有时会使用陈旧数据,因为应用程序采用默认实现.原因:
I was once involved in implementing a new provider of an existing DAL abstraction. It ended up performing badly, introduced a lot of bugs, Errorhandling was a mess and sometimes used stale data because the application assumed the default implementation. Reasons:
- 缓存不知道上下文
- 缓存实现有不同的语义
- 批处理 API 差异太大而无法抽象
- 错误特定于实现(例如,FileNotFound -> FilesearchDialog 对于基于 tcp/ip 的数据库是无用的)
- 错误恢复是不同的(每个实现都有自己的一组可以从中恢复的错误)
- 锁定机制不同
- SQL 数据库中没有一致的更改事件
- 嵌套事务
- 模型类中的默认实现被破坏
- 重新实现所有抽象的查询需要大量的工作,并引入了很多复制粘贴错误
- 未明确说明顺序的查询将在不同的实现中返回不同的排序结果
- Caching does not know context
- Cacheimlementation had different semantics
- batching APIs too different to be abstracted
- Errors are specific to implementation (e.g. FileNotFound -> FilesearchDialog is uselesss for a tcp/ip based databases)
- Error recovery is different (each implementation has it's own set of errors it can recover from)
- locking mechanism was different
- no consistent change event in SQL-Databases
- nested transactions
- default implementation bleeded in Model classes
- reimplementing all abstracted Queryies was a lot of work and introduced a lot of copy paste bugs
- querying without explicitly stating the order will return different ordered results in different implementations
对应用程序进行了大量重构:
It took a lot of refactoring of the application:
- 去除仅一种实现提供的功能
- 每个实现的缓存管理
- 由于瞬态数据导致的包装器身份问题
- 非常努力地实现对两个数据存储的查询
附加点:
- 通过抽象 DAL 迁移数据的速度慢得要命
- 实现另一个实现永远不会发生,因为上述问题太昂贵了(在提到的场景中,我们开始慢慢重新实现整个项目)
- 实现 DAL API 的正确语义极其困难,因为在纯 API 中没有使用上下文
移植(业务任务)在 IMO 中会少很多痛苦,因为我们为了性能而做了一些.
Porting (of business tasks) would have been a lot less painfull IMO as we did that for a few because of performance.
更新 2:经验 2:尝试从 NHibernate 移植到 EntityFramework 时遇到障碍(使用 NH 实现,但无法在合理时间内使用 EF 4)
Update2: experience2: RoadBlocks while trying to port from NHibernate to EntityFramework (impl with NH but couldn't with EF 4 in reasonable time)
- 嵌套事务
- 枚举支持
- 带有compositeId 的引用(如何去掉referenceId)
- 组件中的引用
- 读取批处理(Futures),一次性完成页面 + 计数
- 映射 CultureInfo(IUserType 支持)
这篇关于如何在 n 层架构中解决这个 NHibernate 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!