前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nhibernate入门与demo 升级版

Nhibernate入门与demo 升级版

作者头像
老马
发布2022-05-10 15:33:40
2950
发布2022-05-10 15:33:40
举报
文章被收录于专栏:老马寒门IT

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

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

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

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

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

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

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

代码语言:javascript
复制
<?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实现】

代码语言:javascript
复制
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配置文件。【注意文件名必须是这个】

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

代码语言:javascript
复制
ISession session = NhibernateHelper.GetCurrentSession();
//.....User 初始化
session.Save(User);
session.Delete(User);
session.Update(User);

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

源码下载:NhibernateDemo-2.0.zip

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2010-09-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档