日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

nhibernate处理多数据库

發布時間:2025/3/15 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nhibernate处理多数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

nhibernate處理多數據庫


When using NHibernate in an ASP.NET application it is important to manage sessions correctly. Typically, a session-per-request pattern is used with a single session created and used for the lifetime of a request and then discarded. This helper project makes using NHibernate with ASP.NET easier by automating the creation of sessions and ensuring that they are closed after each request. It also provides some convenient methods to make coding simpler and more readable:

Automatically create the database schema:

Db.CreateDatabase();
Load an object from the database

Customer customer = (Customer)Db.Load(typeof(Customer), 123);
Save an object:

Db.Save(Customer);
Delete an object:

Db.Delete(Customer);
Access to the NHibernate Configuration and SessionFactory objects are provided if needed.

Configuration
NHibernate already has a comprehensive and flexible configuration system where settings can be defined in code, in the web.config file or in a separate XML file. Using the separate XML file enables the location of the mapping files to be configured as required. The configuration below defines the NHibernate connection properties and driver classes to use (these could also be defined in the web.config file) and the location of the nhibernate mapping files (in this case the assembly 'MyAssembly' which would contain embedded resources).

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration? xmlns="urn:nhibernate-configuration-2.0" >
<session-factory name="chat">
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!-- mapping files -->
<mapping assembly="MyAssembly" />
</session-factory>
</hibernate-configuration>

Rather than simply use the default filename "nhibernate.cfg.xml" I prefer instead to use "nhibernate.config", mainly because using an xml file extension is a security risk as the config file could easily be downloaded whereas access to the .config extension is restricted by ASP.NET automatically. To provide some extra flexibility the name of the file is specified in the <appSetting> section with the key "nhibernate.config":

??? <appSettings>
<add key="nhibernate.config" value="~/nhibernate.config" />
</appSettings>


Now, the only other thing that needs to be added to the web.config file is the entry for the HttpModule:

??? <httpModules>
<add name="NHibernateModule" type="NHibernate.Helper.Module, NHibernate.Helper" />
</httpModules>


Here is the HttpModule itself. As you can see, it is extremly simple and simply closes the connection at the end of the request.

using System;
using System.Web;
namespace NHibernate.Helper
{
/// <summary>
///??????? HttpModule to automatically close a session at the end of a request
/// </summary>
public sealed class Module : IHttpModule
{
public void Init(HttpApplication context)
{
context.EndRequest += new EventHandler(EndRequest);
}
public void Dispose() { }
public void EndRequest(Object sender, EventArgs e)
{
Db.CloseSession();
}
}
}


The Db class contains all the helper methods and manages the creation and lifetime of the sessions (with the HttpModule above).

