MVC下NHibernate小试牛刀

这是一个信息管理项目,实际管理内容就不说了

框架脱胎于PetShop3.0
分为如下几层
Web
BLL
IDAL
Model
DALFactory
NHDAL

根据PetShop3.0的框架:Web层只依赖于BLL和Model,也就是说web层调用BLL层方法返回的Model层定义的数据;BLL层依赖于IDAL,Model,这一层实际是使用IDAL中的方法组合为业务,并处理IDAL层返回的Model;IDAL定义了所有底层方法,给DAL层留下接口;Model定义了对象实体,只有属性没有方法,基本上可以作为抽象类定义。DALFactory的作用是根据配置文件中的设置动态取出取出数据访问层(Data Access Layer)对象的实例,这样做就把实际使用的DAL跟BLL层分离,如果需要切换到其他DAL,只要修改此层就可以了。

本项目使用NH(NHibernate)作为DAl,所以命名为NHDAL。NHDAL依赖于Model,继承Model中属性的定义,并添加了NH需要用到的属性器。同时NHDAL实现IDAL中定义的方法。

下面是对登陆验证和储存日志的实例
先看看IDAL和Model中的定义吧

代码段一 IDAL接口定义

None.gif using  System;
None.gif
None.gif
namespace  CManager.IDAL.Log
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ILoginLog 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public interface ISignLog
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
void LogSignIn( string userid, string sessionid, string clientip, DateTime logindate );
InBlock.gif
InBlock.gif        
bool CheckPassword( string userid, string password);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

代码段二 Model对象定义

None.gif using  System;
None.gif
None.gif
namespace  CManager.Model.Log
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// LoginInfo 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SignLogInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int SignLogID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SignLogID;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string UserID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _UserID;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string SessionID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SessionID;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string ClientIP
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _ClientIP;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public DateTime SignInDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SignInDate;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public DateTime SignOutDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SignOutDate;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected int _SignLogID;
InBlock.gif        
protected string _UserID;
InBlock.gif        
protected string _SessionID;
InBlock.gif        
protected string _ClientIP;
InBlock.gif        
protected DateTime _SignInDate;
InBlock.gif        
protected DateTime _SignOutDate;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

然后是BLL中调用。因为BLL不依赖于任何DAL,所以通过中间层DALFactory创建DAL的实例,并通过IDAl定义的接口返回给BLL。

代码段三 BLL方法

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
None.gif
using  CManager.Model.Log;
None.gif
using  CManager.Model.Collections;
None.gif
using  CManager.DALFactory.Log;
None.gif
using  CManager.IDAL.Log;
None.gif
None.gif
None.gif
namespace  CManager.BLL.Log
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    public class Log
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
//未完成,应该返回Module.Log.Account
InBlock.gif
        public static bool Login ( string userid, string password, HttpContext context )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISignLog log 
= LogFactory.CreateSignLog();
InBlock.gif            
if (log.CheckPassword(userid,password))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                log.LogSignIn( userid ,context.Session.SessionID, context.Request.UserHostAddress, DateTime.Now );
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

代码段四 DALFactory方法

None.gif using  System;
None.gif
using  CManager.IDAL.Log;
None.gif
using  System.Reflection;
None.gif
None.gif
namespace  CManager.DALFactory.Log
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class LogFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static ISignLog CreateSignLog()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//获取web.config里DAL的设置
InBlock.gif
            string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
InBlock.gif            
//组合出类名
InBlock.gif
            string className = path + ".Log.SignLog";
InBlock.gif            
//运行时创建类实例,需要类实现实例化
InBlock.gif
            return (ISignLog) Assembly.Load(path).CreateInstance(className);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

代码段五 web.config

None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif    
<!--  这段大家都熟悉了  -->
None.gif  
< configSections >
None.gif      
< section  name ="nhibernate"  type ="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"   />
None.gif  
</ configSections >
None.gif  
< nhibernate >
None.gif    
< add  key ="hibernate.connection.provider"  value ="NHibernate.Connection.DriverConnectionProvider"   />
None.gif    
< add  key ="hibernate.connection.driver_class"  value ="NHibernate.Driver.SqlClientDriver"   />
None.gif    
< add  key ="hibernate.connection.connection_string"  value ="server=127.0.0.1;database=CManager;uid=sa;pwd=;"   />
None.gif    
< add  key ="hibernate.connection.isolation"  value ="ReadCommitted" />
None.gif    
< add  key ="hibernate.dialect"  value ="NHibernate.Dialect.MsSql2000Dialect"   />
None.gif  
</ nhibernate >
None.gif    
< appSettings >
None.gif        
<!--  默认显示每页显示记录数  -->
None.gif        
< add  key ="DefaultRecordCount"  value ="20"   />
None.gif        
<!--  *数据库层所在命名空间* 实际使用的DAL在这里设置    -->
None.gif        
< add  key ="WebDAL"  value ="CManager.NHDAL"   />
None.gif  
</ appSettings >
None.gif  
< system .web >
                <!-- 这段按下,不占页面了 -->None.gif   
