Category: RavenDB

Accessing RavenDB Studio when using Embedded Mode

I have seen a few people ask how to go about accessing the RavenDB studio when they use RavenDB in embedded mode, normally you would tend to use embedded mode when unit testing.

In the following article I show you how to get the RavenDB Management Studio working in an MVC 3 application.

  1. File, New, MVC 3 Application as below:- EmbeddedMode
  2. Choose whatever you prefer in the next window, I chose Internet Application:- EmbeddedMode
  3. Now using Nuget add in the RavenDB Embedded package:- EmbeddedMode
  4. Now we need to add in some details for the application to talk to RavenDB, lets do that now, so edit the global.asax.cs file to have the following code:- [sourcecode language=”csharp”] public static IDocumentStore Store { get; set; }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
    filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
    }

    protected void Application_Start()
    {
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    Store = new EmbeddableDocumentStore {
    DataDirectory = "Data",
    UseEmbeddedHttpServer = true
    };
    Store.Initialize();
    }
    [/sourcecode]

  5. Now in order to be able to view the RavenDB Management Studio we need to locate the file Raven.Studio.xap from the packages folder which was created when we added in the Nuget package above.

    The packages folder is located in the directory above the folder in which your solution currently resides, once you locate the packages folder locate the RavenDB-Embedded(version number) folder in my case this is RavenDB-Embedded.1.0.701 – the file we are looking for is in the packages\RavenDB-Embedded.1.0.701\lib\net40\

    Copy the Raven.Studio.xap to the root of your website and now try running your MVC website.

  6. Once that loads up now browse to http://localhost:8080 and the RavenDB Studio should load up – and that’s you.
  7. If you see this:- EmbeddedMode

    It means you havent copied the Raven.Studio.xap to the root of your website.

I hope this helps someone out.




Profiling your RavenDB MVC application

Your application is starting to take shape and you want to be able to quickly see what’s going on when using RavenDB.

Step forward the in-built profiling dll which is very easy to set up and get going with, lets implement that along with MiniProfiler.

  1. Add the Nuget package MiniProfiler.RavenDb to your application
  2. Add the Raven.Client.MvcIntegration dll which can be found in the Client folder when you download the source for RavenDB
  3. Add the following to your global.asa.cs file:-
    [sourcecode language=”csharp”] protected void Application_BeginRequest()
    {
    if (Request.IsLocal)
    {
    MiniProfiler.Start();
    }
    }
    [/sourcecode]

    and then :-

    [sourcecode language=”csharp”] protected void Application_EndRequest()
    {
    MiniProfiler.Stop();
    }
    [/sourcecode]
  4. Add the following to your global.asa.cs file in the Application_Start event:- [sourcecode language=”csharp”] Raven.Client.MvcIntegration.RavenProfiler.InitializeFor(Store);
    MvcMiniProfiler.RavenDb.Profiler.AttachTo((DocumentStore)Store);
    [/sourcecode]
  5. Lastly add the following lines into your _Layout.cshtml file in the Shared folder, under Views:- [sourcecode language=”csharp”] @MvcMiniProfiler.MiniProfiler.RenderIncludes()
    @Raven.Client.MvcIntegration.RavenProfiler.CurrentRequestSessions()
    [/sourcecode]

That’s it – you have now adedd the built in profiler and MiniProfiler for RavenDB, run your application and you can
see that in the top left hand corner you can view the details of whats going on in both profilers:-

MiniProfiler for RavenDB looks like this:-

MiniProfiler

and this:-

MiniProfiler 2

while with the RavenDB profiler we see something like this:-

RavenDB Profiler

The in-built profiler from RavenDB shows us how long the query took, the status, result, Method, Url, Query and Actions as well as the actual request details.

All in all both profilers are very hand and very easy to install – go get them installed.




RavenDB Hints and Tips

