博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于NHibernate的三层结构应用程序开发初步
阅读量:5133 次
发布时间:2019-06-13

本文共 6357 字,大约阅读时间需要 21 分钟。

本文并不是去详细的介绍如何使用NHibernate,而是通过一个简单的例子来演示基于NHibernate的三层结构应用程序开发过程。关于NHibernate的有关文档,DDL已经做了汉化,但是由于英文文档自身就不完善,所以汉化后也是不全。菩提树在一篇《》随笔中谈到了学习NHibernate遇到的困难,也希望大家把自己在使用NHibernate中的经验和心得能够共享出来,与大家分享。另外我也是刚开始接触NHiernate,有错误之处还请大家指点。

第一步:准备数据表

在这里用一个最简单的例子,有一张关于的用户的表,有编号,姓名,密码,Email地址和最后一次的登录时间几个字段。

None.gifCreate Table Users(

None.gif
None.gif    LogonID varchar(20) Primary key,
None.gif
None.gif    Name varchar(40),
None.gif
None.gif    Password varchar(20),
None.gif
None.gif    EmailAddress varchar(40) ,
None.gif
None.gif    LastLogon datetime
None.gif
None.gif)

第二步:创建需要被持久化的类

在.NET中创建一个NHibernateWebDemo.Model的工程,添加User实体类。

None.gif//User.cs

None.gif
None.gifusing System;
None.gif
None.gifnamespace NHibernateWebDemo.Model
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif public class User
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif public User()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{   
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif private string id;
InBlock.gif
InBlock.gif private string userName;
InBlock.gif
InBlock.gif private string password;
InBlock.gif
InBlock.gif private string emailAddress;
InBlock.gif
InBlock.gif private DateTime lastLogon;
InBlock.gif
InBlock.gif public string Id 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return id; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif set dot.gif{ id = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public string UserName 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return userName; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif set dot.gif{ userName = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public string Password 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return password; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif set dot.gif{ password = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public string EmailAddress 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return emailAddress; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif set dot.gif{ emailAddress = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public DateTime LastLogon 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif get dot.gif{ return lastLogon; }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif set dot.gif{ lastLogon = value; }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedBlockEnd.gif}

第三步:创建持久化映射文件

该文件的命名为User.hbm.xml,并且与User.cs放在同一个目录里。设置该文件的生成操作属性为“嵌入的资源”,这一点要切记。另外,使用编号当作主键,由用户输入,所以在映射文件中用assigned。

None.gif<?xml version="1.0" encoding="utf-8" ?>

None.gif
None.gif<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
None.gif
None.gif <class name="NHibernateWebDemo.Model.User, NHibernateWebDemo.Model" table="users">
None.gif
None.gif <id name="Id" column="LogonId" type="String" length="20">
None.gif
None.gif <generator class="assigned" />
None.gif
None.gif </id>
None.gif
None.gif <property name="UserName" column= "Name" type="String" length="40"/>
None.gif
None.gif <property name="Password" type="String" length="20"/>
None.gif
None.gif <property name="EmailAddress" type="String" length="40"/>
None.gif
None.gif <property name="LastLogon" type="DateTime"/>
None.gif
None.gif </class>
None.gif
None.gif</hibernate-mapping>

第四步:进行配置文件的设置

在配置文件中,我们要告诉NHibernate所使用的数据库是什么,以及如何连接该数据库。

None.gif<configSections>

None.gif
None.gif <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
None.gif
None.gif </configSections>
None.gif
None.gif<nhibernate>
None.gif
None.gif <add key="hibernate.connection.provider"
None.gif
None.gif                       value="NHibernate.Connection.DriverConnectionProvider"/>
None.gif
None.gif <add key="hibernate.dialect"
None.gif
None.gif                       value="NHibernate.Dialect.MsSql2000Dialect"/>
None.gif
None.gif <add key="hibernate.connection.driver_class"
None.gif
None.gif                       value="NHibernate.Driver.SqlClientDriver"/>
None.gif
None.gif <add key="hibernate.connection.connection_string"
None.gif
None.gif                       value="server=.;uid=sa;pwd=sa;database=test"/>
None.gif
None.gif </nhibernate>

第五步:编写数据访问层的公用类

在这里,编写了两个公用的类,分别进行Session的创建和实体的操作。在这两个类中用单件模式,来限制Session的创建。为了做到与具体的应用程序无关,在这里把程序集的名称作为参数,传递给OpenSession()方法。可以把这两个类单独放在一个名为Common的工程下,这里先把它们放在DAL层中。这两个类只是个人的一种写法,大家可以自行去编写。

None.gif//SessionFactory.cs

None.gif
None.gifusing System;
None.gif
None.gifusing System.Reflection;
None.gif
None.gifusing System.Data;
None.gif
None.gifusing NHibernate;
None.gif
None.gifusing NHibernate.Cfg;
None.gif
None.gifusing NHibernate.Tool.hbm2ddl;
None.gif
None.gifnamespace NHibernateWebDemo.DAL
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif public class SessionFactory
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif public SessionFactory()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif private static ISessionFactory sessions;
InBlock.gif
InBlock.gif private static Configuration cfg;
InBlock.gif
InBlock.gif static readonly object padlock = new object();
InBlock.gif
InBlock.gif public static ISession OpenSession(string AssemblyName)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif if(sessions == null)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif lock(padlock)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif if(sessions == null)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                        BuildSessionFactory(AssemblyName);
InBlock.gif
ExpandedSubBlockEnd.gif                    }
InBlock.gif
ExpandedSubBlockEnd.gif                }
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif return sessions.OpenSession();
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif private static void BuildSessionFactory(string AssemblyName)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            cfg = new Configuration();
InBlock.gif
InBlock.gif            cfg.AddAssembly(AssemblyName);
InBlock.gif
InBlock.gif            sessions = cfg.BuildSessionFactory();
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedBlockEnd.gif}

None.gif//EntityControl.cs

None.gif
None.gifusing System;
None.gif
None.gifusing System.Collections;
None.gif
None.gifusing NHibernate;
None.gif
None.gif
None.gif
None.gifnamespace NHibernateWebDemo.DAL
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif public class EntityControl
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif private static EntityControl entity;
InBlock.gif
InBlock.gif private string _AssemblyName;
InBlock.gif
InBlock.gif static readonly object padlock = new object();
InBlock.gif
InBlock.gif public static EntityControl CreateEntityControl(string AssemblyName)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif if(entity == null)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif lock(padlock)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif if(entity == null)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                        entity = new EntityControl();
InBlock.gif
InBlock.gif                        entity._AssemblyName = AssemblyName;
InBlock.gif
ExpandedSubBlockEnd.gif                    }
InBlock.gif
ExpandedSubBlockEnd.gif                }
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif return entity;
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void AddEntity(Object entity)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            ISession session = SessionFactory.OpenSession(_AssemblyName);
InBlock.gif
InBlock.gif            ITransaction transaction = session.BeginTransaction();
InBlock.gif
InBlock.gif try
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                session.Save(entity);
InBlock.gif
InBlock.gif                transaction.Commit();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif catch(Exception ex)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                transaction.Rollback();
InBlock.gif
InBlock.gif throw ex;
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif finally
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                session.Close();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void UpdateEntity(Object entity,Object key)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            ISession session = SessionFactory.OpenSession(_AssemblyName);
InBlock.gif
InBlock.gif            ITransaction transaction = session.BeginTransaction();
InBlock.gif
InBlock.gif try
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                session.Update(entity,key);
InBlock.gif
InBlock.gif                transaction.Commit();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif catch(Exception ex)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                transaction.Rollback();
InBlock.gif
InBlock.gif throw ex;
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif finally
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                session.Close();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void DeleteEntity(object entity)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            ISession session = SessionFactory.OpenSession(_AssemblyName);
InBlock.gif
InBlock.gif            ITransaction transaction = session.BeginTransaction();
InBlock.gif
InBlock.gif try
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                session.Delete(entity);
InBlock.gif
InBlock.gif                transaction.Commit();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif catch(Exception ex)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                transaction.Rollback();
InBlock.gif
InBlock.gif throw ex;
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif finally
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif                session.Close();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public IList GetEntities(string strHQL)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            IList lst;
InBlock.gif
InBlock.gif            ISession session = SessionFactory.OpenSession(_AssemblyName);
InBlock.gif
InBlock.gif            ITransaction transaction = session.BeginTransaction();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            lst=session.Find(strHQL);
InBlock.gif
InBlock.gif            transaction.Commit();
InBlock.gif
InBlock.gif            session.Close();
InBlock.gif
InBlock.gif return lst;
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedBlockEnd.gif}

