Thursday, January 17, 2013

RavenDB And Wrapping Up

Last, but not least, we get to RavenDB. Here is that implementation of my code...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Client;
using Raven.Client.Document;
using Raven.Abstractions;
using Raven.Database;

namespace ConsoleRaven1
{
    class Program
    {
        static void Main(string[] args)
        {
            DocumentStore store = new DocumentStore();
            store.Conventions.IdentityPartsSeparator = "-";
            store.DefaultDatabase = "console1";
            store.Url = "http://ross-pc:8080";
            store.Initialize();

            var session = store.OpenSession();

            Console.Write("First name: ");
            string first = Console.ReadLine();

            Console.Write("Last name: ");
            string last = Console.ReadLine();

            Name input = new Name() { FirstName = first, LastName = last };
            int dupCount = (from s in session.Query<Name>()
                            where s.FirstName == first && s.LastName == last
                            select s).Count();

            if (dupCount != 0)
            {
                Console.WriteLine("{0} {1} is already on file", first, last);
                Console.WriteLine("dupCount = {0}", dupCount);
            }
            else
            {
                Console.WriteLine("Proceeding...");
                session.Store(input);
                session.SaveChanges();
                Console.WriteLine("{0} {1} has been added", first, last);
            }

            Console.WriteLine("Names include:");

            var names = from s in session.Query<Name>()
                        orderby s.LastName, s.FirstName
                        select s;

            foreach (Name name in names)
            {
                Console.WriteLine("{0} {1} {2}", name.Id, name.FirstName, name.LastName);
            }

            store.Dispose();
        }
    }

    class Name
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

First of all, I used the "RavenDB.Client" NuGet package. There is also a package for an embedded server, which I believe is called "RavenDB.Embedded".

This Name class is the cleanest of the 3. No data annotations were needed. By default, RavenDB IDs are structured as Class/Number, like "Name/33". You can make that more web-friendly by setting the IdentityPartsSeparator, as I did. So, the same document ID will be rendered "Name-33" in my code. You also noticed I specified a URL. You could probably use the name of any computer you have network access to. Initializing starts the server instance with the settings you used. You need a Session in order to actually access your data. Disposing the data store helps clean up memory.

If you're familiar with the 2008 (or more recent) version of the .NET Framework, the LINQ should be recognizable. The API calls for querying are minimal. Plus, the data object doesn't need to be massaged to be stored.

RavenDB is NOT a Windows Service. You have to manually start and stop the server, unless you you want to embed its functionality into your project. The nice thing is RavenDB comes with a browser-based administration console, which was been recently redesigned. If the server is running, just enter the same url as in the code and log in like you would Windows.

Couchbase has one as well, and you can query CouchDB using URLs if you know the right syntax. "http://localhost:5984/console1/_design/names/_view/all" is an example. MongoDB uses a command-line EXEs  for administration.

Well, that's all for now. Ciao!    

No comments:

Post a Comment