My Gestalt

Understand the chaotic world by developing meaningful perception through the acquisition of seemingly disparate knowledge.

Collect information in large quantity with precise detail and connect the dots of meaning to form your own picture of the universe.

Act upon it

Knowledge and understanding without action is waste.

Be Grateful

Never underestimate the power of gratitude.

Charity

Forgiveness will make your heart happy. Forgiveness is not about someone deserving it, it's about healing your heart.

RavenDB, VB and WebAPI

What is a great combination of tools for the Modern Web Application?

Since our development language of choice here is VB.Net we need things that can work with it.

RavenDB is a great Document Database with support for VB.Net

WebAPI allows us to make Mobile-ready backends that can also be used with MVC.

Combining these three together is not exactly simple.  This Sample gives you the missing pieces to use this fantastic combination to get a VB.Net Application going.

Using StructureMap, here is the entire RavenApiController:

Imports System.Web.Http
Imports Raven.Client

Public MustInherit Class RavenAPIController
    Inherits ApiController

    Public Property DocumentSession As IDocumentSession

    Protected Sub New()
        StructureMap.ObjectFactory.BuildUp(Me)
    End Sub

End Class
When you add a DocumentSession Property as above, StrucureMap is invoked like this:
'setter injection for API Controllers
SetAllProperties(Sub(x)
                     x.OfType(Of IMembershipProvider)()
                     x.OfType(Of IOAuthProvider)()
                     x.OfType(Of IUserStore)()
                     x.OfType(Of IDocumentSession)()
                 End Sub)

And a GET Method in the WebAPI requiring Authorization looks like this:
    ''' <summary>
    ''' Return a single Id as requested
    ''' </summary>
    <Authorize>
    Public Overloads Function [Get](id As String) As Todo
        ControllerContext.Configuration.Formatters.
            JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore

        Try
            Dim entity = DocumentSession.Load(Of Todo)(id)
            If entity Is Nothing Then
                _Responsecode = HttpStatusCode.NotFound
                ControllerContext.Request.CreateResponse(_Responsecode)
                Return Nothing
            Else
                If member Is Nothing OrElse entity.Owner.href <> member.Id Then
                    _Responsecode = HttpStatusCode.Unauthorized
                    ControllerContext.Request.CreateResponse(_Responsecode)
                End If
                Return Nothing
            End If
            Return entity
        Catch ex As Exception
            'log exception
            ControllerContext.Request.CreateResponse(_Responsecode)
            Return Nothing
        End Try
    End Function

A full Blog Article describing the usage and the Source are available by request.