第六步:编写数据访问层

创建一个名为NHibernateWebDemo.DAL的工程,数据访问层的代码编写非常简单,在创建EntityControl的实例时,需要把Model的程序集名称作为参数传入,可以通过配置文件来避免程序集名称的硬编码。

None.gif//UserDAL.cs

None.gif
None.gifusing System;
None.gif
None.gifusing System.Collections;
None.gif
None.gifusing NHibernateWebDemo.Model;
None.gif
None.gifnamespace NHibernateWebDemo.DAL
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif public class UserDAL
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif private EntityControl control;
InBlock.gif
InBlock.gif public UserDAL()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            control = EntityControl.CreateEntityControl("NHibernateWebDemo.Model");
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void AddUser(User user)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            control.AddEntity(user);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void UpdateUser(User user,string Id)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            control.UpdateEntity(user,user.Id);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void DeleteUser(User user)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            control.DeleteEntity(user);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public IList GetAllUsers(string strHQL)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif return control.GetEntities(strHQL);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedBlockEnd.gif}

第七步:编写业务逻辑层

建立NHibernateWebDemo.BLL工程,为了简单期间,在业务逻辑层中我没有做任何的业务检测。

None.gif//UserBLL.cs

None.gif
None.gifusing System;
None.gif
None.gifusing System.Collections;
None.gif
None.gifusing NHibernateWebDemo.DAL;
None.gif
None.gifusing NHibernateWebDemo.Model;
None.gif
None.gifnamespace NHibernateWebDemo.BLL
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif public class UserBLL
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif public void AddUser(User user)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif
InBlock.gif
InBlock.gif            UserDAL dal = new UserDAL();        
InBlock.gif
InBlock.gif            dal.AddUser(user);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void UpdateUser(User user,string Id)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            UserDAL dal = new UserDAL();
InBlock.gif
InBlock.gif            dal.UpdateUser(user,Id);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public void DeleletUser(User user)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            UserDAL dal = new UserDAL();
InBlock.gif
InBlock.gif            dal.DeleteUser(user);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif public IList GetAllUsers(string strHQL)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif            UserDAL dal = new UserDAL();
InBlock.gif
InBlock.gif return dal.GetAllUsers(strHQL);
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedBlockEnd.gif}

第八步:实现用户界面

用户界面很简单,这里就不给出代码了,完成后的用户界面:

PIC087.gif

转载于:https://www.cnblogs.com/360S/archive/2011/09/28/2194123.html

你可能感兴趣的文章
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
继承条款effecitve c++ 条款41-45
查看>>
Java泛型的基本使用
查看>>
1076 Wifi密码 (15 分)
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
图片等比例缩放及图片上下剧中
查看>>
background-clip,background-origin
查看>>
【Linux】ping命令详解
查看>>
对团队成员公开感谢博客
查看>>
java学习第三天
查看>>
django+uwsgi+nginx+sqlite3部署+screen
查看>>