NHibernate:映射到字段或属性? [英] NHibernate : map to fields or properties?
问题描述
当您创建映射文件时,您是否将属性映射到字段或属性:
When you create your mapping files, do you map your properties to fields or properties :
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Foo" namespace="Foo.Bar" >
<class name="Foo" table="FOOS" batch-size="100">
[...]
<property name="FooProperty1" access="field.camelcase" column="FOO_1" type="string" length="50" />
<property name="FooProperty2" column="FOO_2" type="string" length="50" />
[...]
</class>
</hibernate-mapping>
当然,请解释原因:)
通常,我映射到属性,但映射到字段可以在属性的 getter/setter 中放置一些逻辑".
Usually, I map to properties, but mapping to fields can enable to put some "logic" in the getters/setters of the properties.
映射到字段不好"吗?有最佳实践吗?
Is it "bad" to map to fields ? Is there a best practice ?
推荐答案
我映射到属性.如果我觉得有必要,我会将 SETTER 映射到一个字段.(通常通过access=field.camelcase"之类的方式).
I map to properties. If I find it necessary, I map the SETTER to a field. (usually via something like "access=field.camelcase").
这让我有漂亮的查询,例如from People Where FirstName = 'John'" 而不是from People Where firstName/_firstName",并且在给我的实体补水时也避免了 setter 逻辑.
This lets me have nice looking Queries, e.g. "from People Where FirstName = 'John'" instead of something like "from People Where firstName/_firstName" and also avoid setter logic when hydrating my entities.
这篇关于NHibernate:映射到字段或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!