Nhibernate入门与demo 升级版

       在第一篇文章中有几个地方作为第一个Nhibernate入门demo还有很多不足!今天特意写点补充知识!请先阅读:Nhibernate入门与demo

以下是我们项目的升级的地方:

先看一下程序结构的截图:

问题一:关于hibernate.cfg.xml配置文件。

文件名称必须是hibernate.cfg.xml 。Nhibernate自动到项目输出中查找此文件。必须将此文件的属性设置为始终复制。

问题二:在webconfig中配置Nhibernate,不使用单独的:hibernate.cfg.xml

在webconfig中配置Nhibernate是我们另外一种配置方式。格式如下:

复制代码
代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Add this element -->
<configSections>
<section
name="hibernate-configuration"
type
="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>

<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">
Server=(local);initial catalog=hkTemp;Integrated Security=SSPI
</property>
<mapping assembly="NhibernateDemo" />
</session-factory>
</hibernate-configuration>

<!-- Leave the system.web section unchanged -->
<system.web>
...
</system.web>
</configuration>
复制代码

解释:NHibernate通过方言(dialect)区分 我们配置的是使用 Microsoft SQL Server 2005数据库并且通过指定的连接字符串连接数据库

问题三:sessionFactory 是针对一个数据库,所以我们可以采用单例模式来实现一个NhibernateHelper。看下面代码【这是官方给的NhibernateHelper实现】

复制代码
代码
public sealed class NhibernateHelper
{
private const string CurrentSessionKey = "nhibernate.currentsession";
private static readonly ISessionFactory sessionFactory;

static NhibernateHelper()
{
Configuration cfg
= new Configuration();

sessionFactory
= new Configuration().Configure().BuildSessionFactory();
}

public static ISession GetCurrentSession()
{
HttpContext context
= HttpContext.Current;
ISession currentSession
= context.Items[CurrentSessionKey] as ISession;

if (currentSession == null)
{
currentSession
= sessionFactory.OpenSession();
context.Items[CurrentSessionKey]
= currentSession;
}

return currentSession;
}

public static void CloseSession()
{
HttpContext context
= HttpContext.Current;
ISession currentSession
= context.Items[CurrentSessionKey] as ISession;

if (currentSession == null)
{
// No current session
return;
}

currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}

public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
复制代码

上面NhibernateHelper不报错的基础是:你在webconfig中正确配置Nhibernate或者添加了hibernate.cfg.xml配置文件。【注意文件名必须是这个】

这样实现后我们的调用代码就变得简单多了,看一下代码

 

ISession session = NhibernateHelper.GetCurrentSession();
//.....User 初始化
session.Save(User);
session.Delete(User);
session.Update(User);

 

 

以上是简单的这个demo的一个小小的升级!

 

源码下载:NhibernateDemo-2.0.zip

posted @   FlyDragon  阅读(3689)  评论(13编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示