Home > Uncategorized > Playing with RavenDB

Playing with RavenDB

I finally got some time to play with RavenDB. I decided to write a simple “todo list-ish” application. Since I wanted the application to be stand alone, I went for the embedded version. RavenDB is just a few clicks away using NuGet:

image

However, remember to switch to full .Net framework. This is required by the embedded version of RavenDB.

full

Now, we can create a database connection:

NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);

store = new EmbeddableDocumentStore { DataDirectory = "Data", UseEmbeddedHttpServer = true }.Initialize();

 

A nice feature of RavenDB is that, even though we are using the embedded vesion, it is possible to inspect the database using a web browser. For this to work however, the UseEmbeddedHttpServer property must be set to true as above. The embedded server is available att http://localhost:8080 by default.

image

Oh, that is right. I needed to to one more thing; I added Raven.Studio.xap as a content file to my project.

content

The file can be found in $PROJECT\packages\RavenDB-Embedded.1.0.616\lib\net40.

Alright, now we can start adding (or updating) data:

private void SaveButton_Click(object sender, RoutedEventArgs e)

{

    using (var session = store.OpenSession())

    {

        CurrentNote.HasUnsavedChanges = false;

        session.Store(CurrentNote);

        session.SaveChanges();

    }           

}

Search for data:

private void SearchButton_Click(object sender, RoutedEventArgs e)

{

    using (var session = store.OpenSession())

    {

        _searchResult.Clear();

        foreach (var note in

            session.Query<Note>().

            Where(IsMatch))

        {

            _searchResult.Add(note);

            note.TrackChanges();

        }

        FirePropertyChanged("SearchResult");

    }           

}

 

Delete data:

private void buttonDelete_Click(object sender, RoutedEventArgs e)

{

    if (CurrentNote != null)

    {

        using (var session = store.OpenSession())

        {

            var orig = session.Query<Note>().Where(n => n.Id == CurrentNote.Id).FirstOrDefault();

            if (orig != null)

            {

                session.Delete<Note>(orig);

                session.SaveChanges();

            }

        }

        _searchResult.Remove(CurrentNote);

        CurrentNote = null;

    }

}

The sample project is available here. RavenDB library is not included.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: