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 ...
随机推荐
- cxSpreadBook 要么 cxSpreadSheet 设置文本格式
uses cxSSStyles,cxSSDesigner; Type TStyleAccess = class(TcxSSCellStyle); TSheetAccess = class(TcxS ...
- Ubuntu中编译链接Opencv应用的简便方式
安装完毕Opencv后,使用下面命令查 看编译/连接參数 pkg-config --cflags --libs opencv 可看到例如以下信息 -I/usr/include/opencv /usr ...
- Android 2.3.5源码 更新至android 4.4,能够下载,度娘网盘
Android 4.4源代码下载(linux合并) ==============================切割线结束========================= 旧版本号的能够使用115, ...
- DevExpress中获取RichTextEdit中RichEditControl的两种方式
方式一: var rte = sender as RichTextEdit; control = rte.Controls[] as RichEditControl; 方式二: PropertyInf ...
- lambda 表达式 自定义查询
遇到 这样的 问题 常用 EF . 实现 like 用 Contains("asd") 搞定 他生成的是 %asd% . 如果 我希望 生成 asd%,怎么搞呢? Start ...
- ArcGIS Runtime SDKs v10.2.4最新(Android、iOS、OSX和.NET)
ArcGIS Runtime SDKs v10.2.4最新,它包含:Android.iOS.OS X和.NET四大平台,用户和开发人员可以登录Esri最新的SDK安装包.或者通过云盘下载(http:/ ...
- Entity Framework查询原理
Entity Framework查询原理 前言 Entity Framework的全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Rela ...
- 关于MariaDB和Mysql
最近安装数据库的时候发现执行yum install mysql 命令时安装成了mariadb的数据库,它其实也是mysql的一个分支,完全兼容mysql 安装数据库: yum install mysq ...
- 分布式基础学习(2)分布式计算系统(Map/Reduce)
二. 分布式计算(Map/Reduce) 分 布式式计算,同样是一个宽泛的概念,在这里,它狭义的指代,按Google Map/Reduce框架所设计的分布式框架.在Hadoop中,分布式文件 系统,很 ...
- 条形码(JBarcode)
一世尘梦 少小离家老大回,妖娆尘世,程序唧唧:问君能有几多愁,恰是满屏BUG无处修. 商品条形码(JBarcode) 之前没有使用过这个,现在使用JBarcode生成商品条形码,工作之前的准备工作: ...