views:

118

answers:

1

I am new to ninject using ninject 2.0. My application is hosted in asp.net mvc.

Now i don't know how to access kernel created in my class library.

I think i should create kernel in global.aspx and load all modules in it. But how can i make it available throughout application?

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);

        IKernel kernel = new StandardKernel(new ServiceModule());
    }

Where should Kernel created above go? and how to access it?

+1  A: 

Look on the Ninject Dojo's extensions section for Ninject.Web.Mvc. This will show you the default pattern for managing such things (go look at the source).

You can either use it as-is (most people do), or customise it to your liking.

You also tagged this with CSL. Before you do use Ninject for Service Location, have a search around for why this is considered an antipattern (the extension referred to by default does not make the Kernel public from Global - you'd have to add a public propery yourself (but the WCF integration on the other hand does)

Ruben Bartelink
@mamu: Anybody home? This any use? Any follow-ups, will answer...
Ruben Bartelink
I am making it work by injecting kernel with mvc extention where ever i need it right now. but that looks like hack to inject kernel as it makes so much dependent on ninject.Also accessing global property of HttpApplication is not so convincing. What is your take on using CSL for kernel only. so i pieces where i don't want to auto inject, i can get kernel from CSL and do IOC?
mamu
Also i am just talking about pieces where i don't want to do constructor injection. Most of the implementation is around automatic constructor injection which is fine.
mamu
[As you pretty much know and are saying] You dont want to do Service Location, period - any time you use it you're undoing all your good work. See http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject and http://stackoverflow.com/questions/2710718/how-do-i-handle-classes-with-static-methods-with-ninject/2712271#2712271 for ways of not giving the kernel (or a Ninject-specific class) directly to your class. (esp. the Func<T> injection). I wish the docs were more complete on this...
Ruben Bartelink