本文介绍了NHibernate的:映射到字段或属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你创建你的映射文件,你的属性映射到字段或属性:

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>

当然,请解释原因:)

Of course, please explain why :)

通常情况下,我映射到属性,但映射到字段可以使把一些逻辑,在吸气的属性/ 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 ?

推荐答案

我映射到属性。如果我觉得有必要,我映射二传手到外地。 (通常是通过类似访问= 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").

这让我有很好看的查询,如从不同人的名字='约翰',而不是像从人民那里的firstName / _FirstName和滋润我的实体时,也避免了二传手的逻辑。

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的:映射到字段或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:19