NHibernate连多个数据库,NHibernate X-Factories
来源:互联网 发布:三级域名属于哪个 编辑:程序博客网 时间:2024/05/08 12:16
NHibernate X-Factories v1.2.0
NHibernate X-Factories allow you to combine multiple .cfg.xml into one. X-Factories does this by allowing each session-factory to be named and refer to them individually by name. This gives a cleaner and less verbose configuration for using NHiberate between multiple databases.
Setup
Setup is really quite simple. Just add the schema, include the extension and you're good to go.
Adding the Schema
- Include the schema in your Project, Solution, or Visual Studios XML Schemas folder on your computer. Should be something similar to
%ProgramFiles%\Microsoft Visual Studio 10.0\Xml\Schemas
. - Change the
xmlns
attribute of thehibernate-configuration
element in your .cfg.xml tourn:nhibernate-configuration-2.2-x-factories
. - Change
hibernate-configuration
elementhibernate-configuration-x-factories
. - Give the
session-factory
element a name and create as manysession-factory
elements as you like.
Including the Extension
- Open your Visual Studio project that already has NHibernate included.
- Copy the
ConfigurationExtensions.cs
into the project.
Note: Visual Studio Website projects might require that the extension be located in the App_Code
folder.
XFactoriesExtensions.cs
//--------------------------------------------------------------------------------------------
// NHibernate-X-Factories v1.2.0, Copyright 2014 roydukkey, 2014-10-14 (Tue, 14 Oct 2014).
// Dual licensed under the MIT (http://www.roydukkey.com/mit) and
// GPL Version 2 (http://www.roydukkey.com/gpl) licenses.
//--------------------------------------------------------------------------------------------
namespace NHibernate.Cfg
{
using ConfigurationSchema;
using System;
using System.IO;
using System.Xml;
public static class XFactoriesExtensions
{
public const string CONFIG_MUTATION_SUFFIX = "-x-factories";
private const string CHILD_PREFIX_PATH = CfgXmlHelper.CfgNamespacePrefix + ":";
private const string ROOT_PREFIX_PATH = "//" + CHILD_PREFIX_PATH;
/// <summary>
/// Configure NHibernate from a specified session-factory.
/// </summary>
/// <param name="config"></param>
/// <param name="fileName">System location of '.cfg.xml' configuration file.</param>
/// <param name="factoryName">Name value of the desired session-factory.</param>
/// <returns></returns>
public static Configuration Configure(this Configuration config, string fileName, string factoryName)
{
// Load Configuration XML
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
return config.Configure(PrepareConfiguration(doc, factoryName));
}
/// <summary>
/// Configure NHibernate from a specified session-factory.
/// </summary>
/// <param name="config"></param>
/// <param name="textReader">The XmlReader containing the hibernate-configuration.</param>
/// <param name="factoryName">Name value of the desired session-factory.</param>
/// <returns></returns>
public static Configuration Configure(this Configuration config, XmlReader textReader, string factoryName)
{
// Load Configuration XML
XmlDocument doc = new XmlDocument();
doc.Load(textReader);
return config.Configure(PrepareConfiguration(doc, factoryName));
}
/// <summary>
/// Parses the configuration xml and ensures the target session-factory is the only one included.
/// </summary>
/// <param name="doc">The XmlDocument containing the hibernate-configuration.</param>
/// <param name="factoryName">Name value of the desired session-factory.</param>
/// <returns></returns>
private static XmlTextReader PrepareConfiguration(XmlDocument doc, string factoryName)
{
// Add Proper Namespace
XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(doc.NameTable);
namespaceMgr.AddNamespace(CfgXmlHelper.CfgNamespacePrefix, CfgXmlHelper.CfgSchemaXMLNS + CONFIG_MUTATION_SUFFIX);
// Query Elements
XmlNode nhibernateNode = doc.SelectSingleNode(ROOT_PREFIX_PATH + CfgXmlHelper.CfgSectionName + CONFIG_MUTATION_SUFFIX, namespaceMgr);
if (nhibernateNode != null) {
if (nhibernateNode.SelectSingleNode(ROOT_PREFIX_PATH + "session-factory[@name='" + factoryName + "']", namespaceMgr) != default(XmlNode))
foreach (XmlNode node in nhibernateNode.SelectNodes(ROOT_PREFIX_PATH + "session-factory[@name!='" + factoryName + "']", namespaceMgr))
nhibernateNode.RemoveChild(node);
else
throw new Exception(String.Format("<session-factory name=\"{0}\"> element was not found in the configuration file.", factoryName));
}
else
throw new Exception(String.Format("<{1}{0} xmlns=\"{2}{0}\"> element was not found in the configuration file.", CONFIG_MUTATION_SUFFIX, CfgXmlHelper.CfgSectionName, CfgXmlHelper.CfgSchemaXMLNS));
return new XmlTextReader(new StringReader(nhibernateNode.OuterXml.Replace(CONFIG_MUTATION_SUFFIX, "")));
}
}
}
Usage
<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration-x-factories xmlns="urn:nhibernate-configuration-2.2-x-factories"> <session-factory name="Development"> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Server=dsql01;DataBase=dbDev;uid=nhDeveloper;pwd=pass1234 </property> <property name="show_sql">true</property> <mapping assembly="DataLayer" /> </session-factory> <session-factory name="Production"> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Server=psql02;DataBase=dbDev;uid=nhDeveloper;pwd=pass5678 </property> <property name="show_sql">false</property> <mapping assembly="DataLayer" /> </session-factory></hibernate-configuration-x-factories>
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();config .Configure(HostingEnvironment.MapPath("~/nhibernate.cfg.xml"), "Development") .BuildSessionFactory();
Support
Required
- .Net 3.5 ≥
- NHibernate (http://nhforge.org/Default.aspx)
Optional
- Configuration from Web.Config
- NHibernate连多个数据库,NHibernate X-Factories
- NHibernate
- nhibernate
- NHibernate
- NHibernate
- NHibernate
- Nhibernate
- NHibernate
- NHibernate
- Nhibernate
- NHibernate
- NHibernate
- NHibernate
- NHibernate
- NHibernate
- NHibernate
- NHibernate
- NHibernate
- 从源码角度认识ArrayList,LinkedList与HashMap
- android 沉浸式状态栏
- 【opencv练习23 - 霍夫直线变换——直线检测】
- javascript变量作用域
- java的初学者,最想知道的莫过于如何才能学好java
- NHibernate连多个数据库,NHibernate X-Factories
- Android基础————UI布局之UI美化分解详解
- PDO简介
- SSM框架----修改mybatis-generate源码实现实体类加上字段注释
- Java加解密与数字签名
- 数字电视机顶盒的接收过程
- 多线程总结之旅(3):多线程的优缺点
- 线程池的原理及实现
- JAVA NIO系列(三) Buffer 解读