RavenDB - Image Gallery Project (IV) -Tracking Documents

Published on 2010-9-30

The code for this and all other entries can be found here: http://github.com/robashton/RavenGallery/ 

Apologies for yesterday, something happened to my post and it got swallowed up into the ether, it might be worth going back and reading it if you wanted why it ended so abruptly: The Application Lifecycle

Before continuing on, I want to make it clear how RavenDB and basic interaction with RavenDB works, I’m not going to dwell on this as the focus of this series is how RavenDB works within the larger scale of an application, but knowing how to save/retrieve/modify documents is definitely important.

The following examples use this document model:

   1:      public class Image
   2:      {
   3:          public string Id
   4:          {
   5:              get;
   6:              set;
   7:          }
   8:   
   9:          public string Name
  10:          {
  11:              get;
  12:              set;
  13:          }
  14:   
  15:          public Tag[] Tags
  16:          {
  17:              get;
  18:              set;
  19:          }
  20:      }
 
   1:      public class Tag
   2:      {
   3:          public string Name
   4:          {
   5:              get;
   6:              set;
   7:          }
   8:      }

Saving a new document

   1:      using (var s = store.OpenSession())
   2:      {
   3:          // Create the image
   4:          var image = new Image()
   5:          {
   6:              Name = "SomeImage",
   7:              Tags = new Tag[]{
   8:                  new Tag() { Name = "SomeTag" },
   9:                  new Tag() { Name = "SomeOtherTag" }
  10:              }
  11:          };
  12:   
  13:          // Store the image
  14:          s.Store(image);
  15:                      
  16:          // The act of storing the image has given us an id to work with
  17:          string documentId = image.Id;
  18:   
  19:          // Only when this is called are the changes flushed to the server
  20:          s.SaveChanges();                  
  21:      }

Getting a document out by Id

Now we have an id, we can request a document by id in a completely different session:

   1:     using(var s = store.OpenSession())
   2:      {
   3:          // Load an image by id
   4:          var image = s.Load<Image>(documentId);
   5:   
   6:          // Do stuff with that image
   7:      }

 

Querying for a document using standard queries

   1:          using(var s = store.OpenSession())
   2:          {
   3:              // Find an image by name
   4:              var image = s.Query<Image>()
   5:                  .Where(x=>x.Name== "SomeImage")
   6:                  .SingleOrDefault();
   7:          }

Modifying a document and persisting those changes

Both the above examples of retrieving documents from the store have something in common, the RavenDB client will track those loaded documents and any changes to them automatically. Thus the following code will just work

   1:          using(var s = store.OpenSession())
   2:          {
   3:              // Find an image by name
   4:              var image = s.Query<Image>()
   5:                  .Where(x=>x.Name== "SomeImage")
   6:                  .SingleOrDefault();
   7:   
   8:              // Change the name
   9:              image.Name = "NewName";
  10:   
  11:              // Commit any changes
  12:              s.SaveChanges();
  13:          }

How this fits into our structure

If a repository takes responsibility for saving new documents and retrieving those documents, responsibility for actually persisting those changes can be given to another part of our application, the rest of the application doesn’t have to care about IDocumentSession or RavenDB, it can just work on those documents and accept any changes that are made will get flushed at the end of the unit of work.

In the following blog entries if anything is unclear, refer back to this page to try and understand what is going on.

2020 © Rob Ashton. ALL Rights Reserved.