RavenHQ is a hosted RavenDB solution that relieves you from the responsibility of managing a RavenDB server. Your data is managed in the cloud. You have flexible hosting options based on your needs.
We continue to use our Twitter clone Chirpy in this post, but with one change. We didn’t realize Chirpy was an awesome codeplex project owned by a different team[thanks to thecodejunkie for letting us know that]. To avoid confusion we will be referring to our twitter clone as ChirpyHQ going forward.
RavenHQ seems like a perfect idea for our app since we would like a backend system that scales as ChirpyHQ grows in popularity and a cloud based service is perfect for our needs.
Currently RavenHQ is in Beta and the way to participate is to register your application on AppHarbor and use RavenHQ as an Add-On. So we will register and deploy ChirpyHQ to AppHarbor as well.
Note: When introducing ChirpyHQ in the previous article, we had used a standard Repository pattern. Since then, our readers have commented on how RavenDB apps don’t really need a Repository. Use of patterns is as much an opinion as it is a best practice. We encourage people to choose horses-for-courses. So in this article, we will see the same app using a slightly different pattern.
Getting Started with RavenHQ
To setup a RavenHQ Beta account go through the following steps
Step 1: Register yourself at www.appharbor.com and create a new Application.
Step 2: Next navigate to the ‘AddOns’ page (from the left hand side menu) and scroll down to find RavenHQ
Step 3: Click on “See More” and Install the Plan that works best for you.
Step 4: After installation, the add-on will be available in the application home page.
Click on it to navigate to the add-on details page.
Step 5: Click on Go to RavenHQ to navigate to the Management Console launch page.
Step 6: Click on the “Launch RavenDB Management Studio” link to launch RavenDB Management UI
Step 7: Navigate to the Index page and click on “Create an Index” link to launch the Index creation functionality.
Step 8: Create the Index for the HashTagCount and save it
Step 9: Updating code for RavenHQ. There are a couple of changes to RavenDB ‘Server’ code, to adapt it for RavenHQ.
a. No Multi-Tenancy support. So you cannot specify a ‘Database Name’ in your API calls (Query, Load etc.)
b. The Document Store initiation code needs to supply the API Key. Thus the original code for initializing DocumentStore changes from
c. We have removed the call to EnsureDatabase(databaseName). This is because currently AppHarbor limits you to have one Database per App Installation. So if you have any API calls that depend on databaseName, you’ll need a version without the databaseName. RavenHQ uses the ‘default’ database internally in such cases.
d. Also we are using a connection string parser that pulls out the ApiKey and the Url and passing it to the DocumentStore on instantiation.
Step 10: With that bit of plumbing done, we are all set to deploy our code. But unlike regular web-hosts, AppHarbor can automatically pull code from the popular code repositories like GitHub, BitBucket and CodePlex. It involves a simple hookup process, described below
a. From AppHarbor application console
- i. Click on “Build URL” (it’s a Flash button that copies the URL into clipboard)
- ii. Save the copied url somewhere and extract the query parameter ‘Authorization’. Save this for next step.
b. Link with GitHub or Bitbucket repositories
i. Log in to GitHub or Bitbucket and select the repository
ii. Select Admin
- In GitHub select “Service Hooks”
- In Bitbucket select Services
- Provide API key copied from 10.a.ii
- Provide the Application Name
- For GitHub select the ‘Active’ checkbox
- Save Settings
Step 11: Now commit the code changes and push them to your remote repository.
Step 12: Switch to the AppHarbor application panel, you will see AppHarbor building and deploying the solution
a. In case of build errors, you can click on the Build Status Row to see the status. In the report, you have a ‘Details’ link that has the build output, check that to determine the exact issue. As seen below, our build failed and it was because of Entity Framework bindings.
b. If there are no errors after the build is successful, it gets deployed within a few seconds.
c. Once deployed, you can click on the ‘Go to your application’ link to see the application running.
d. However we notice a glitch here, the App Title is still ‘Chirpy’ instead of ChirpyHQ. So we will modify it and commit the code and let AppHarbor do it’s magic as we can see in the images below
Code Walkthrough – Dissecting ChirpyHQ
In this code sample, we will see an implementation of a ‘repository-less’ pattern. The idea is very simple, RavenDB persists POCOs so why need a heavy repository layer to wrap data extraction. There are arguments in favor and against. I would rather let you pick what’s best for you. In the previous sample, we have already seen what the proposed Repository should look like.
This is a controller class that acts as our base controller. All MVC controllers in our application derive from it.
- The correct document store gets injected into it via constructor injection
- The RavenDB Session is saved in the RavenSession property
- The OnActionExecuting method is overridden and RavenSession is initialized at this point
- The OnActionExecuted method is overridden and the RavenSession persists all change if there are no errors. It is disposed off thereafter.
By deriving all our controllers from RavenController, we ensure that we have a RavenSession ready whenever a Controller action is invoked.
The Home Controller
The HomeController has three Actions, Index (GET), Tag (GET), Search (POST and GET).
The Index action simply gets all the Chirps
Note: This is heavily dependent on RavenDB’s safe by default behavior and will not return a million rows of data if present.
The Tags action gets all the Tags and their total count. This uses the Index that we created earlier.
This simply returns the Search view.
This posts the search term applied and retrieves back all tweets with the given search text in the tag only. However it’s an exact match search. Partial text searches are not honored.
The Administrator Controller
The Administrator is a simple CRUD controller that does the basic CRUD operations on RavenDB. Currently the Home page only navigates to the Create action on the Administrator controller. Rest of the actions doesn’t have a UI in the sample.
Committing to Git and AppHarbor Deployment
With the code all set and our Git+AppHarbor integration ready, we will commit code to Git and it will get automatically pulled into AppHarbor.
Make sure you have the web.config’s RavenDB connectionString set to a non-production URL. AppHarbor will automatically transform the live URL onto this connectionString as long as it is called “RavenDB”.
MVC3, the second Solution and AppHarbor Deployment
In my previous post, some readers were unable to open the web project because of the MVC4 Beta dependency. We understand not everyone lives on the bleeding edge. Last time the community jumped in and helped us with an MVC3 version. This time we will provide the MVC3 version too. We create a separate solution called ChirpyHQ.Mvc3 into which we add an MVC3 Internet web project, move the views, controllers and composition root over and bingo, we have an MVC3 version that looks pretty much like the MVC4 version.
Now you are wondering how will AppHarbor pick which solution? I had the same doubt and @AppHarbor responded with this reference. What it basically means is, if you have two or more solutions, it tries to find AppHarbor.sln first. If not, it tries the ‘Application Slug’.sln . In our case we gave the Slug Name in GitHub is ChirpyHQ so the default MVC4 version will be picked up. If you want to test for MVC3 you can rename the csproj file OR change the slug in the build environment to ChirpyHQ.Mvc3.sln
The app that we discussed here is deployed on http://chirpyhq.apphb.com/ through AppHarbor. It is running RavenHQ as its backend. RavenDB is a scalable document database in it’s own right, but with RavenHQ service it is now available (in Beta currently) for use as a flexible service that grows as per your requirements, without you having to keep up with new hardware or software.
The entire source code for this article is on Github. You can fork it, clone it or download the zip. Have fun with RavenDB and RavenHQ!
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!