RavenDB Here is a list of hints and tips if you’re looking for some help with RavenDB.


  • Sign up for the mailing list where you can see other people’s previous questions and answers to problems on the View the Google Group.
  • To make sure RavenDB doesn’t clash with routing used in MVC change the global.asa.cs file from:-

    {controller}/{action}/{id} to {controller}/{action}/{*id} – Note the * next to the {id} or you can do this.

  • If I choose the Embedded version of RavenDB how do I start the server – to start the RavenDB server locate the RavenDB folder and then look for the server folder and then run Raven.Server.Exe
  • If I install RavenDB using Nuget then what do I do, locate the packages folder which is in the folder one above the solution you’ve added it to on the file system, locate the RavenDB folder and then look for the server folder and then run Raven.Server.Exe
  • How do you view the RavenDB Studio – easiest way is to browse to http://localhost:8080, that’s the default location of installation when you first run the Raven.Server.Exe
  • Unit testing – how best to go about it – the recommended way to go about this is by using the EmbeddedDocumentStore
    which is basically all done in memory, you can set it up like this:-

    [sourcecode language=”csharp”] var documentStore = new EmbeddableDocumentStore
    {
    DataDirectory = "Data",
    RunInMemory = true,
    UseEmbeddedHttpServer = true
    };
    [/sourcecode]
  • Good RavenDB Samples RavenOverFlow and Racoon Blog

If your stuck with something give me a shout I’ll see if I can shed some light, I’ll be adding more tips as I learn more RavenDB.




Skillsmatter RavenDB Course Review

RavenDBOn Feb 28th and 29th of February I attended the Skillsmatter RavenDB course in London with Itamar Syn-Hershko, course details here.

Itamar is a developer on RavenDB and works with Oren aka Ayende from Hibernating Rhinos in Israel and this was a chance to learn from one of the guys who works on RavenDB.

The course was a 2 day course covering a wide range of features of RavenDB including:-

  • Principles of RavenDB
  • Getting started with RavenDB
  • CRUD operations
  • The basics of querying
  • Advanced querying
  • Document based modeling
  • Using Includes for better performance
  • Caching and cache utilization
  • Creating static indexes
  • Map/Reduce
  • Extending RavenDB
  • Replication, Sharding & Scale
  • Full text search

The course covers a lot of content and you’re given exercises to do which was very useful in finding out how to properly use parts of RavenDB as well as learn how to go about a certain task the recommended way.

I have come away from the course with a much better understanding of the how and why and that’s the main reason why I wanted to go on the course in the first place and I cant wait to get started using RavenDB on our upcoming project.

From asking a number of questions about RavenDB its clear to me that RavenDB has a lot to it, there is a lot to learn about it but it has a number of great features which are very powerful and yet very simple to use, some of the things that stood out for me where the following:-

  • Safe by default – this means that it stops the user from writing code that is requesting too much data, and limits the number of requests that each session is allowed to make.
  • Lucene – searching using Lucene provides a fantastic way to search and in Raven we can utilise the power of Lucene to even have full text search capabilities.

The course itself cost £1250 and was well worth it – anyone thinking about going on the upcoming courses I would recommend it to them for definite, details on the next RavenDB course they are hosting can be found here.



Skillsmatter RavenDB Course

On the 28th and 29th Feb I attended the RavenDB Course at Skillsmatter in London, details on the next RavenDB course they are hosting can be found here.

Skillsmatter is located in central London and very easily accessible, being a short taxi ride from Liverpool Street train station – upon arrival we were warmly welcome and asked to register and make our food choices for the first day of the course.

Skillsmatter is quite a fair size and has several training rooms upstairs and an area downstairs for talks and presentations, many which are given by user groups and can hold id say about 70 people (with room for more).

Our course took part in one of the smaller rooms which was perfect for our course, plenty of power/access points, projecter and whiteboard(s), all well laid out and just what you’d expect really.

The food at lunch was great and the staff there were very friendly and very helpful, really nice people.

On day 2 of the course we went to the pub for lunch and again everyone was very friendly and keen to ask us about how we were enjoying the course and keen to know if we were enjoying out training course.

For anyone looking for a great training venue or a place to hold a user group meeting in London then this would be an ideal place to go – the courses they run are great and the list of courses is very good indeed



