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

Using Ninject in a Web Application的更多相关文章

  1. Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques

    Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques Jan 04, 2017, Vers ...

  2. 微软压力测试工具 web application stress

    转自 http://www.cnblogs.com/tonykan/p/3514749.html lbimba  铜牌会员 这里给广大的煤油推荐一个web网站压力测试工具.它可以用来模拟多个用户操作网 ...

  3. 使用Microsoft Web Application Stress Tool对web进行压力测试

    Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试一些Web服务器的运行状态和响应时间等等,对于Web服务器的承受力测试是个非常好的手法.Web 压力测试通常是利用一些工具,例如微软 ...

  4. 实验环境里新创建成功的web application却在浏览器中返回404错误

    刚刚翻笔记翻到一些刚学SharePoint时候解决的一些很2的初级问题,本来是有些挣扎该不该把它们记录到这个blog里的?因为担心这些很初级的文章会拉低这个blog的逼格,但是我的哥们善意的提醒了我一 ...

  5. SharePoint创建web application出现“The password supplied with the username was not correct”错误的解决方法

    平台环境 Windows Server 2012 R2 Standard, SharePoint Server 2010, Microsoft SQL Server 2012 (SP1) 问题描述 在 ...

  6. SharePoint中新创建的Web Application在浏览器中报404错误

    问题描述:在安装完成SharePoint 2010后,进入Central Administration,创建一个新的Web Application,可以正常创建,但访问时却返回404. 平台环境:Wi ...

  7. Intellij Idea中的Jetty报出Web application not found src/main/webapp错误的解决方案

    今天在Intellij Idea中编译项目的时候,运行起来一直会报出如下的错误: Web application not found src/main/webapp 当时感觉应该是什么文件缺少了.所以 ...

  8. 如何Recycle一个SharePoint Web Application,以及为什么

    当你发现SharePoint服务器的CPU或者内存使用率居高不下的时候,很多人都会选择iisreset来让资源使用率降下来.但是在企业环境中,这毫无疑问会使这台服务器中断服务从而影响到用户的使用,所以 ...

  9. You may receive an exception when you browse a .NET Framework 2.0 ASP.NET Web application

    SYMPTOMS When you browse a Microsoft .NET Framework 2.0 ASP.NET Web application, you may receive one ...

随机推荐

  1. ASP.NET MVC5 插件机制中插件的简单实现

    Autofac 依赖注入 ASP.NET MVC5 插件机制中插件的简单实现 一.前言 由于项目业务复杂,创建了多个插件并把他们放在了不同的项目中,项目使用AutoFac做的IOC:但是主项目可以注入 ...

  2. SRM 587 Div II L3:ThreeColorabilityEasyy

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12699 这道题目是第一次在比赛的时候做出来的,开始还想用bru ...

  3. 2136 Largest prime factor(打表)

    Problem Description Everybody knows any number can be combined by the prime number.Now, your task is ...

  4. Select与SelectMany的区别

    Select() 和 SelectMany() 的工作都是依据源值生成一个或多个结果值. Select() 为每个源值生成一个结果值.因此,总体结果是一个与源集合具有相同元素数目的集合.与之相反,Se ...

  5. 导入三方包,出现ClassNotFoundException

    在项目中须要引用settings模块里面的某个活动.在eclipse中导入settins.jar包之后,使用例如以下方式启动: Intent intent = new Intent(); intent ...

  6. 算法题C#

    几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...

  7. GDI+学问------ 绘制可变角度的色彩渐变效果

    GDI+ 它是GDI(Windows 图形设备接口提供的早期版本)也许是版本号,它是Microsoft Windows XP作系统即兴许版本号的图形显示技术. 它已经集成到了.Net开发环境中.所以无 ...

  8. 【百度地图API】如何制作商圈地图?行政地图?

    原文:[百度地图API]如何制作商圈地图?行政地图? 摘要: 想要显示某一个区域,并且鼠标放上去,该区域就会变色.这时,你就需要巧用多边形覆盖物,和它的鼠标事件了! 快来看看去哪儿网的实例吧:http ...

  9. utf8 和 UTF-8 在使用中的差别

    在使用中经常遇到utf-8和utf8,如今最终弄明确他们的使用不同之处了,如今来和大家分享一下,以下我们看一下utf8 和 UTF-8 有什么差别  "UTF-8"是标准写法,ph ...

  10. 完整具体解释GCD系列(二)dispatch_after;dispatch_apply;dispatch_once

    原创Blog,转载请注明出处 本文阅读的过程中,如有概念不懂,请參照前专栏中之前的文章,假设还有疑惑,请留言. 这是我关于GCD专栏的地址 http://blog.csdn.net/column/de ...