Ninject Dependency Injection into ASP.NET Web Pages Sample

I just created a screencast where I discussed injecting dependencies into web pages using Unity IoC:

Given the release of Ninject 1.0 - Lightning Fast Dependency Injection for .NET, I thought I would put together a sample of injecting dependencies into ASP.Net Web Pages using Ninject.

Just like with Unity, there are several ways to inject dependencies into a web page using Ninject. In this case we will leverage the Ninject.Framework.Web Assembly which includes NinjectHttpApplication, PageBase, MasterPageBase, etc. for integrating Ninject into an ASP.NET Web Application. You will see a lot of similarities between this example and the example I showed in the screencast.

You will need to reference two assemblies in your web application:

  • Ninject.Core
  • Ninject.Framework.Web

 

The Page

In this very simple example, we want to inject an ICustomerDAO Service into our default page. I am using the InjectAttribute, [Inject], to tell Ninject that I want it to inject the ICustomerDAO Service into the CustomerDAO Property.

 

public partial class Default : PageBase
{
    [Inject]
    public ICustomerDAO CustomerDAO { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Customer customer = CustomerDAO.FetchCustomer();
    }
}

 

The Page derives from the PageBase Abstract Class in Ninject.Framework.Web, which calls KernelContainer.Inject(this) in the OnInit Event to inject dependencies into the page.

 

Our Global Application Class

Above is what we want to happen, but we have a little bit of work to do to get our container created and our type bindings set-up so Ninject knows what to supply for ICustomerDAO.

First things first, let's derive our Global Aplication Class from NinjectHttpApplication and implement the CreateKernel Method to create our container.

 

public class Global : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        IKernel kernel =
            new StandardKernel(new InfrastructureModule());
        return kernel;
    }
}

 

You can see the code is very sparse. The StandardKernel accepts Modules in its constructor and it is in the modules where we define the type mappings. Hence the InfrastructureModule is where we specify the type bindings for ICustomerDAO.

 

InfrastructureModule

The InfrastructureModule binds a CustomerDAO Class to the ICustomerDAO interface and specifies the SingletonBehavior.

 

public class InfrastructureModule : StandardModule
{
    public override void Load()
    {
        Bind<ICustomerDAO>().To<CustomerDAO>()
            .Using<SingletonBehavior>();
    }
}

 

Conclusion

And that's it! It is very easy to get up and running with dependency injection in your ASP.NET Web Pages using Ninject. Again, this example is similar to how I did it with Unity in the screencast mentioned above.

Hope this helps.

 

posted on Friday, June 20, 2008 1:09 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices