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. Weka初步

    从前年開始使用weka最数据挖掘方面的研究,到如今有一年半的时间了.看到我们同组的兄弟写了关于weka方面的总结.我也想整理一下.由于网上的资料实在是太少.记得刚接手的时候,真是硬着头皮看代码.只是到 ...

  2. 【C语言的日常实践(八)】弦

    串数据类型是一个重要的,但C有没有明确的语言字符串数据类型.头文件string.h它包括大多数字符串处理函数. 故,有操作的串.通常包括string.h头文件. 1.字符串的长度:对字符串进行的操作中 ...

  3. 使用 CodeIgniter 框架快速开发 PHP 应用(四)

    原文:使用 CodeIgniter 框架快速开发 PHP 应用(四) 使用 CI 简化数据库开发你学习CI 是因为你想要使编程更容易和更有生产力.这一章讲述CI的Active Record类. 如果C ...

  4. Android 使用DexClassLoader要执行其他apk方法

    Android在apk文件dex文件是java编译出来.class次打包,当然在打包之前会利用自己的协议做一些数据处理,比如优化函数表和变量表.在java程序中是使用classloader来载入这些编 ...

  5. 使用linux服务logrotate文件tomcat日志文件

    使用notepad++编辑本地文件 tomcat: /usr/tomcat/logs/catalina.out { copytruncate daily dateext nocompress miss ...

  6. HDU1051 Wooden Sticks 【贪婪】

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(6)- EF上下文实例管理

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(6)- EF上下文实例管理 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    ( ...

  8. VPS(Virtual Private Server 虚拟专用服务器)[转自百度]

    系统选择 vps上常用的操作系统是linux(有多种发行版).freeBSD,windows server等.一般来说,vps的操作系统不是自由安装的,linux系列vps可以安装多个linux发行版 ...

  9. HDU 1598 find the most comfortable road (最小生成树) >>

    Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

  10. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...