Using Ninject in a Web Application

http://aidenweb.co.uk/?p=15 

Using Ninject in a Web Application

I have been meaning to look at Ninject for a while now, and today I finally got my chance.
I am only using some basic features of Ninject to replace my normal use of Constructor Injection that I tend to favour.

This first example is based on the code needed to drive my jQuery examples. When I reached the point of requiring server side data, I decided that I wanted to try out Ninject.

I wanted to load some data using an HttpHandler. I used a handler as I wanted to also save data using the same URL.
The handler has what might be a PageController, which will determine the action to take based on the request being an HTTP GET or POST.
The controller itself relies on an implementation of IDataLayer which handles the data access.

Currently I have only explored the basic binding techniques in Ninject. This involves creating a class that specifies the bindings, then registering it with the Kernel.

    using System;
using Ninject.Core;

namespace Core.NinjectModules{
public class PeopleModule : StandardModule{
public override void Load(){
Bind().To();
Bind().ToSelf();
}
}
}

This is the binding module for specifying the dependancies of my People class.

    using System;
using System.Text;
using Ninject.Core;

namespace Core{
public class People{
IDataLayer _dataAccess;

[Inject]
public People(IDataLayer dataAccess){
_dataAccess = dataAccess;
}
...
}
}

This is the People class and with the single attribute needed to identify to ninject what to use for injection.

It is using the constructor injection feature. I have removed the rest of the implementation of the class to help with reading.

Ninject offers a set of features specifically for web applications. They can be found in the Ninject.Frameworks.Web namespace.

From this namespace I used the NinjectHttpApplication and HttpHandlerBase classes.

The NinjectHttpApplication provides the implementation required to attach a ninject Kernel to your HttpApplication.

It is an abstract class that requires the user to implement a CreateKernel() method, in which the user initialises the Kernel.

    using System;
using Ninject.Framework.Web;
using Ninject.Core;

namespace jQueryExamples
{
public class Global : NinjectHttpApplication{
protected override Ninject.Core.IKernel CreateKernel(){
IKernel kernel = new StandardKernel(new Core.NinjectModules.People());
return kernel;
}
}
}

This is all that I needed to register my People module with the ninject Kernel.

I then needed the People controller to be injected into my handler. This is where the HttpHandlerBase comes into it.


using System;
using System.Web;
using System.Web.Services;
using Core;
using Ninject.Core;

namespace jQueryExamples.handlers{

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class people : Ninject.Framework.Web.HttpHandlerBase{
private People _people;

[Inject]
public People PeopleController{
get { return _people; }
set { _people = value; }
}

protected override void DoProcessRequest(HttpContext context){
_people.RequestType = context.Request.RequestType;
string responseText = _people.Process();
context.Response.Clear();
context.Response.Write(responseText);
context.Response.ContentType = "text/xml";
context.Response.StatusCode = 200;
}

public override bool IsReusable{
get { return false; }
}
}
}

This is the entire implimentation of my handler. The People class will be injected into the handler when it is needed.

That is it for now for ninject. I can do the basics, next I need to learn about the context specific bindings etc

原文地址:https://www.cnblogs.com/Leo_wl/p/3630888.html