NHibernate 中整数的映射列表
在 NHibernate 手册中我发现了这样的映射:
<bag name="Sizes" table="SIZES" order-by="SIZE ASC">
<key column="OWNER"/>
<element column="SIZE" type="Int32"/>
</bag>
我不禁想知道为什么有人想做这样的事情?映射普通整数是否比创建与给定整数对应的实体(本场景中的 Size 实体)并创建真正的一对多关系更好?
In NHibernate manual I have found mapping like this:
<bag name="Sizes" table="SIZES" order-by="SIZE ASC">
<key column="OWNER"/>
<element column="SIZE" type="Int32"/>
</bag>
I can't help wondering why would anyone want to do something like this? Is there something better about mapping plain integers than creating an entity corresponding to a given integer (Size entity in this scenario) and creating true one-to-many relationship?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应用程序运行的业务领域的要求将决定您是否应该拥有一个整数列表(值类型的集合)或一个实体列表。如果
IList
有一个好的用例,那么一定要使用它,并让 NHibernate 相应地映射此关联。否则只需将其删除。但是,因为您不熟悉而将其删除并不是一个有效的理由。
我在我的领域模型中经常使用它。现在我有很多“Twitter 应用程序”,它们根据搜索“关键字”对推文进行索引,因此我将其映射如下:
我使用此映射是因为我知道:
(大约 4 - 6)
关键字 DateAdded 等。
查询关键字,只需
同时检索它们,
或根本不在
此基础上,我决定将其映射为字符串集合是合适的。
The requirements of your business domain that your application operates in will dictate whether you should have a list of ints (a collection of value types), or a list of entities. If there's a good use case for a
IList<int>
then by all means go for it, and let NHibernate map this association accordingly. Otherwise just remove it.However, removing it because it seems unfamiliar to you, isn't a valid reason.
I use this a lot in my domain models. Right now I have a lot of 'Twitter Applications' that index tweets based on search 'Keywords', so I have mapped it like this:
I use this mapping because I know that:
(about 4 - 6)
Keyword DateAdded etc.
Querying on the Keywords, just
retrieve them all at the same time,
or not at all
On this basis, I decided mapping it as a collection of strings was appropriate.
问题不在于你是否想像这样映射它,问题在于你的模型中是否需要一个整数列表。
当你的模型中有一个整数列表,那么你想像这样映射它。您不想仅仅因为映射而在模型中编写复杂的代码。
那么,您认为在类中拥有一个整数列表有用吗?或者一个 Guid、枚举、双精度列表?
这对你来说没有意义吗?
The question is not if you want to map it like this, the question is, if you need a list of ints in your model.
When you have a list of ints in your model, then you want to map it like this. You don't want to write complex code in your model, just because of the mapping.
So, do you think it is useful to have a list of ints in a class? Or a list of Guids, enums, doubles?
Doesn't it make sense to you?