简单的三层asp.net webForm使用Ninject实现Ioc

在asp.net webform下使用Ninject的简单过程。

首先建立个项目,如下图,简单三层(PS:UI层要同时引用BLL、Model、DAL这三层)

写好代码

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class UserInfo
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public DateTime LastLoginDate { get; set; }
public int Integral { get; set; } }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class Log
{
public int Id { get; set; }
public int ActionUserId { get; set; }
public string Desription { get; set; }
public DateTime CreateOn { get; set; }
}
}

DAL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
namespace DAL
{
public interface IUser
{
Model.UserInfo Create(Model.UserInfo user);
void Update(Model.UserInfo user);
Model.UserInfo GetModel(string userName);
Model.UserInfo GetModel(int userId);
void DeleteUser(int uid);
}
public class User : IUser
{
public Model.UserInfo Create(Model.UserInfo user)
{
//do some SQL
return new UserInfo() { UserID=1, UserName="myName", Password="Passowrd", LastLoginDate=DateTime.Now, Integral=0 };
}
public void Update(Model.UserInfo user)
{
//do some SQL
}
public Model.UserInfo GetModel(string userName)
{
//do some SQL
return new UserInfo() { UserID = 1, UserName = "admin", Password = "passowrd", LastLoginDate = DateTime.Now, Integral = 0 };
}
public Model.UserInfo GetModel(int uid)
{
//do some SQL
return new UserInfo() { UserID = uid, UserName = "myName", Password = "Passowrd", LastLoginDate = DateTime.Now, Integral = 0 };
}
public void DeleteUser(int uid)
{
//do some SQL
}
}
}

最后是BLL的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;
namespace BLL
{
public interface IUser
{
bool Validate(string name, string pwd);
bool Login(string name, string pwd);
}
public class User : IUser
{
[Inject]
public DAL.IUser UserDAL { get; set; }
public bool Validate(string name, string pwd)
{
var isValidate = false;
var model = UserDAL.GetModel(name);
if (model != null)
{
pwd = this.GetEncryptPassword(pwd);
if (model.Password == pwd)
isValidate = true;
}
return isValidate;
}
public bool Login(string name, string pwd)
{
var isValidate = this.Validate(name,pwd);
if (isValidate)
{
var model = UserDAL.GetModel(name);
//更新登录时间和积分
model.LastLoginDate = DateTime.Now;
model.Integral += 5;
UserDAL.Update(model);
//记录获得积分事件
BLL.Log.WriteLog(new Model.Log() { ActionUserId = model.UserID, Desription = "每天登录获得5积分", CreateOn = DateTime.Now });
}
return isValidate;
}
private string GetEncryptPassword(string pwd)
{
//返回加密后的密文
return pwd;
}
}
}

现在,先使用nuGet(如何使用nuGet)在UI层引用组件Ninject和Ninjecet.Web.Common,如下图,绿色的钩子表示已经安装

引用成功后,不用你设置任何配置,然后在自动添加的文件中,添加接口注入:

在文件NinjectWebCommon.cs的方法添加我们进行接口注入:

private static void RegisterServices(IKernel kernel)
{
kernel.Bind<BLL.IUser>().To<BLL.User>();
kernel.Bind<DAL.IUser>().To<DAL.User>();
}

为了在BLL中进行对DAL接口进行注入,也要在BLL中引用Ninect

方法:右击BLL,选择添加引用,引用的路径 是:

AspNETWebFormNinject\packages\Ninject.3.0.1.10\lib\net40\Ninject.dll

这样就可以在BLL中使用Ninject注入了:

最后们在UI层使用登录:

namespace AspNETWebFormNinject
{
public partial class _Default : System.Web.UI.Page
{
[Inject]
public BLL.IUser UserBLl { get; set; }
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnLogin_Click(object sender, EventArgs e)
{
var isValidate = UserBLl.Login(txtUserName.Text, txtPwd.Text);
var msg = isValidate ? "登录成功" : "登录失败";
var script = string.Format("alert('{0}')",msg);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", script, true);
}
}
}

整个DEMO  下载

