简单的三层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优化.导航.分页.购物车.订单.产品管 ...
随机推荐
- Class loader:static
package classloader; public class ClassLoaderDisplayDemo { public static void main(String[] args) { ...
- AngularJS与ASP.NET MVC登录超时解决方案
问题: 1.在Action中判断Ajax请求的方法Request.IsAjaxRequest()始终是false 2.返回给前台StatusCode和HttpUnauthorizedResult,前台 ...
- hdu 3068 最长回文(manachar模板)
Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输 ...
- OS和android游戏纹理优化和内存优化(cocos2d-x)
注:原文地址不详! 1.2d游戏最占内存的无疑是图片资源. 2.cocos2d-x不同平台读取纹理的机制不同. ios以下使用CGImage,android和windows下是直接调用png库.我測试 ...
- C#操作Xml:使用XmlWriter写Xml
假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml 1.如何使用XmlWriter写Xml文档声明 ? // WriteStartDocument方法可以接受一个 ...
- Java对多线程~~~Fork/Join同步和异步帧
于Fork/Join骨架,当提交的任务,有两个同步和异步模式.它已被用于invokeAll()该方法是同步的.是任何 务提交后,这种方法不会返回直到全部的任务都处理完了.而还有还有一种方式,就是使用f ...
- Swift-开发 # 1.2版本迁移
{ Parallels: 可以将一个win程序拖到mac中运行. } --类似于虚拟机 遇到的几大问题: 1.自动修改无效? --忽略它的存在,坑. 2.无止境的修改,还是错? --使用替换工具-&g ...
- [CLR via C#]6. 类型和成员基础
原文:[CLR via C#]6. 类型和成员基础 6.1 类型的各种成员 在一个类型中,可以定义0个或多个以下种类的成员: 1)常量 常量就是指出数据值恒定不变的符号.这些符号通常用于使代码更 ...
- 找不到方法: Int32 System.Environment.get_CurrentManagedThreadId() .
这个问题在本地运行没错...放到服务器上就出现这个问题.. 原因:是这个方法是.NETFRAMWORK4.5的..服务器上用的是4.0就会出现这个问题. 解决办法:在本地WEB项目右键把项目改到FRA ...
- java中HashSet详解
HashSet 的实现 对于 HashSet 而言,它是基于 HashMap 实现的,HashSet 底层采用 HashMap 来保存所有元素,因此 HashSet 的实现比较简单,查看 HashSe ...