Ninjecting Neo4jClient into MVC5/4/3

First, create your new MVC project (or load your existing one!)

Then add the Ninject.MVC nuget package:

image

We’ll also want the Neo4jClient package

image

Now we have those two, we can hook it up, first off we need to tell Ninject how to bind to Neo4jClient, I use a Module for this, so add this class to your project (usually I have them in a sub-folder called ‘Modules’ in the App_Start folder – but it can be anywhere):

public class Neo4jModule : NinjectModule
{
    /// <summary>Loads the module into the kernel.</summary>
    public override void Load()
    {
        Bind<IGraphClient>().ToMethod(InitNeo4JClient).InSingletonScope();
    }

    private static IGraphClient InitNeo4JClient(IContext context)
    {
        var neo4JUri = new Uri(ConfigurationManager.ConnectionStrings["Neo4j"].ConnectionString);
        var graphClient = new GraphClient(neo4JUri);
        graphClient.Connect();

        return graphClient;
    }
}

Now we just need to tell Ninject to load the module, so in the NinjectWebCommon.cs file (in the App_Start folder) edit the ‘RegisterServices’ method (at the bottom of the file) so it looks like:
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Load<Neo4jModule>();
} 

Lastly, we just need to inject into our controller, which is a case of adding a constructor (or modifying an existing one) to take an ‘IGraphClient’ instance:

private readonly IGraphClient _graphClient;
public HomeController(IGraphClient graphClient)
{
    _graphClient = graphClient;
}
 

Now the controller has an instance of the graphclient to use as and when it pleases:

public ActionResult Index()
{
    ViewBag.NodeCount =  _graphClient.Cypher.Match("n").Return(n => n.Count()).Results.Single();

    return View();
}
image

Obviously extending this, you could add a base ‘Neo4jController’ which any controllers requiring Neo4j override.

Print | posted @ Tuesday, April 1, 2014 8:23 AM

Comments on this entry:

Gravatar # re: Ninjecting Neo4jClient into MVC5/4/3
by Kevin Anderson at 6/3/2015 10:39 AM

Thank you for the excellent blog detailing Neo4JClient and Ninject.
I'm having some difficulty compiling your code, but am a little lost as to why.
Error detail...
Error 1 Inconsistent accessibility: parameter type 'IContext' is less accessible than method 'MVC_5_Skeleton2.App_Start.Modules.Neo4jModule.InitNeo4JClient(IContext)' c:\users\kevin\documents\visual studio 2013\Projects\MVC 5 Skeleton2\MVC 5 Skeleton2\App_Start\Modules\Neo4jModule.cs 18 37 MVC 5 Skeleton2

and

Error 2 'IContext' is inaccessible due to its protection level c:\users\kevin\documents\visual studio 2013\Projects\MVC 5 Skeleton2\MVC 5 Skeleton2\App_Start\Modules\Neo4jModule.cs 18 53 MVC 5 Skeleton2

I know I'm probably being a bit thick, but I'm just not seeing the cause.
Best regards,
Kevin
Post A Comment
Title:
Name:
Email:
Comment:
Verification: