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

  1. 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.
  2. Change the xmlns attribute of the hibernate-configuration element in your .cfg.xml to urn:nhibernate-configuration-2.2-x-factories.
  3. Change hibernate-configuration element hibernate-configuration-x-factories.
  4. Give the session-factory element a name and create as many session-factory elements as you like.

Including the Extension

  1. Open your Visual Studio project that already has NHibernate included.
  2. 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
0 0
原创粉丝点击