zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    DotNetty 实现 Modbus TCP 系列 (一) 报文类
    C# 获取文件详细备注信息 (如图片、视频实际创建时间)
    Java 下载 HLS (m3u8) 视频
    开源 DotNetty 实现的 Modbus TCP/IP 协议
    SQL Server 2008 开启远程连接
    在 Web 页面使用 VLC 插件播放 m3u8 视频流 (360 极速模式)
    在 Web 页面中使用离线地图
    使用 Travis CI 自动部署 Hexo 站点至 GitHub Pages
    BZOJ 3238: [Ahoi2013]差异((单调栈+后缀数组)/(后缀树))
    BZOJ 3998: [TJOI2015]弦论(后缀自动机)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3630888.html
Copyright © 2011-2022 走看看