Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Managing RavenDB Document Store Startup

DZone's Guide to

Managing RavenDB Document Store Startup

· Database Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

The RavenDB’s document store is your main access point to the database. It is strongly recommended that you’ll have just a single instance of the document store per each server you are accessing. That usually means that you have to implement a singleton, with all the double checked locking nonsense that is involved in that. I was giving a course in RavenDB last week and I stumbled upon a very nice coding pattern:

    public static class Global
    {
        private static readonly Lazy<IDocumentStore> theDocStore = new Lazy<IDocumentStore>(()=>
            {
                var docStore = new DocumentStore
                    {
                        ConnectionStringName = "RavenDB"
                    };
                docStore.Initialize();

                //OPTIONAL:
                //IndexCreation.CreateIndexes(typeof(Global).Assembly, docStore);

                return docStore;
            });

        public static IDocumentStore DocumentStore
        {
            get { return theDocStore.Value; }
        }
    }

This is a very readable code, and it handles pretty much all of the treading stuff for you, without obscuring what you really want to do.

And what about when you have multiple servers? How do you handle it then? Same idea, taken one step further:

    public static class Global
    {
        private readonly static ConcurrentDictionary<string, Lazy<IDocumentStore>> stores = 
            new ConcurrentDictionary<string, Lazy<IDocumentStore>>();
        
        public static IDocumentStore GetDocumentStoreFor(string url)
        {
            return stores.GetOrAdd(url, CreateDocumentStore).Value;
        }

        private static Lazy<IDocumentStore> CreateDocumentStore(string url)
        {
            return new Lazy<IDocumentStore>(() =>
                {
                    var docStore = new DocumentStore
                        {
                            ConnectionStringName = url
                        };
                    docStore.Initialize();

                    //OPTIONAL:
                    //IndexCreation.CreateIndexes(typeof(Global).Assembly, docStore);

                    return docStore;
                });
        }
    }

This is nice, easy and the correct way to handle things.




Get comfortable using NoSQL in a free, self-directed learning course provided by RavenDB. Learn to create fully-functional real-world programs on NoSQL Databases. Register today.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}