views:

346

answers:

2

What I am looking is something similar to the below (http://github.com/ninject/ninject.web.mvc):

README.markdown

This extension allows integration between the Ninject core and ASP.NET MVC projects. To use it, just make your HttpApplication (typically in Global.asax.cs) extend NinjectHttpApplication:

public class YourWebApplication : NinjectHttpApplication { public override void OnApplicationStarted()
{ // This is only needed in MVC1 RegisterAllControllersIn("Some.Assembly.Name"); }

public override IKernel CreateKernel() { return new StandardKernel(new SomeModule(), new SomeOtherModule(), ...);

// OR, to automatically load modules:

var kernel = new StandardKernel();
kernel.AutoLoadModules("~/bin");
return kernel;   } }

Once you do this, your controllers will be activated via Ninject, meaning you can expose dependencies on their constructors (or properties, or methods) to request injections.

A: 

As you didnt rule it out in your question, I have to assume you're not aware of the WebForms equivalent of the one you cited

(Linked from the Ninject extensions index on the home site)

Ruben Bartelink
@Ruben, partly right. I am aware of the extension ninject.web, but not how to implement it on say on existing asp.net web application.
No Body
@No Body: http://davidhayden.com/blog/dave/archive/2008/06/20/ninjectdependencyinjectionaspnetwebpagessample.aspx covers it pretty well - there's a base class for Global and for pages. Is there anything not covered by that? If there is, ask.
Ruben Bartelink
@Ruben, fair enough but lastly, hayden uses Ninject.Framework.Web is this the same as the new ninject.web?
No Body
It's the v1 edition (esp going on the date). In V2, the extensibility mechanism has been cleaned up/tidied/generalised a bit when compared to v1 - the new naming is part of this. The way stuff works re integration hasnt fundamentally changed [and obv ASP.NET hasnt changed much]. (@Ian Davis terms his work a port of Nate's one, which reflects this. If he'd fundamentally changed it, I have confidence he'd have doc'd it either in the readme.markdown or in comments in the code). Have you downloaded it and looked at the source? It's good code and will teach you far more than to/froing on forums :P
Ruben Bartelink
@Ruben thanks, haven't dig so far with the source code, I was hoping to see some sample code/practical application around but, could see one, seems most of the people are into asp.net mvc rather than the asp.net web.
No Body
A: 

Just want share how did I solved it using Visual Studio 2008

For those of you guys been to www.tekpub.com code below is kinda familiar, Yes! your correct code below is from Mastering ASP.NET MVC 2.0 series, and a demonstration of how to use NLog

Needed Reference:

  • Ninject.dll
  • Ninject.Web
  • NLog.dll

Global.asax :

<%@ Application Language="C#" Inherits ="Ninject.Web.NinjectHttpApplication"  %>
<%@ Import Namespace="App_Code.Infrastructure.Logging"%>
<%@ Import Namespace="Ninject.Modules"%>
<%@ Import Namespace="Ninject"%>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }


    protected override void OnApplicationStarted()
    {
        //base.OnApplicationStarted();

        Container.Get<ILogger>().Info("Application Started");
    }

    protected override IKernel CreateKernel()
    {
        return Container;
    }

    static IKernel Container
    {
        get
        {
            return new StandardKernel(new SiteModule());
        }

    }

    class SiteModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ILogger>().To<NLogger>().InSingletonScope();
        }
    }

</script>
No Body