Perhaps I’m not going about this the right way, so if anyone can steer me in the right direction, I would greatly appreciate it.
I have a lot of methods in my code that do something like
void UpdateWidget(Widget updatedWidget)
{
IDocumentSession session = documentStore.OpenSession();
oldWidget = session.Load<Widget>("widgets" + widget.Id);
oldWidget.Name = updatedWidget.Name;
oldWidget.Color = updatedWidget.Color;
oldWidget.CreatedDateTime = updatedWidget.CreatedDateTime;
session.SaveChanges();
}
What I’d LIKE to do is something like the following (assuming the IDs are the same of course)
void UpdateWidget(Widget updatedWidget)
{
IDocumentSession session = documentStore.OpenSession();
oldWidget = session.Load<Widget>("widgets" + widget.Id);
oldWidget = updatedWidget
session.SaveChanges();
}
Can someone help me with some syntax or pattern that can get me close to this? Is there a way to use object tracking in Raven to accomplish this? Or do I need some sort of mapping tool like automapper (which I don’t understand very well)
thanks in advance!
You can just store the updated object and it will replace the old one if it exists already.
However, you have to consider the concurrency effects.
If you have optimistic concurrency disabled, it should just accept the new document, but you risk stepping on others changes. If you have it enabled, you need to either do the Load and update in the same session, or you need to track the etag of the document yourself.
Review the following RavenDB knowledge base article for details: http://ravendb.net/kb/16/using-optimistic-concurrency-in-real-world-scenarios
ALSO – not related to your question, but it’s very important that your session is being disposed properly. Wrap it in a
usingstatement. (Or if it’s being injected, then make sure your IoC has the appropriate lifetime scope and is disposing it when appropriate.)