Umbraco 6 MVC and Dependency Injection

Thanks to some feedback on an Umbraco forum post I have re factored my code and made use of Dependency Injection.

I am using the awesome ninject and if you use the nuget package manager it is so easy its un real.

So navigate to https://www.nuget.org/packages/Ninject.MVC3/3.0.0.6 you can see the nuget install command Install-Package Ninject.MVC3 -Version 3.0.0.6 so install that into your web project.  This will pull down everything also in your app_start folder it will create a class NinjectWebCommon in there you have method


private static void RegisterServices(IKernel kernel)

{
kernel.Bind<ISearchService>().To<SearchService>()
}

You add your bindings there. Next update your surface controller mine looks like


private readonly ISearchService _searchService;
///
/// inject in this dependancy using ninject
///

///
public SearchFormSurfaceController(ISearchService searchService)
{
_searchService = searchService;
}

Its as easy as that. Now you too can be all hip and upto date with all the cool kids!!

Advertisements

3 thoughts on “Umbraco 6 MVC and Dependency Injection

  1. Tom says:

    Hi Ismail,
    I’m trying to get a surface controller to work with v7 using ninject web common and keep getting a parameterless constructor ysod.. Just wondering if you can suggest anything.. It’s almost like web activator isn’t kicking in.
    Cheers,
    Tom

    • leandro koiti says:

      hey Tom, did you find a solution for your problem? I’ve tried Unity and Ninject and both are raising a parameterless constructor exception, couldn’t find anything useful yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s