Host your RavendDB based MVC app on Appharbor

RavenDBIn this blog post I will walk through how to host an MVC application which uses RavenDB as the back-end document database hosted up on Appharbor, Appharbor now allows us to host RavenDB in the cloud.

In the blog post I am going to make several assumptions, the post itself is more to do with getting an application deployed and running up on AppHarbor using RavenDB as the document database.

I am assuming you have you have a github account and know how to get your code up onto github, an Appharbor account and know how to deploy your code to github to Appharbor.

If you need some help with doing this then check here.

Ok lets create a brand new MVC Web application project, I used MVC 3 application

Raven2

And then I chose the internet template.

Raven3

You can either download RavenDB from RavenDB.net or use Nuget to get RavenDB. For the demo I just installed it using Nuget
as follows, within Visual Studio goto Tools, Library Package Manager and then select Package Manager Console and then type:-

Raven

RavenDB is now downloaded using NuGet and you will find it within the packages folder in the same location on disk as your project solution files, to start RavenDB locat the packages folder and then go to the RavenDB.1.0.616 folder and then the Server folder and double click on Raven.Server.exe as below:-

Raven4

By default RavenDb runs on http://localhost:8080, once started you can view the Raven Studio from within a browser, and you should see something like this:- (if it says Create Sample Data then click on the button to create sample data)

Raven10

Moving back to our MVC Application we now need to wire a couple of things up to use RavenDB as below:-

In the global.asax add in the following in the Application_Start event:-

[sourcecode language=”csharp”] protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);

var parser = ConnectionStringParser<RavenConnectionStringOptions>.FromConnectionStringName("RavenDB");
parser.Parse();

Store = new DocumentStore
{
ApiKey = parser.ConnectionStringOptions.ApiKey,
Url = parser.ConnectionStringOptions.Url,
}.Initialize();
}
[/sourcecode]

Add in a connection string in your web.config as follows:-

[sourcecode language=”csharp”] <connectionStrings>
<add name="RavenDB" connectionString="Url=http://localhost:8080" />
</connectionStrings>
[/sourcecode]

All new Controllers will inherit from RavenController which is below:-

[sourcecode language=”csharp”] public class RavenController : Controller
{
public IDocumentSession RavenSession { get; private set; }

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.IsChildAction)
return;
RavenSession = MvcApplication.Store.OpenSession();
base.OnActionExecuting(filterContext);
}

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.IsChildAction)
return;
using (RavenSession)
{
if (filterContext.Exception != null)
return;

if (RavenSession != null)
RavenSession.SaveChanges();
}
}
}
[/sourcecode]

Below is some example code for some basic CRUD.

[sourcecode language=”csharp”] public class AlbumController : RavenController
{
public ActionResult Index()
{
var model = this.RavenSession.Query<Album>().ToList();
return View(model);
}

public ActionResult Create()
{
var model = new Category();
return View(model);
}

[HttpPost] public ActionResult Create(Album album)
{
this.RavenSession.Store(album);
return RedirectToAction("Index");
}

public ActionResult Edit(string id)
{
var model = this.RavenSession.Load<Album>(id);
return View(model);
}

[HttpPost] public ActionResult Edit(Album album)
{
this.RavenSession.Store(album);
return RedirectToAction("Index");
}

public ViewResult Details(string id)
{
var model = this.RavenSession.Load<Album>(id);
return View(model);
}

public ActionResult Delete(string id)
{
var model = this.RavenSession.Load<Album>(id);
return View(model);
}

[HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(string id)
{
this.RavenSession.Advanced.DatabaseCommands.Delete(id, null);
return RedirectToAction("Index");
}
}
[/sourcecode]

Within Appharbor choose RavenDB as an add-on, appharbor will provision you with a connection string if you choose
RavenDB as an add-on, wait a couple of seconds and you will be supplied with a connection string for your RavenDB instance in the cloud.

Step 1

Step1

Step 2

Step 2

Step 3

Step 3

Step 4
Step 4

Step 5
Step 5

This setting is the one you will change to within your web.config, for testing locally use http://localhost:8080 but for deploying to the cloud use the connection string setting supplied to us by Appharbor.

Getting RavenDB from NuGet means quite a large download, we dont want to have to upload all of this content to github so we can do a nice little thign within our solution, roght click on the entire solution within Visual Studio and choose:-

Package Restore

This means our github repository doesnt have to contain all of the Nuget packages and will pull them down for us when building.

Once your code is building and running locally send it up to github and use the service hook to deploy to Appharbor.

If you come across any build errors then you may have to turn customerrors=”Off” in your web.config as well as checking the build log on Appharbor to see what went wrong.

At this point we should have a working MVC application which is using RavenDB as the back-end document database – sweet.

If I missed anything or anyone has any questions or comments please tweet me @gsuttie or leave a comment.




RavenDB Useful Tools

Here I will list all of the useful tools I come across when using RavenDB.

So here is the list:-

  • RavenDB add on for Glimpse Glimpse RavenDB.
  • RavenDB Mini Profiler – more info can be found here and here.
  • LinqPad which can be found here is a great tool which you can use to query RavenDB using Linq statements – if you have the correct third party driver installed.

    To learn how to get Linqpad to query RavenDB you will find all you need to do here.

  • Rest-Client which can be found here is extremely handy for testing Restful webservices which RavenDB is.

    RestClient

    Click the downloads tab and then select the following:-

    RestClient

    Once selected rest-client will fire up, it’s very straight forward to use, put in the url to your database where RavenDB is running and then you can use the POST, GET and so on as below:-

    RestClient3

  • Fiddler 2 which can be found here

    Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect traffic, set breakpoints, and “fiddle” with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language

    We can use Fiddler to monitor what is going on when we work with RavenDB as well as use it to inject documents on the fly which is shown in the Tekpub video series.

  • If anyone has any other useful tools for working with RavenDB then please let me know and I will add to this list.




RavenDB Video Tutorials

RavenDB is fast becoming pretty popular and 2 of the best resources I find for learning is TekPub and Pluralsight, these sites are for developer training and require you to pay for them.

Both sites offer subscriptions but Tekpub also offers the ability to pay for a single course such as the RavenDB course, which is a nice addition.

  • Tekpub Course Details:- Tekpub RavenDB course

    In this production, Oren Eini walks Rob through the various aspects of RavenDB – from the basics through to advanced Administrative Tasks. Along the way you’ll learn how to query with the core Lucene engine, how to index your documents for searches, backups, sharding, replication to SQL Server, and how to plug all of this in to an ASP.NET MVC application.

    This series is In Production which means we are actively recording and producing content for it. Currently we have 10-12 episodes planned.”

  • Pluralsight Course Details:- Pluralsight RavenDB course

I have watched both courses and they are both superb and I encourage you to check them both out.




Adding RavenDB to an MVC application

Adding RavenDB to an MVC 3 application is very straightforward – in this post I will add RavenDB via Nuget and it will run RavenDB in embedded mode.

  1. Start Visual Studio 2010 and Add a new MVC 3 Application, choose Internet Application from the project template option. This will create our new app ready to add RavenDB to.
  2. Ok now to Add RavenDB into your application lets use Nuget to add in the RavenDB Embedded package, go to the Tools Menu and then select Library Package Manager and then Manage Nuget Packages – search for RavenDB Embedded as seen below:-

    RavenDB via Nuget

    Click install and wait a little, it’s a larger package compared to most, also accept the license agreement.

  3. That’s it RavenDB has been added to our MVC 3 application, in the next blog post we will make use of RavenDB in our application.


Learning RavenDB

This is the first post in a series I will be doing on learning RavenDB, RavenDB is a NoSQL document database written by Ayende Rahien (@ayende on twitter)
The concept is quite new to me and so I have spent a few hours looking at RavenDB.

For those of you who are new to RavenDB and the whole NoSql thing take a look here:-

Updated: This link is to a blog by one of the devs who works on RavenDB.