I came up with my own solution using a singleton pattern.
What I did was create a singleton, that exposes a public IDocumentStore property, in the data layer of my application. It utilizes a static constructor which runs upon first request for static property (executed in Application_Start), and in turn instantiates an IDocumentStore object. The initial instance is then then returned for each reference to DocStore.Instance in a base controller and in other layers of my application (like the security layer f.ex.)
public sealed class DocStore
{
protected static readonly IDocumentStore instance;
static DocStore()
{
// instantiate documentStore
instance = new DocumentStore { ConnectionStringName = Constants.ConnectionStrings.XXXXX };
instance.Initialize();
// instantiate tenants
try
{
instance.DatabaseCommands.EnsureDatabaseExists(Constants.Tenants.XXXXX);
}
catch (Exception ex)
{
//TODO: catch exception
throw ex;
}
// initialize indexed
try
{
InitializeIndexes(instance);
}
catch (Exception ex)
{
//TODO: catch exception
throw ex;
}
}
private DocStore()
{
}
public static IDocumentStore Instance
{
get { return instance; }
}
private static void InitializeIndexes(IDocumentStore store)
{
// builds all indexes defined in XXXXX.Data.dll
var dataCatalog = new CompositionContainer(new AssemblyCatalog(typeof(DocStore).Assembly));
IndexCreation.CreateIndexes(dataCatalog,
store.DatabaseCommands.ForDatabase(Constants.Tenants.XXXXX),
store.Conventions);
}
}