Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any disadvantage to calling _documentSession.Store when updating existing documents in RavenDB?

Tags:

ravendb

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?

like image 424
Jason More Avatar asked Mar 27 '13 21:03

Jason More


3 Answers

Jason, Your code will always overwrite the document. Is that something that you want to do?

like image 159
Ayende Rahien Avatar answered Oct 26 '22 14:10

Ayende Rahien


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:

  • Load()
  • make some changes (but not changes to everything)
  • Store()
  • SaveChanges()

Perhaps Ayende Rahien can enlighten us on anything I missed here?

like image 22
John Culviner Avatar answered Oct 26 '22 16:10

John Culviner


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.

like image 29
synhershko Avatar answered Oct 26 '22 16:10

synhershko