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 ...
随机推荐
- Android重力加速度传感器数据去噪
public void onSensorChanged(SensorEvent event) { final float alpha = 0.8; gravity[0] = alpha * gravi ...
- Angularjs,WebAPI 搭建一个简易权限管理系统
Angularjs,WebAPI 搭建一个简易权限管理系统 Angularjs名词与概念(一) 1. 目录 前言 Angularjs名词与概念 权限系统原型 权限系统业务 数据库设计和实现 Web ...
- Nyoj 引水工程(最小生成树)
描述 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工程,事关中华民族长远发展.“南水北调工程”,旨在缓解中国华北和西北地区水资源短缺的国家 ...
- [mysql]刷新windows恢复后mysql和"Access denied for user'root'@'IP'"处理问题
mysql数据库软件实际上是绿色的,重装系统后能够继续使用. 1.重装系统保留原有的后mysql安装文件夹,数据文件夹. 2.制作用于启动一个批处理文件mysql:[run.bat]的文件存储在mys ...
- 【百度地图API】如何根据摩卡托坐标进行POI查询,和计算两点距离
原文:[百度地图API]如何根据摩卡托坐标进行POI查询,和计算两点距离 摘要: 百度地图API有两种坐标系,一种是百度经纬度,一种是摩卡托坐标系.在本章你将学会: 1.如何相互转换这两种坐标: 2. ...
- http层负载均衡之haproxy
http层负载均衡之haproxy实践篇(一) 方案 上篇文章讲到了负载均衡的相关理论知识,这篇文章我打算讲讲实践方法以及实践中遇到的问题 方案:haproxy http层负载均衡 安装一个hapro ...
- MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦
原文:MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦 文本编辑器有很多,比如ticymce和CKEditor就比较好用,但涉及到图片.文件上传,需要结合CKFinde ...
- uploadfiy 动态传递Form 参数
参见 百度 http://jingyan.baidu.com/article/a3a3f8118b1c4d8da3eb8a60.html @{ ViewBag.Title = "Ind ...
- 通俗易懂地解决中文乱码问题(2) --- 分析解决Mysql插入移动端表情符报错 ‘incorrect string value: '\xF0...
原文:[原创]通俗易懂地解决中文乱码问题(2) --- 分析解决Mysql插入移动端表情符报错 'incorrect string value: '\xF0... 这篇blog重点在解决问题,如果你对 ...
- Android adb端口转发调试助手Packet Sender
相信大家做过安卓开发或者安卓自动化测试开发的都离不开adb这个Android Debug Bridge这个工具,该工具有个很重要的功能就是端口转发.比如你在目标安卓机器端建立了一个服务来处理获取当前界 ...