NHibernate的:映射到字段或属性?

编程入门 行业动态 更新时间:2024-10-28 15:23:03
本文介绍了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的:映射到字段或属性?

本文发布于:2023-11-17 05:42:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   属性   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!