Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

RavenDB Provider

Daniel J. Summers edited this page Aug 3, 2019 · 3 revisions

Nancy.Session.RavenDB

A RavenDB-backed persistable session store for use with Nancy.

(This is a community project with no formal relationships with the awesome Nancy project or the outstanding RavenDB project.)

Enabling the Provider

The RavenDB IDocumentStore object must be initialized up front, and should be implemented as a singleton. The examples below register an instance of an IDocumentStore, then utilize it to register the RavenDB session store.

C# Example

namespace ExampleNancyApp
{
    using Nancy;
    using Nancy.Bootstrapper;
    using Nancy.Session.Persistable;
    using Nancy.Session.RavenDB;
    using Nancy.TinyIoc;
    using Raven.Client.Documents;
    using System;

    public class ApplicationBootstrapper : DefaultNancyBootstrapper
    {
        public static IDocumentStore DocStore = new Lazy<IDocumentStore>(() =>
        {
            var store = new DocumentStore();
            // configure store
            return store.Initialize();
        }).Value;

        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            // Register the document store
            container.Register(DocStore);

            // Register the session store
            PersistableSessions.Enable(pipelines,
                new RavenDBSessionConfiguration(container.Resolve<IDocumentStore>()));
        }
    }
}

F# Example

module ExampleNancyApp

open Nancy
open Nancy.Bootstrapper
open Nancy.Session.Persistable
open Nancy.Session.RavenDB
open Raven.Client.Documents

/// Set up a database connection
let docStore = lazy (() ->
    let store = new DocumentStore()
    // configure options
    store.Initialize())

type ApplicationBootstrapper() =
  inherit DefaultNancyBootstrapper()
  override this.ApplicationStartup (container, pipelines) =
    base.ApplicationStartup (container, pipelines)
    // RavenDB connection
    container.Register<IDocumentStore>(docStore.Force ())
    |> ignore
    // Sessions
    PersistableSessions.Enable
      (pipelines, RavenDBSessionConfiguration(container.Resolve<IDocumentStore>()))

VB.NET Example

Imports Nancy
Imports Nancy.Bootstrapper
Imports Nancy.Session.Persistable
Imports Nancy.Session.RavenDB
Imports Nancy.TinyIoc
Imports Raven.Client.Documents

Namespace ExampleNancyApp

    Public Class ApplicationBootstrapper
        Inherits DefaultNancyBootstrapper

        Private Shared Function InitStore() As IDocumentStore
            Dim store = New DocumentStore()
            Return store.Initialize()
        End Function

        Public Shared DocStore As IDocumentStore = New Lazy(Of IDocumentStore)(InitStore).Value

        Protected Overrides Sub ApplicationStartup(container As TinyIoCContainer, pipelines As IPipelines)
            MyBase.ApplicationStartup(container, pipelines)
            ' Register the document store
            container.Register(Of IDocumentStore)(DocStore)
            ' Register the session store
            PersistableSessions.Enable(pipelines,
                                       New RavenDBSessionConfiguration(container.Resolve(Of IDocumentStore)))

        End Sub
    End Class
End Namespace

Configure It

As you may have noticed in the examples above, the configuration object takes the IDocumentStore as its only parameter. (There is a zero-parameter constructor as well, but if you do not specify a document store before enabling sessions, the provider will throw an exception.) This is only required configuration option.

In addition to the options provided by Nancy.Session.Persistable, the following options are available (SDHP uses all the defaults):

  • Database (string - default: IDocumentStore-configured database)

    This is the RavenDB database that will be used for persistence.

  • Collection (string - default: "Sessions")

    This is the document collection within the RavenDB database that will be used for persistence; by convention, this name should be a plural noun (ex. "Visitors" instead of "Visitor").