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

When creating new or updating existing documents in RavenDB, the documentation says that it performs the following actions:

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 from my team is doing another way to both create new documents and update existing ones:

public string Save(Blogpost post)
{
    _documentSession.Store(post);
    _documentSession.SaveChanges();
    return post.Id;
}

Is there a flaw to always invoke .Store(), even if the document already exists?

+5
source share
3 answers

Store will save this item in a new document. Use the change tracking feature built into the session object, as well as the Unit of Work template, as documents suggest.

+1
source

BlogPost :

//GET BlogPost/1
public BlogPost Get(int id)
{
    return _documentSession.Load<BlogPost>(id)
}

BlogPost . , , :

//POST BlogPost/
public void Post(BlogPost post)
{
    //blog post already has an Id in this example
    _documentSession.Store(post)
    _documentSession.SaveChanges(); 
}

documentSession.Load<Blogpost>(id)

RavenDB JSON , , , JSON Raven.

, Load and Store, , Raven , Fiddler.

(, BlogPost), RavenDB.NET API - :

  • ()
  • ( )
  • ()
  • SaveChanges()

, -, ?

+5

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

+4
source

All Articles