DNN Community Blog

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.


Look Mom, NoSQL! – Using RavenDB in a DotNetNuke Module

In a number of articles last fall on my personal blog I discussed NoSQL Databases in general and showed how RavenDB – a .NET NoSQL Database - could be used as the Data Store for an ASP.NET MVC Application.

In this post, I will demonstrate how RavenDB can be used as the Data Store for a DotNetNuke Module. (I will assume that readers are familiar with NoSQL Databases and RavenDB in particular, but if you are not familiar then please read those earlier articles)

First lets assume I have created a Web Application Project for my DotNetNuke Module.  My module, surprise, surprise, is going to be a Tasks or To-do List module.  A task is defined by the Task class - see Listing 1.

Listing 1: The Model class

public class Task
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsComplete { get; set; }
}

The first point to note is that Raven expects our “key” field to be called Id and it also assumes it is a string.  Our main View Module Control will display a List of Tasks - see Listings 2 and 3.

Listing 2: The TaskList Module Control

Task List

The Html in Listing 2 has been made more readable by removing some of the attributes associated with the styling of the DataGrid (e.g. Column Width, Header Text, etc.

Listing 3: The TaskList Module Control OnLoad method

protected override void OnLoad(System.EventArgs e)
{
    base.OnLoad(e);

    tasksGrid.DataSource = TaskController.Instance.GetTasks();
    tasksGrid.DataBind();
}

The TaskController could be any DotNetNuke Controller class.  I have elected to use the new “Testable” Controller pattern, based on the ServiceLocator base class.  GetTasks returns an IList which is bound to the DataSource of the grid. 

Before I explain how we connect with a RavenDB database lets just look at the resulting Module Control when the module is added to the page.

Figure 1: The Tasks  Module

image

RavenDB comes in two flavors - a server version and an embedded version.  We will use the embedded version and to add RavenDB Embedded to our Module, we will use NuGet (Figure 2). To use Nuget in Visual Studio right-click on the References folder and select “Manage NuGet Packages”.

Figure 2: Adding a new Nuget Package

image

In the Package Manager dialog enter RavenDB to get a list of RavenDB packages (Figure 3).

Figure 4: Install RavenDB using NuGet

image

Select the RavenDB Embedded package and click Install to add RavenDB Embedded and any dependencies to your project. (In the image the green check marks show that I have already installed RavenDB in my project).

Everything in RavenDB is accessed through an instance of IDocumentStore, in the case of RavenDB Embedded an  EmbeddableDocumentStore, so we can create a utility method to create our IDocumentStore instance (Listing 4)

Listing 4: CreateDocumentStore method

public static IDocumentStore CreateDocumentStore()
{
    var instance = new EmbeddableDocumentStore
        {
            ConnectionStringName = "RavenDB",
            Conventions = {IdentityPartsSeparator = "-"}
        };
    instance.Initialize();
    return instance;
}

This will create an IDocumentStore instance based on the named Connection String - RavenDB - which we need to add to web.config (Listing 5).  Note that for this connectionString to work we will also need to create the folder - RavenTasks in the Website’s App_Data folder.

Listing 5: RavenDB Connection String


  
  

In Listing 4 I initialized the DocumentStore with its Connection String and a “convention” - I set the IdentityPartsSeparator to “-“. RavenDB identifies items using the plural of the type name and the Id of the object (eg. tasks-123), separated by the IdentityPartsSeparator. The default separator is “/”, which would cause problems if the id is passed in a url, so I have elected to use a dash which is safer.

Now that we have installed RavenDB, and configured a Document Store, we can create our methods in the TaskController.

Listing 6: Implementing GetTasks

public IList GetTasks()
{
    IList tasks;
    using (var ds = RavenDocumentStore.CreateDocumentStore())
    {
        using (var session = ds.OpenSession())
        {
            tasks = session.Query().ToList();
        }
    }
    return tasks;
}

To get a list of tasks, we first get our IDocumentStore instance by calling our utility method.  Then using the document store we can open a session to communicate with it.  The Session object’s Query method allows us to get an IEnumerable from the Document Store.  Finally, we ensure that the Session and DocumentStore objects are disposed of by using the using statement.

And that’s it - its pretty easy.  Listing 7 shows the remaining methods necessary to delete, get and save Tasks - it’s a pretty standard Unit of Work pattern.

Listing 7: The remaining methods

public void DeleteTask(string taskId)
{
    using (var documentStore = RavenDocumentStore.CreateDocumentStore())
    {
        using (var session = ds.OpenSession())
        {
            var task = session.Load(taskId);
            session.Delete(task);
            session.SaveChanges();
        }
    }
}

public Task GetTask(string taskId)
{
    Task task;
    using(var ds = RavenDocumentStore.CreateDocumentStore())
    {
        using (var session = ds.OpenSession())
        {
            task = session.Load(taskId);
        }
    }
    return task;
}

public void UpdateTask(Task task)
{
    using (var ds= RavenDocumentStore.CreateDocumentStore())
    {
        using (var session = ds.OpenSession())
        {
            session.Store(task);
            session.SaveChanges();
        }
    }
}

The rest of the module is normal DotNetNuke module development.  In fact the only piece that is different is the implementation of the TaskController class.  This is a very simple example.  We could improve it by avoiding getting a new Document Store every time, by using a Service Location or Dependency Injection Container.  But, it is clear from this example that using RavenDB is very easy.

This post is reproduced from my personal blog (with some modification)

Comments

There are currently no comments, be the first to post one.

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (15)
Alec Whittington (11)
Alex Shirley (10)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (32)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Bogdan Litescu (1)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (209)
Chris Paterra (55)
Clinton Patterson (41)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (154)
Dave Buckner (2)
David Poindexter (4)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (74)
Geoff Barlow (10)
George Alatrash (6)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (274)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matthias Schlomann (15)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (28)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Peter Donker (54)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott S (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Timo Breumelhof (24)
Tony Henrich (3)
Torsten Weggen (2)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (165)
William Severance (5)

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  


Copyright 2017 by DNN Corp Terms of Use Privacy
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out