When creating new or updating existing documents in RavenDB, the documentation says to do it along these lines:
public string Save(Blogpost post)
{
Blogpost model;
if (String.IsNullOrEmpty(post.Id))
{
model = new Blogpost();
_documentSession.Store(model);
}
else
{
model = _documentSession.Load<Blogpost>(post.SimpleId);
}
model.Text = template.Text;
model.Name = template.Name;
_documentSession.SaveChanges();
return model.Id;
}
Someone on my team is doing saves another way for both creating new documents or updating existing ones:
public string Save(Blogpost post)
{
_documentSession.Store(post);
_documentSession.SaveChanges();
return post.Id;
}
Is there any disadvantage to always calling .Store()
even if the document already exists?
Jason, Your code will always overwrite the document. Is that something that you want to do?
If you are doing a rich client app and serializing the full BlogPost to the client like so:
//GET BlogPost/1
public BlogPost Get(int id)
{
return _documentSession.Load<BlogPost>(id)
}
Then rehyrdating a full BlogPost on the server after a user has made changes. The below code appears to be more efficient than doing a Load and then a Store:
//POST BlogPost/
public void Post(BlogPost post)
{
//blog post already has an Id in this example
_documentSession.Store(post)
_documentSession.SaveChanges();
}
When you do a
documentSession.Load<Blogpost>(id)
RavenDB returns the full JSON for the blog post that you already have just for you to overwrite it, turn around and re-save sending the full JSON for the blog post back again over the wire to Raven.
This means that doing a Load and Store when you already have all the data results in twice the network traffic to Raven for no added benefit that I can see using Fiddler.
Even if you were only changing a part of the object (say the Name of the BlogPost) the RavenDB .NET API still sends the full object over the wire when doing a:
Perhaps Ayende Rahien can enlighten us on anything I missed here?
Store will store this object in a new document. Use the change tracking feature built-in in the session object aka the Unit of Work pattern, like the docs suggest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With