简单的三层asp.net webForm使用Ninject实现Ioc
简单的三层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);
}
}
}
简单的三层asp.net webForm使用Ninject实现Ioc的更多相关文章
- 简单原始的ASP.NET WEBFORM中多文件上传【参考其他资料修改】
首先是ASPX页面中添加file标签 <input onclick="addFile()" type="button" value="增加&qu ...
- 解析ASP.NET WebForm和Mvc开发的区别
因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...
- 解析ASP.NET WebForm和Mvc开发的区别 分类: ASP.NET 2013-12-29 01:59 11738人阅读 评论(5) 收藏
因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...
- ASP.NET MVC与ASP.NET WebForm
ASP.NET MVC是微软公司的一款WEB开发框架,整合了“模型-视图-控制器”架构的高效与整洁,是敏捷开发最现代的思想与技术.它是传统ASP.NET WebForm的一个完善的替代品. 1.当今的 ...
- ASP.Net WebForm温故知新学习笔记:一、aspx与服务器控件探秘
开篇:毫无疑问,ASP.Net WebForm是微软推出的一个跨时代的Web开发模式,它将WinForm开发模式的快捷便利的优点移植到了Web开发上,我们只要学会三步:拖控件→设属性→绑事件,便可以行 ...
- 一、ASP.NET MVC 路由(一)--- ASP.NET WebForm路由模拟
ASP.NET WebForm 应用,用户请求的是物理文件,其中包括静态页面和动态页面,在Url中的显示都是服务器中一个物理文件的相对路径.但是ASP.NET MVC就不同了,用户请求的是Contro ...
- (转)教你记住ASP.NET WebForm页面的生命周期
对于ASP.NET Webform的开发者,理解ASP.NET Webform的页面生命周期是非常重要的.主要是为了搞明白在哪里放置特定的方法和在何时设置各种页面属性.但是记忆和理解页面生命周期里提供 ...
- 【Ext.Net学习笔记】01:在ASP.NET WebForm中使用Ext.Net
Ext.NET是基于跨浏览器的ExtJS库和.NET Framework的一套支持ASP.NET AJAX的开源Web控件,包含有丰富的Ajax运用,其前身是Coolite. 下载地址:http:// ...
- MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
随机推荐
- git stash用法
使用场景: 当前修改的代码还不足以提交commit,但又必须切换到其他分支,要想完成这样的操作就可以使用git stash git stash意思就是备份当前的工作区的内容,从最近的一次提交中读取相关 ...
- c#有关udp可靠传输(包传输数据包) 升级
在c#有关udp可靠传输(包传输数据包)我们讨论,UDP包的发送,可是上一个程序有一个问题.就是数据比較大.一个Message类序列化后都有2048B,而实际的数据量也就只是 50B罢了,这就说明当中 ...
- Linq的Distinct太不给力了[转]
假设我们有一个类:Product public class Product { public string Id { get; set; } public string Name { get; set ...
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- Java设计模式菜鸟系列(四)工厂方法模式建模与实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39760895 工厂方法模式(Factory Method) 工厂方法:顾名思义,就是调用工 ...
- ReactJS.NET 开发
初探ReactJS.NET 开发 ReactJS通常也被称为"React",是一个刚刚在这场游戏中登场的新手.它由Facebook创建,并在2013年首次发布.Facebook ...
- WPF - Visual调试工具Snoop
原文:WPF - Visual调试工具Snoop Snoop经过很长一段时间,最近更新到支持NET 3.5了,它是一个WPF运行时对Visual UI调试的一个工具,最近我用过它调试修改过一个bug, ...
- HBase文件格式演变之路
Apache HBase是Hadoop的分布式开源的存储管理工具,很适合随机实时的io操作. 我们知道,Hadoop的Sequence File是一个顺序读写,批量处理的系统.可是为什么HBase能做 ...
- 使用Visual Studio创建映像向导(Image Sprite)——Web Essential
原版的:Creating Image Sprite in Visual Studio - Web Essential 译者注:有关图片精灵的信息请參阅http://baike.baidu.com/vi ...
- Windows 10技术布局,谈微软王者归来
Windows 10技术布局,谈微软王者归来 每个时代都有王者,王者的成功,往往是因为恰逢其时地发布了一个成功的产品(具有里程碑意义,划时代的产品).Windows 95的成功标示着微软是PC时代的王 ...