RavenDB ID for child documents

I like how cleanly the object is stored in ravenDB, but there is a practical question for which I am not sure about the best answer.

Suppose 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 clicks on a page, I will remove from the list of quotes from which they can choose. These quotes are only associated with the quote request instance.

My question is, since a child, such as a quote in the list, does not have an identifier generated by the database, how can I create a request so that the next page knows which quote the user wants to buy?

There may be several quotes by one .dd supplier.

My thoughts were either to add QuoteId, or to add to it. Quotes.Count, but it seems a bit hacked or generates a random number, also a little hacked.

How do people generally cope with something like this?

+4
source share
3 answers

Do you really need to link the purchase (what the user decided to buy) with the original quote? I assume that you take a quote and then convert it to a purchase. If so, then don't worry about id at all. Just pass the component values ​​to the next step. In other words, interpret the quote as a meaning in the sense of DDD.

However, if you do need to keep in touch with the purchase ... well, then it depends on what you really need for tracking. For example, you can simply update QuoteRequest by noting the selected quote. (Add IsSelected or something similar to the quote Quote class.) You can then link the purchase back to the quote request, and you can identify the quote using the flags.

Again, it all depends on the context (and I just guess about it).

+2
source

Since no one answered this, I will simply say how I will do it;

Why add an identifier at all? just use list index? This is the query "? Quote = 0", they get a quote at position 0?

Not quite sure if I don't get something here, though ...

+1
source

One option is for the parent to retain the last used identifier. When adding a new child, you increment the id counter and add it to the child. When the object is saved, the counter-identifier is automatically incremented.

Let's say you have a 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 may not be 100% correct, but should give you an idea of ​​how to do this at least!

0
source

Source: https://habr.com/ru/post/1336053/


All Articles