在实体中存储附加数据

发布于 2024-12-12 13:14:28 字数 608 浏览 2 评论 0原文

假设我有以下实体:Location & 产品

位置具有尺寸(长、宽、高),产品也如此。 地点包含一个或多个产品。基本上,我需要获取一堆 Locations 并根据它们的尺寸和 Location 处的 Products 计算特定位置的剩余空间。我基本上需要存储该值(剩余空间量)并根据该值对 Locations 进行排序。

我应该在哪里存储此值?

我应该将其存储在 Location 实体本身中,作为未映射到数据库中任何内容的属性吗? 这似乎是不好的做法,但很容易做到。

我是否应该为此创建一个包含此附加属性和其余位置数据的 DTO(对于这种情况,这似乎需要大量额外工作)

我是否应该创建一个基本上具有此附加属性和其中实际 Location 实体的包装器?

Let's say I have the following Entities: Location & Product

Locations have dimensions (length, width, height) and so do Products. Locations contain one or more Products. Basically I need to get a bunch of Locations and calculate based on their dimensions and Products at the Location the remaining room left at a particular location. I need to basically store this value (the amount of room left) and sort the Locations based on this value.

Where should I store this value?

Should I store it in the Location entity itself as just a property that is not mapped to anything in the database? This seems like it is bad practice but easy to do.

Should I create a DTO for this that contains this additional property and the rest of the location data (this kind of seems like a lot of extra work for this scenario)?

Should I maybe create a wrapper basically that has this one additional property and the actual Location entity inside it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

生生漫 2024-12-19 13:14:28

对我来说,数据似乎是对象本身固有的,因此我认为将其添加到 Location 类是完全合适的。

我只是将其设置为 Location 类的惰性属性。它应该只在需要时计算,并且可能会触发位置产品集合的延迟加载。

如果你愿意,你可以将它映射到表列,这样当 NHibernate 刷新时它总是会写出最新的值。这将使您能够在数据库级别的查询中使用它进行排序和过滤。

To me it seems that the data is intrinsic to the object itself so I think it is entirely appropriate to add it to Location class.

I would simply make this a lazy property on the Location class. It should only be computed when required and presumably might trigger lazy loading of the Location's collection of Products.

If you want to you can map it to a table column so when NHibernate is flushing it will always write out the most up to date value. This will then enable you to use it in queries for ordering and filtering at the db level.

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