using System;
using System.Web;
using System.Configuration;
using NHibernate;
using NHibernate.Cfg;
namespace NHibernate.Helper
{
/// <summary>
///??????? Provides access to a single NHibernate session on a per request basis.
/// </summary>
/// <remarks>
///??????? NHibernate requires mapping files to be loaded. These can be stored as
///??????? embedded resources in the assemblies alongside the classes that they are for or
///??????? in separate XML files.
///??????? <br /><br />
///??????? As the number of assemblies containing mapping resources may change and
///??????? these have to be referenced by the NHibernate config before the SessionFactory
///??????? is created, the configuration is stored in a separate config file to allow
///??????? new references to be added. This would also enable the use of external mapping
///??????? files if required.
///??????? <br /><br />
///??????? The name of the NHibernate configuration file is set in the appSettings section
///??????? with the name "nhibernate.config". If not specified the the default value of
///??????? ~/nhibernate.config is used.
///??????? <br /><br />
///??????? Note that the default config name is not used because the .xml extension is
///??????? public and could be downloaded from a website whereas access to .config files is
///??????? automatically restricted by ASP.NET.
/// </remarks>
public sealed class Db
{
/// <summary>
/// Key used to identify NHibernate session in context items collection
/// </summary>
private const string sessionKey = "NHibernate.Db";
/// <summary>
/// NHibernate Configuration
/// </summary>
private static readonly Configuration configuration = new Configuration();
/// <summary>
/// NHibernate SessionFactory
/// </summary>
private static readonly ISessionFactory sessionFactory = configuration.Configure( HttpContext.Current.Request.MapPath(ConfigurationSettings.AppSettings["nhibernate.config"]) ).BuildSessionFactory();
/// <summary>
/// NHibernate Session. This is only used for NUnit testing (ie. when HttpContext
/// is not available.
/// </summary>
private static readonly ISession session = sessionFactory.OpenSession();
/// <summary>
/// None public constructor. Prevent direct creation of this object.
/// </summary>
private Db() { }
/// <summary>
/// See beforefieldinit
/// </summary>
static Db() { }
/// <summary>
/// Creates a new NHibernate Session object if one does not already exist.
/// When running in the context of a web application the session object is
/// stored in HttpContext items and has 'per request' lifetime. For client apps
/// and testing with NUnit a normal singleton is used.
/// </summary>
/// <returns>NHibernate Session object.</returns>
[CLSCompliant(false)]
public static ISession Session
{
get
{
ISession session;
if (HttpContext.Current == null)
{
session = Db.session;
}
else
{
if (HttpContext.Current.Items.Contains(sessionKey))
{
session = (ISession)HttpContext.Current.Items[sessionKey];
}
else
{
session = Db.sessionFactory.OpenSession();
HttpContext.Current.Items[sessionKey] = session;
}
}
return session;
}
}
/// <summary>
/// Closes any open NHibernate session if one has been used in this request.<br />
/// This is called from the EndRequest event.
/// </summary>
[CLSCompliant(false)]
public static void CloseSession()
{
if (HttpContext.Current == null)
{
Db.session.Close();
}
else
{
if (HttpContext.Current.Items.Contains(sessionKey))
{
ISession session = (ISession)HttpContext.Current.Items[sessionKey];
session.Close();
HttpContext.Current.Items.Remove(sessionKey);
}
}
}
/// <summary>
/// Returns the NHibernate Configuration object.
/// </summary>
/// <returns>NHibernate Configuration object.</returns>
[CLSCompliant(false)]
public static Configuration Configuration
{
get { return Db.configuration; }
}
/// <summary>
/// Returns the NHibernate SessionFactory object.
/// </summary>
/// <returns>NHibernate SessionFactory object.</returns>
[CLSCompliant(false)]
public static ISessionFactory SessionFactory
{
get { return Db.sessionFactory; }
}
/// <summary>
/// Loads the specified object.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="id">Id.</param>
public static void Load(System.Type type, object id)
{
Db.Session.Load(type, id);
}
/// <summary>
/// Gets the specified object.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="id">Id.</param>
public static object Get(System.Type type, object id)
{
return Db.Session.Get(type, id);
}
/// <summary>
/// Save object item using NHibernate.
/// </summary>
/// <param name="item">Object to save</param>
public static void Save(object item)
{
ITransaction transaction = Db.Session.BeginTransaction();
try
{
Db.Session.Save(item);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Save object item using NHibernate.
/// </summary>
/// <param name="item">Object to delete</param>
public static void Delete(object item)
{
ITransaction transaction = Db.Session.BeginTransaction();
try
{
Db.Session.Delete(item);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Creates the specified database.
/// </summary>
/// <param name="script">Script.</param>
/// <param name="export">Export.</param>
public static void CreateDatabase()
{
NHibernate.Tool.hbm2ddl.SchemaExport se = new NHibernate.Tool.hbm2ddl.SchemaExport(Db.Configuration);
se.Create(true, true);
}
}
}

?

總結

以上是生活随笔為你收集整理的nhibernate处理多数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产第一网站 | 亚洲国产精彩视频 | 91日韩精品 | 羞羞成人| 一区二区三区四区在线播放 | 日本一区二区观看 | 看一级黄色片 | 色欲AV无码精品一区二区久久 | 不卡在线一区二区 | 亚洲视频精品 | 97精品国产97久久久久久粉红 | 影音先锋在线观看视频 | 亚洲天堂精品视频 | 久久精品人妻一区二区 | 91网站在线免费看 | 可以免费观看的毛片 | 国产成人精品免费看视频 | 亚洲乱乱| 亚洲精品视频在线播放 | avtt国产| 欧美成人69 | 日韩黄色免费观看 | 国产做a视频 | 精品国产丝袜一区二区三区乱码 | 欧美精品一区二区三区在线 | 99热久 | 久久伊人五月天 | 91亚洲视频在线 | 日本精品一区二区在线观看 | 日日夜夜爱 | 日韩精品电影一区二区三区 | 欧美一区自拍 | 日本黄色大片视频 | 欧美第一视频 | 五月天综合 | 草的我好爽 | 久久精品色| 色伊人网 | 国产精品丝袜黑色高跟鞋 | 精品美女久久久久 | 久久影视av | 无码gogo大胆啪啪艺术 | 99热99这里只有精品 | 精品国产乱码久久久久久1区2区 | 2019自拍偷拍| 天天干天天要 | 美女福利在线 | 色窝窝无码一区二区三区 | 亚洲国产一区视频 | 成人精品久久久 | 国产中文字幕精品 | 麻豆一区产品精品蜜桃的特点 | 日韩久久久 | 女人洗澡一级特黄毛片 | 视频一区 中文字幕 | 女同性恋一区二区三区 | 色夜av | 在线观看免费毛片 | 亚洲天堂网在线观看视频 | 中文字幕狠狠干 | 久久6精品 | 久久福利在线 | 国产丝袜在线播放 | 每日在线观看av | 国产精品视频123 | 少妇毛片一区二区三区粉嫩av | 伦理片av | 在线观看一二三区 | 蜜桃精品视频在线观看 | 精品人妻一区二区三区蜜桃 | 色老板精品凹凸在线视频观看 | 欧美日韩中文字幕一区二区 | 亚洲蜜臀av | 欧美成人精品在线观看 | 欧美日韩一级二级 | 男人肌肌桶女人肌肌 | 日韩三级久久 | 精品少妇v888av | 成年人视频在线看 | 国产freexxxx性播放麻豆 | 天天射天天搞 | 国产99页| 在线观看中文字幕 | 欧美一区二区三区在线观看视频 | 色哟哟av | 国产精品久久777777毛茸茸 | 亚洲中文字幕一区在线 | 黄网在线免费观看 | 国产高清一区在线 | 免费的黄色av | 国产一区二区三区免费 | 久草影视网 | 黄色网址在线免费播放 | 天天做日日干 | 日韩电影在线一区二区 | 手机av在线免费观看 | 懂色一区二区三区免费观看 | 五月婷婷六月激情 | 无码精品国产一区二区三区 |