Join NoSQL movement with RavenDB – Part 2 (Embedded, InMemory, and Management Studio)

This is the second post continued from the post,  Join NoSQL movement with RavenDB – Part 1.  Part 1 showed how easy it is to use RavenDB by embedding it into your console application using NuGet package manager.

This post, I will embed RavenDB to a normal Web Form application. After creating an empty web application, I just go to NuGet package manager like the previous post and install RavenDB Embedded.

imageimage

The screenshots above show the solution before and after RavenDB Embedded has been installed. Besides the required RavenDB libraries in References, you should see Raven.Studio.Xap, which I didn’t mention last time. The xap file allows me to use the Management Studio, which is a silverlight client that let you manage the data in a RavenDB server instance similar to a typical RDBMS client such as SQL Server Management Studio.

Next step is to continue building the application. I add a normal web form page. I will just put a normal GridView into the page with AutoGenerateDeleteButton set to true and a RowDeleting event handler. I also add a model class, Person (which I also use in the post, Adding Search to your C# Metro Style App ).

image

In the ListPeople.aspx code-behind page, I initialize the RavenDB instance as well as adding documents . I should have done this in Global.asax in non-demo application Winking smile.

    public partial class ListPeople : System.Web.UI.Page
    {
        static IDocumentStore ravenInstance;        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && ravenInstance == null){           
                // Initialize new RavenDB Memory Instance with EmbeddedHttpServer                
                ravenInstance = new EmbeddableDocumentStore { RunInMemory = true, UseEmbeddedHttpServer = true };                
                ravenInstance.Initialize();

                // Build Sample Data
                SamplePeople.BuildSamplePeople();
                
                using(var session = ravenInstance.OpenSession())
                {
                    foreach (var person in SamplePeople.People)
                    {
                        session.Store(person);
                    }
                    session.SaveChanges();
                }
                BindPeopleGrid(SamplePeople.People);
            }
        }        

        protected void PeopleGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            using (var session = ravenInstance.OpenSession())
            {
                // Get Deleting Id
                var personId = e.Values[0].ToString();   
                             
                // remove from RavenDB
                var person = session.Load<Person>(personId);
                session.Delete(person);
                session.SaveChanges();

                // rebind the data from RavenDB
                var people = session.Query<Person>();
                BindPeopleGrid(people);
            }            
        }

        void BindPeopleGrid(IEnumerable<Person> people)
        {
            PeopleGrid.DataSource = people;
            PeopleGrid.DataBind();
        }
    }

This time you might notice that I used different configuration options from the last time. I wanted to run RavenDB instance in memory (of course this means every time an instance is created, it will be an empty database, so utilize it carefully). Instead of setting ConnectionString, I put RunInMemory and UseEmbeddedHttpServer equal to true. The UseEmbeddedHttpServer option will allow me to access the RavenDB instance externally or through the REST API.

The other interesting in the code is the PeopleGrid_RowDeleting method which handles deleting event when a Delete button/link is clicked. Now let me just run the application.

image

I can also run the RavenDB Management Studio to see what documents we have in the current RavenDB instance. I can do this by hitting http://localhost:8080/ (RavenDB might use another port on your machine if 8080 is already used by another process), and I should see the nice Silverlight client. I can hover the document to see what my data looks like (i.e., JSON document). Please note that the value in Id property is automatically given by RavenDB when the person instance is being stored. For more information about different key generation options, please check http://ravendb.net/docs/theory/document-key-generation.

image

I clicked the Delete button/link in the GridView, and the document is deleted in RavenDB and on GridView.

image

image

In next RavenDB post (which I also promised last time), I will run RavenDB server in the console mode and show the RavenDB management studio which can be used to manage RavenDB server as well as see RavenDB in more detail. Enjoy coding!

Advertisements

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