NHibernate 中整数的映射列表

发布于 2024-09-19 19:18:15 字数 289 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-09-26 19:18:15

应用程序运行的业务领域的要求将决定您是否应该拥有一个整数列表(值类型的集合)或一个实体列表。如果 IList 有一个好的用例,那么一定要使用它,并让 NHibernate 相应地映射此关联。否则只需将其删除。

但是,因为您不熟悉而将其删除并不是一个有效的理由。

我在我的领域模型中经常使用它。现在我有很多“Twitter 应用程序”,它们根据搜索“关键字”对推文进行索引,因此我将其映射如下:

public class TwitterApplication
{
    public virtual int Id { get; set; }
    public virtual string ApplicationName { get; set; }

    // other properties (snip)

    public virtual ISet<string> Keywords { get; set; }
}

我使用此映射是因为我知道:

  1. 关键字的数量会很少
    (大约 4 - 6)
  2. 我对存储不感兴趣
    关键字 DateAdded 等。
  3. 我不会进行分页或
    查询关键字,只需
    同时检索它们,
    或根本不在

此基础上,我决定将其映射为字符串集合是合适的。

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:

public class TwitterApplication
{
    public virtual int Id { get; set; }
    public virtual string ApplicationName { get; set; }

    // other properties (snip)

    public virtual ISet<string> Keywords { get; set; }
}

I use this mapping because I know that:

  1. The number of Keywords will be small
    (about 4 - 6)
  2. I'm not interested in storing
    Keyword DateAdded etc.
  3. I'm not going to be doing Paging or
    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.

花桑 2024-09-26 19:18:15

问题不在于你是否想像这样映射它,问题在于你的模型中是否需要一个整数列表。

你的模型中有一个整数列表,那么你想像这样映射它。您不想仅仅因为映射而在模型中编写复杂的代码。

那么,您认为在类中拥有一个整数列表有用吗?或者一个 Guid、枚举、双精度列表?

class Graph
{
  IList<double> Values { get; private set; }
}

这对你来说没有意义吗?

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?

class Graph
{
  IList<double> Values { get; private set; }
}

Doesn't it make sense to you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文