None.gif 
</ system.web >
None.gif
None.gif
</ configuration >
None.gif

理论上Web层并不知道IDAL层定义的原子方法,只使用BLL层给出的方法。这就是所谓的业务层与表现层分开。Web层只接受用户发出的请求,然后处理参数交给BLL层,并对BLL层返回的结果进行判断返回给结果。

代码段六 Web层调用

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  CManager.BLL.Log;
None.gif
using  CManager.Model.Log;
None.gif
None.gif
namespace  BTTech.CManager.Web
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Login 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Login : PageBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.TextBox UserName;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox Password;
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlInputImage ImageButton1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ImageButton1_ServerClick(object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Log.Login( UserName.Text, Password.Text, this.Context))
InBlock.gif                Response.Redirect(
"",true);
InBlock.gif            
else
InBlock.gif                Alert(
"用户名或密码错误");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

最后是本文的主角NHDAL了,虽然最后出现,却是所有实际操作的最终执行者。

代码段七 实现IDAL

None.gif using  System;
None.gif
using  NHibernate.Cfg;
None.gif
using  NHibernate;
None.gif
None.gif
namespace  CManager.NHDAL.Log
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SignLog 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SignLog : NHObject,CManager.IDAL.Log.ISignLog
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public SignLog()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void LogSignIn(string userid, string sessionid, string clientip, DateTime logindate)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CreateSession();
InBlock.gif            session.Save(
new SignLogInfo(userid, sessionid, clientip, logindate));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool CheckPassword( string userid, string password)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{//未完成
InBlock.gif
            return true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

代码段八 继承Model

None.gif using  System;
None.gif
using  CManager.Model;
None.gif
None.gif
namespace  CManager.NHDAL.Log
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SignLog 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SignLogInfo : CManager.Model.Log.SignLogInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return this._SignLogID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set    dot.gif{this._SignLogID = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string NH_UserID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _UserID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_UserID = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string NH_SessionID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SessionID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_SessionID = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string NH_ClientIP
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _ClientIP;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_ClientIP = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private DateTime NH_SignInDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SignInDate;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_SignInDate = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private DateTime NH_SignOutDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return _SignOutDate;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{_SignOutDate = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private SignLogInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal SignLogInfo(string userid, string sessionid, string clientip, DateTime signdate )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._UserID = userid;
InBlock.gif            
this._SessionID = sessionid;
InBlock.gif            
this._ClientIP = clientip;
InBlock.gif            
this._SignInDate = signdate;
InBlock.gif            
this._SignOutDate = signdate;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

代码段九 影射文件  SignLogInfo.hbm.xml  这里用到上篇文章《不让NH属性器破坏封装》提到的方法

None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
None.gif    
< class  name ="CManager.NHDAL.Log.SignLogInfo, CManager.NHDAL"  table ="CM_SignLog" >
None.gif        
< id  name ="Id"  type ="Int32"  unsaved-value ="0" >
None.gif            
< column  name ="SignLogID"  sql-type ="int"  not-null ="true"  unique ="true"  index ="PK__CM_SignLog__79A81403" />
None.gif            
< generator  class ="native"   />
None.gif        
</ id >
None.gif        
< property  name ="NH_UserID"  type ="String" >
None.gif            
< column  name ="UserID"  length ="50"  sql-type ="nvarchar"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="NH_SessionID"  type ="String" >
None.gif            
< column  name ="SessionID"  length ="24"  sql-type ="nvarchar"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="NH_ClientIP"  type ="String" >
None.gif            
< column  name ="ClientIP"  length ="20"  sql-type ="nvarchar"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="NH_SignInDate"  type ="DateTime" >
None.gif            
< column  name ="SignInDate"  sql-type ="datetime"  not-null ="true" />
None.gif        
</ property >
None.gif        
< property  name ="NH_SignOutDate"  type ="DateTime" >
None.gif            
< column  name ="SignOutDate"  sql-type ="datetime"  not-null ="false" />
None.gif        
</ property >
None.gif    
</ class >
None.gif
</ hibernate-mapping >

代码段十 小技巧,NHObject基类
定义了一个NHObject的基类,因为任何实际操作对象is a NHObject,所以均继承这个类
这样做的好处是当我们需要更改加载连接字符串方法(比如解密)的时候只需在此出修改,还可以比较大面积地更改NH的使用方法。

None.gif using  System;
None.gif
using  NHibernate;
None.gif
using  NHibernate.Cfg;
None.gif
None.gif
namespace  CManager.NHDAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// NHConfigBase 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NHObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static Configuration config = new Configuration().AddAssembly(System.Reflection.Assembly.Load("CManager.NHDAL"));
InBlock.gif        
private static ISessionFactory sessionFactory = config.BuildSessionFactory();
InBlock.gif        
protected static NHibernate.ISession CreateSession()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return sessionFactory.OpenSession();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 这样一个完整的框架就成功地搭建并运行了。:)

转载于:https://www.cnblogs.com/pblee/archive/2006/05/24/407881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值