Ninject之旅之十四:Ninject在ASP.NET Web Form程序上的应用(附程序下载)
摘要
ASP.NET Web Forms没有像MVC那样的可扩展性,也不可能使它创建UI页面支持没有构造函数的的激活方式。这个Web Forms应用程序的的局限性阻止了它使用构造函数注入模式,但是仍能够使用其他的DI模式,例如初始化方法模式。
下面使用一个具体的ASP.NET Web Forms应用程序使用Ninject例子来说明怎样在ASP.NET Web Forms上使用Ninject。
1. 在解决方案Demo.Northwind内,创建MVC工程Demo.Northwind.MVC。
2. 添加引用。
使用NutGet Manager添加引用:

添加Demo.Northwind.Core工程的引用。
3. 修改根路径下的Web.config文件。
添加connectionStrings节。
<connectionStrings>
<add name="NorthwindContext" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=NORTHWND;Integrated Security=True" />
</connectionStrings>
4. 修改Ninject配置。
展开App_Start文件夹,发现自动添加了NinjectWeb.cs代码文件和NinjectWebCommon.cs代码文件。

NinjectWeb.cs文件不用修改,它是一个静态类,有一个静态函数Start。
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
}
这将NinjectHttpModule注入到Http请求流程中。
NinjectWebCommon.cs文件内容跟在ASP.NET MVC上使用Ninject的NinjectWebCommon.cs一样。
查看他的CreateKernel方法和RegisterServices方法:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
} /// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
}
修改RegisterServices方法,添加DI代码:
kernel.Bind(x => x.FromAssembliesMatching("Demo.Northwind.*")
.SelectAllClasses().EndingWith("Repository")
.BindAllInterfaces());
5. 修改Default.aspx,添加一个GridView。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Northwind.Webforms._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="row">
<asp:GridView ID="customersGridView" runat="server"></asp:GridView>
</div>
</asp:Content>
6. 修改Default.aspxa.cs代码。
using Demo.Northwind.Core.Interface;
using Demo.Northwind.Core.Model;
using System;
using System.Linq;
using System.Web.UI; namespace Demo.Northwind.Webforms
{
public partial class _Default : Page
{
private ICustomerRepository repository; [Ninject.Inject]
public void Setup(ICustomerRepository customerRepository)
{
if (customerRepository == null)
{
throw new ArgumentNullException("customerRepository");
}
this.repository = customerRepository;
} protected void Page_Load(object sender, EventArgs e)
{
var customers = repository.GetAll();
customersGridView.DataSource = customers.ToList<Customer>();
customersGridView.DataBind();
}
}
}
接口ICustomerRepository通过Setup方法的参数注入,而不是通过构造函数参数注入。这是因为ASP.NET Web Forms UI引擎不能够用默认构造函数初始化UI元素。这个Setup方法在页面对象创建的时候被调用,使得它的参数被解析和被注入。
运行程序,得到执行结果:

Ninject之旅之十四:Ninject在ASP.NET Web Form程序上的应用(附程序下载)的更多相关文章
- Ninject之旅之十:Ninject自定义提供者
摘要 提供者是特殊的工厂类,Ninject使用它来实例化解析类型.任何时候我们绑定一个服务类型到一个组件,我们都隐式地关联那个服务类型到一个可以实例化那个组件的提供者.这个隐藏的提供者被称为Stand ...
- Ninject之旅之十二:Ninject在Windows Form程序上的应用(附程序下载)
摘要: 下面的几篇文章介绍如何使用Ninject创建不同类型的应用系统.包括: Windows Form应用系统 ASP.NET MVC应用系统 ASP.NET Web Form应用系统 尽管对于不同 ...
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
JAVA之旅(十四)--静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制 JAVA之旅,一路有你,加油! 一.静态同步函数的锁是clas ...
- Ninject之旅之十三:Ninject在ASP.NET MVC程序上的应用(附程序下载)
摘要: 在Windows客户端程序(WPF和Windows Forms)中使用Ninject和在控制台应用程序中使用Ninject没什么不同.在这些应用程序里我们不需要某些配置用来安装Ninject, ...
- Ninject之旅之十一:Ninject动态工厂(附程序下载)
摘要 如果我们已经知道了一个类所有的依赖项,在我们只需要依赖项的一个实例的场景中,在类的构造函数中引入一系列的依赖项是容易的.但是有些情况,我们需要在一个类里创建依赖项的多个实例,这时候Ninject ...
- Ninject之旅之九:Ninject上下文绑定(附程序下载)
摘要 既然在插件模型里,每一个服务类型可以被映射到多个实现,绑定方法不用决定要返回哪个实现.因为kernel应该返回所有的实现.然而,上下文绑定是多个绑定场景,在这个场景里,kernel需要根据给定的 ...
- Sql Server之旅——第十四站 深入的探讨锁机制
上一篇我只是做了一个堆表让大家初步的认识到锁的痉挛状态,但是在现实世界上并没有这么简单的事情,起码我的表不会没有索引对吧,,,还 有就是我的表一定会有很多的连接过来,10:1的读写,很多码农可能都会遇 ...
- Android 自定义View-android学习之旅(十四)
自定义View的步骤 当andoid提供的系统组件不满足要求时候,完全可以集成View来派生自定义组件. 首定定义一个继承View的子类,然后重写他一个或几个方法. 重写的方法介绍 构造器:这是定制V ...
- Python学习之旅(十四)
Python基础知识(13):函数(Ⅳ) Python内置函数 1.abs:取绝对值 abs(-1) 1 2.all:把序列中的每一个元素拿出来做布尔运算,都为真则返回True,如果序列中有None. ...
随机推荐
- [疯狂Java]JDBC:事务管理、中间点、批量更新
1. 数据库事务的概念: 1) 事务的目的就是为了保证数据库中数据的完整性. 2) 设想一个银行转账的过程,假设分两步,第一步是A的账户-1000,第二步是B的账户+1000.这两个动 ...
- [RxJS] Conclusion: when to use Subjects
As a conclusion to this course about RxJS subjects, let's review when and why should you use them. F ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- [CSS] Dynamically Size Elements with Pure CSS
Learn how to size elements based on the dimensions of the viewport, even when the browser is resized ...
- Android 控件EditText的setOnEditorActionListener方法的理解
需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的回 ...
- 上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的,
上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的, Html部分 <input type="file& ...
- [Django] ModelViewSet from rest_framework and Router
To build rest api easily, we can use ModelViewSet from rest_framework. It provides GET, POST, DELETE ...
- 解决IE6下PNG透明的JS插件:DD_belatedPNG.js
DD_belatedPNG是一款解决IE6下PNG透明的JS插件,支持background-position和background-repeat属性,支持伪类.使用方法: <!--[if lte ...
- SDE 空间表操作
1. 创建空间表(包含st_geometry属性字段) CREATE TABLE sensitive_areas (area_id integer, name varchar(128), area_s ...
- Android图文具体解释属性动画
Android中的动画分为视图动画(View Animation).属性动画(Property Animation)以及Drawable动画.从Android 3.0(API Level 11)開始. ...