子文档的 RavenDB ID

发布于 2024-10-13 00:25:07 字数 578 浏览 2 评论 0原文

我喜欢 ravenDB 中对象存储的干净程度,但有一个实际问题,我不确定最佳答案。

假设我有一个报价请求:

QuoteRequest.cs

int Id;
dateTime DateCreated;
List<Quotes> Quotes;

Quote.cs

int ProviderId;
int Price;
int ServiceDays;
int ServiceTypeId;

当有人点击某个页面时,我会给出一个报价列表,他们可以从中进行选择。这些报价仅与报价请求的实例相关。

我的问题是,由于子对象(例如列表中的报价)没有数据库生成的 Id,因此如何生成查询字符串以让下一页知道用户想要购买哪个报价?

一个providerId 可以有多个报价。

我的想法是添加一个 QuoteId 并根据 this.Quotes.Count 增加它,但这看起来有点老套,或者生成一个随机数,也有点老套。

人们通常如何处理这样的事情?

I like how cleanly an object is stored in ravenDB, but have a practical question for which I'm not sure of the best answer.

Lets say i have a quote request:

QuoteRequest.cs

int Id;
dateTime DateCreated;
List<Quotes> Quotes;

Quote.cs

int ProviderId;
int Price;
int ServiceDays;
int ServiceTypeId;

when someone hits a page, i spit out a list of quotes from which they can choose. These quotes are only related to an instance of the quote request.

My question is, since a child object, such as a quote in the list, doesnt have an Id generated by the database, how do I generate a querystring to let the next page know which quote the user wants to buy?

There could be multiple quotes by one providerId.

My thoughts were either add a QuoteId and increment it based on this.Quotes.Count, but that seems a little hacky, or generate a random number, also a little hacky.

How do people generally handle something like this?

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

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

发布评论

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

评论(3

疧_╮線 2024-10-20 00:25:07

您真的需要将购买(用户选择购买的商品)与原始报价关联起来吗?我猜您会接受报价,然后将其转换为购买。
如果是这样,那么根本不用担心 id。只需将组成值传递到下一步即可。换句话说,将引用视为 DDD 意义上的值。

但是,如果您确实需要存储与购买的关联......那么,这取决于您真正需要跟踪的内容。例如,您可以只更新 QuoteRequest,标记所选报价。 (添加 IsSelected 或类似于报价类 Quote 的内容。)然后,可以将购买链接回报价请求,并且您可以通过标志来识别报价。

同样,所有这些都取决于上下文(我只是对此进行猜测)。

Do you really need to associate the purchase (what the user chose to buy) with the original quote? I'm guessing that you take the quote and then convert it to a purchase.
If that is so, then don't worry about an id at all. Just pass along the constituent values to the next step. In other words, treat quote like a Value in the DDD sense.

However, if you do need to store an association to the purchase... well, then it depends on what you really need to track. For example, you could just update the QuoteRequest, marking the selected quote. (Add an IsSelected or something similar to the quote class Quote.) Then the purchase could be linked back to the quote request, and you could identify the quote by way of the flags.

Again, all this depends on the context (and I'm just making guesses about that).

如歌彻婉言 2024-10-20 00:25:07

既然还没人回答这个问题,我就说一下我会怎么做;

为什么要添加 Id?只使用列表的索引?如果请求是“?quote=0”,他们会在位置 0 处获得报价吗?

不太确定如果我没有在这里得到一些东西......

Since no one has answered this yet I'll just say how I would do it;

Why add a Id at all? just use the index of the List? It the request is "?quote=0" they get the quote at position 0?

Not really sure If I'm not getting something here though...

原谅我要高飞 2024-10-20 00:25:07

一种选择是让父对象存储上次使用的 ID。添加新的子对象时,您会增加 id 计数器并将其添加到子对象中。保存对象时,id 计数器会自动递增。

假设您有带有评论的博客文章:

public class Post
{
  public int NextCommentId;
  public List<Comment> Comments;
  ...
}

...

var comment = new Comment { Id = post.NextCommentId++ };
post.Comments.Add(comment);

session.SaveChanges();

上面的代码可能不是 100% 正确,但至少应该让您了解如何做到这一点!

One option is to have the parent object store the last used id. When adding a new child object you increment the id-counter and add that to the child. When the object is saved the id-counter is automatically incremented.

Lets say you have blog post with comments:

public class Post
{
  public int NextCommentId;
  public List<Comment> Comments;
  ...
}

...

var comment = new Comment { Id = post.NextCommentId++ };
post.Comments.Add(comment);

session.SaveChanges();

The code above might not be 100% correct, but should give you an idea of how to do it at least!

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