Using Ninject in a Web Application
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的更多相关文章
- Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques
Web Application Penetration Testing Local File Inclusion (LFI) Testing Techniques Jan 04, 2017, Vers ...
- 微软压力测试工具 web application stress
转自 http://www.cnblogs.com/tonykan/p/3514749.html lbimba 铜牌会员 这里给广大的煤油推荐一个web网站压力测试工具.它可以用来模拟多个用户操作网 ...
- 使用Microsoft Web Application Stress Tool对web进行压力测试
Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试一些Web服务器的运行状态和响应时间等等,对于Web服务器的承受力测试是个非常好的手法.Web 压力测试通常是利用一些工具,例如微软 ...
- 实验环境里新创建成功的web application却在浏览器中返回404错误
刚刚翻笔记翻到一些刚学SharePoint时候解决的一些很2的初级问题,本来是有些挣扎该不该把它们记录到这个blog里的?因为担心这些很初级的文章会拉低这个blog的逼格,但是我的哥们善意的提醒了我一 ...
- 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) 问题描述 在 ...
- SharePoint中新创建的Web Application在浏览器中报404错误
问题描述:在安装完成SharePoint 2010后,进入Central Administration,创建一个新的Web Application,可以正常创建,但访问时却返回404. 平台环境:Wi ...
- Intellij Idea中的Jetty报出Web application not found src/main/webapp错误的解决方案
今天在Intellij Idea中编译项目的时候,运行起来一直会报出如下的错误: Web application not found src/main/webapp 当时感觉应该是什么文件缺少了.所以 ...
- 如何Recycle一个SharePoint Web Application,以及为什么
当你发现SharePoint服务器的CPU或者内存使用率居高不下的时候,很多人都会选择iisreset来让资源使用率降下来.但是在企业环境中,这毫无疑问会使这台服务器中断服务从而影响到用户的使用,所以 ...
- 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 ...
随机推荐
- 如何将linux用在开发环境中的
如何将linux用在开发环境中的 1.我为什么要写这篇文章 一直想深入学习一下linux的使用,于是将家里的笔记本装了linux系统,但是要将自己的系统打造一个适合开发的环境确实是一件费心费力的事,而 ...
- OCP读书笔记(26) - 题库(ExamF)
501.Note the output of the following query;SQL> SELECT flashback_archieve_name, status FROM dba_f ...
- css+html简单的布局demo
于html介绍css作风.可以改变html块状布局,局更加美观.接下来看一个基础布局的小样例: <html> <head> <meta http-equiv=" ...
- 中英文url解码vc++源程序
本文主要讨论中文url解码实现问题,没有具体解说url编码,utf-8编码.想对编解码问题有更加具体的了解,请查阅相关文档 url编码:实质字符ascii码的十六进制.仅仅是略微有些变动,须要在前面加 ...
- java web.xml listener servlet 和filter加载顺序
在该项目中总会遇到一些关于加载的优先问题.最近遇到了同样的类别似的,所以,如果你发现信息汇总下,以下是转载其他一些人,毕竟,人们写的不错.它不重复创建的轮.只是略作修改自己的观点. 首先能够肯定的是, ...
- 深入理解C指针之三:指针和函数
原文:深入理解C指针之三:指针和函数 理解函数和指针的结合使用,需要理解程序栈.大部分现代的块结构语言,比如C,都用到了程序栈来支持函数的运行.调用函数时,会创建函数的栈帧并将其推到程序栈上.函数返回 ...
- IOS开发中绘制地图线路
地图应用经常会涉及到线路的绘制问题,ios下可以使用MKMapView进行地图开发,使用 MKOverlayView进行线路的绘制. 使用MKMapView添加MKMap.framework 和Cor ...
- JS全选功能代码优化
原文:JS全选功能代码优化 JS全选功能代码优化 最近在看javascript MVC那本书,也感觉到自己写的代码也并不优雅,所以一直在想 用另一种模式来编写JS代码,所以针对之前的简单的JS全选功能 ...
- Bash shell 简单的并行任务,并等待
首先启动两个command line对于实验 第一 command line 依次输入: bash$ sleep 10001 & [1] 38272 bash$ job1=$! bash$ s ...
- java中string和int互相转化
1 怎样将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...