简单的三层asp.net webForm使用Ninject实现Ioc的更多相关文章

  1. 简单原始的ASP.NET WEBFORM中多文件上传【参考其他资料修改】

    首先是ASPX页面中添加file标签 <input onclick="addFile()" type="button" value="增加&qu ...

  2. 解析ASP.NET WebForm和Mvc开发的区别

    因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...

  3. 解析ASP.NET WebForm和Mvc开发的区别 分类: ASP.NET 2013-12-29 01:59 11738人阅读 评论(5) 收藏

    因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...

  4. ASP.NET MVC与ASP.NET WebForm

    ASP.NET MVC是微软公司的一款WEB开发框架,整合了“模型-视图-控制器”架构的高效与整洁,是敏捷开发最现代的思想与技术.它是传统ASP.NET WebForm的一个完善的替代品. 1.当今的 ...

  5. ASP.Net WebForm温故知新学习笔记:一、aspx与服务器控件探秘

    开篇:毫无疑问,ASP.Net WebForm是微软推出的一个跨时代的Web开发模式,它将WinForm开发模式的快捷便利的优点移植到了Web开发上,我们只要学会三步:拖控件→设属性→绑事件,便可以行 ...

  6. 一、ASP.NET MVC 路由(一)--- ASP.NET WebForm路由模拟

    ASP.NET WebForm 应用,用户请求的是物理文件,其中包括静态页面和动态页面,在Url中的显示都是服务器中一个物理文件的相对路径.但是ASP.NET MVC就不同了,用户请求的是Contro ...

  7. (转)教你记住ASP.NET WebForm页面的生命周期

    对于ASP.NET Webform的开发者,理解ASP.NET Webform的页面生命周期是非常重要的.主要是为了搞明白在哪里放置特定的方法和在何时设置各种页面属性.但是记忆和理解页面生命周期里提供 ...

  8. 【Ext.Net学习笔记】01:在ASP.NET WebForm中使用Ext.Net

    Ext.NET是基于跨浏览器的ExtJS库和.NET Framework的一套支持ASP.NET AJAX的开源Web控件,包含有丰富的Ajax运用,其前身是Coolite. 下载地址:http:// ...

  9. MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

随机推荐

  1. hdu 1533 Going Home 最小费用流

    构建地图非常easy bfs预处理地图.距离的成本 来源所有m建方,流程1费0 m所有H建方,流程1距离成本 H汇点建设成为各方.流程1费0 #include<cstdio> #inclu ...

  2. java回调机制及其实现(转)

    1. 什么是回调函数 回调函数,顾名思义,用于回调的函数.回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机.回调 ...

  3. Navicat工具Oracle数据库复制 or 备用、恢复功能(评论都在谈论需要教)

    GXPT它是一个分布式系统,该系统包括一个临时许可系统.基本系统.教学评价体系.考试系统,每个系统都有自己的oracle数据库.统,而评教系统的正常须要借助于权限系统和基础系统,详细的业务这里就不多解 ...

  4. Tyvj P1016 包装问题 (DP)

    底 Background 太原诚成中学2模拟法庭竞赛 第三条道路 描写叙述 Description 有一个箱子容量为v(正整数.o≤v≤20000).同一时候有n个物品(o≤n≤30).每一个物品有一 ...

  5. Cocos发育Visual Studio下一个HttpClient开发环境设置

    Cocos2d-x 3.x相关类集成到网络通信libNetwork图书馆project于.这其中包括:HttpClient分类. 我们需要在Visual Studio溶液中加入libNetwork图书 ...

  6. 64bit Centos6.4编hadoop-2.5.1

    64bit Centos6.4编hadoop-2.5.1   1.说明 a)       因为从apache下载下来的tar.gz包是用32 bit编译的,全部假设用Linux 64作为hadoop的 ...

  7. Sql数据类型转换

     一.ASCII码值与字符间转换 1.ASCII()与CHAR()       ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用''括起来,但含其 ...

  8. 华为-on练习--身高找到最好的二人

    称号: 离5个人选择2个人作为礼工具.中的每个个体的身高的范围160-190,要求2个人高差值至少(假设差异值一样,他们中最高的选择).输出的两个人的身高升序. Smple input:161 189 ...

  9. 百度地图API详解之事件机制,function“闭包”解决for循环和监听器冲突的问题:

    原文:百度地图API详解之事件机制,function"闭包"解决for循环和监听器冲突的问题: 百度地图API详解之事件机制 2011年07月26日 星期二 下午 04:06 和D ...

  10. request的setAttribute()怎么用的?

    request.setAttribute()怎么用的?JSP1代码String [] test=new String[2];test[0]="1";test[1]="2& ...