1

I want to use RavenDB for my personal blog project built on ASP.NET MVC3.

I like to have multiple projects in my solution, so I would have one web project and another class library as the blog engine using RavenDB as its datastore that the web project would call.

How should I setup RavenDB so I can use it from a class library?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Johan Alkstål
  • 5,225
  • 9
  • 36
  • 48

2 Answers2

0

One option you might consider is using Inversion of Control / Dependency Injection to create an instance of DocumentStore as a singleton and inject it into your services via the constructor. I'm using Munq, so my example will use that, but there are lots of other IOC frameworks out there to consider.

// In your Global.asax Application_Start
// MUNQ.  This will give you a RavenDB IDocumentStore as Singleton
var ioc = MunqDependencyResolver.Container;
ioc.Register<IDocumentStore>(c => InitializeRaven()).WithLifetimeManager(new ContainerLifetime());

// Initialize yourself some RavenDB
public static IDocumentStore InitializeRaven()
{
    DocumentStore instance = new DocumentStore { Url = "http://localhost:8080/" };
    instance.Initialize();
    return instance;
}

This will get you a single instance of DocumentStore to use throughout the lifetime of your application.

Here's a Stack Overflow question that may give your some additional guidance (Autofac):

ASP.NET MVC 3, RavenDB, & Autofac Issue Plus 2 Other Autofac Questions

And an example of MSNBC using it (Ninject):

http://development.msnbc.msn.com/_news/2011/08/19/7419461-ninjectmvc3-dependency-injection-in-30-seconds-or-less?lite

Community
  • 1
  • 1
Jeff Mitchell
  • 1,459
  • 1
  • 17
  • 33
-1

Did you read some tutorials/documentation about RavenDB? You create a DocumentStore that will allow you to connect and query the database:

public IEnumerable<Company> GetCompanies()
{
    using (var dc = new DocumentStore("localhost", 8080).Initialize())
    using (var session = dc.OpenSession())
    {
        return session
            .Query<Company>("regionIndex")
            .Where("Region:Europe")
            .ToArray();
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928