NHibernate准则的抽象是否有价值? [英] Is there value in abstracting NHibernate criterion?
问题描述
我对NHibernate还是很陌生,我看到的大多数示例在基本Criterion
或DetachedCriterion
类上都增加了一些抽象层.在简单的情况下,它是某种Query
类,可能看起来像这样:
public class Query<T>
{
public List<QueryCondition> Conditions { get; set;}
}
public class QueryCondition
{
public string Property { get; set; }
public ComparisonEnum Comparison { get; set; }
public object Value { get; set; }
}
在服务或存储库层中,将查询抽象化的某些地方转换为适当的Criterion
对象,然后NHibernate将其用于检索记录.
当然,这个例子太过简单了.在我的项目中,我已经创建了一个包含AND
和OR
条件的Query<T>
类,并且该接口定义了一种翻译我的自定义条件的方法(在某些情况下,此方法比(上面简单的QueryCondition
示例)添加到AbstractCriterion
中,以添加到服务中内置的DetachedCriterion
中.之所以这样做,部分原因是我坚持关联的查询,以便用户可以定义列表并保存它,但是最大的原因是,这似乎是我所见过的n层NHibernate项目的主要方法.>
但是,我开始怀疑这种抽象的开销是否值得.它并没有节省我对NHibernate的依赖.因为我使用期货来批量查询,所以我的所有项目都必须引用NHibernate才能至少访问IFutureValue
以获得记录数.最重要的是,我的Query<T>
类与NHibernate有着千丝万缕的联系,因为它构建了AbstractCriterion
(尽管我可以很容易地将其拉到一个单独的类中).
无论如何,我越看越容易将Query<T>
替换为DetachedCriteria
.两者都是可序列化的,因此我可以将其持久化,并且由于我不会构建与条件本身分开的条件,因此我认为不太可能遇到复杂查询的别名问题.而且就抽象而言,这种抽象似乎并不能提供足够的隔离性.
任何人都可以给我理由,为什么我应该提取NHibernate标准,而不是您可能有一天会更改ORM"(我可以说这在项目的现阶段是不可能的) ?
在单元测试方面,它具有优势. Criteria
API在构建查询方面确实很棒,但是在模拟方面确实很痛苦.如果您有此类课程,则可以轻松验证条件:
query.Conditions.Contains(requiredCondition);
在标准"中,您将需要一些模拟并验证应用程序的行为,而不是更难的输出.
仅因为这个原因,我仍在应用中使用Repository
模式,而不是直接使用ISession
I'm fairly new to NHibernate, and most of the examples I've seen add some layer of abstraction over the base Criterion
or DetachedCriterion
classes. In simple cases, it's some sort of Query
classes that might look something like this:
public class Query<T>
{
public List<QueryCondition> Conditions { get; set;}
}
public class QueryCondition
{
public string Property { get; set; }
public ComparisonEnum Comparison { get; set; }
public object Value { get; set; }
}
Somewhere in a services or repository layer that query abstraction is translated to a proper Criterion
object which is then used by NHibernate to retrieve the records.
That example is a gross oversimplification, of course. In my project, I had gone down the road of creating a Query<T>
class that would contain AND
and OR
conditions, and the interface defines a method to translate my custom conditions (which are in some cases much more complicated than the simplistic QueryCondition
example above) into an AbstractCriterion
to add to a DetachedCriterion
built in the service. Part of the reason I did this is that I persist queries associated so users can define a list and save it, but the biggest reason is that it seems to be the prevailing method for the n-tier NHibernate projects I've seen.
However, I'm starting to wonder if the overhead of this abstraction is worth it. It doesn't save me dependencies on NHibernate. Because I'm using futures to batch queries, my projects all have to reference NHibernate to at least access IFutureValue
for record counts. On top of that, my Query<T>
class is inextricably tied to NHibernate since it builds the AbstractCriterion
(though I could easily pull that out to a separate class).
Regardless, the more I look at it, I could simply replace my Query<T>
with a DetachedCriteria
. Both are serializable, so I can persist them, and since I wouldn't be building criterion conditions separate from the criterion itself I think I'm less likely to run into alias issues for complex queries. And as far as abstractions go, this one doesn't seem to provide much insulation from complexity.
Can anyone give me reasons why I should abstract NHibernate criteria other than the "you might change your ORM one day" (something I'm comfortable saying is an impossibility at this stage in the project)?
It has an advantage in terms of unit testing. Criteria
API is really great at building queries, but is a real pain for mocking. If you have such class you easily verify condition:
query.Conditions.Contains(requiredCondition);
While in Criteria you would need some mocks and verify behavior of application, not output which is much harder.
Only because of this reason I'm still using Repository
pattern in my apps instead of just using ISession
directly
这篇关于NHibernate准则的抽象是否有价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!