代码改变世界

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

2012-08-24 15:34  taz01  阅读(2699)  评论(16编辑  收藏  举报

 

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

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

 

写好代码

Model:

View Code
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; }
        
    }

}
View Code
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:

View Code
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的代码:

View Code
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层使用登录:

View Code
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  下载