Build a module that use a different database than CUY.
Scenario:
You have a site using CUY as a CMS, also you have another site that has a different database.
For example first site using a database called ‘cuy_db’ and the database of other site called ‘sample_db’ .
Now you built a module base on CUY framework and put it on ‘cuy_db’ and want to read data from ‘sample_db’:
1: add the properties of 2nd database to Config/properties.config :
Code:
< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
< properties>
< !-- Database -->
< !-- SQL Server settings -->
< !—first DB Connection String -->
< connectionString>Data Source=(local)\SQLEXPRESS;Initial Catalog= cuy_db;Integrated Security=True< /connectionString>
< nhibernateDriver>NHibernate.Driver.SqlClientDriver< /nhibernateDriver>
< nhibernateDialect>NHibernate.Dialect.MsSql2000Dialect< /nhibernateDialect>
< !—second DB Connection String -->
< connectionString2nd>Data Source=(local)\SQLEXPRESS;Initial Catalog= sample_db;Integrated Security=True< /connectionString2nd >
< nhibernateDriver2nd>NHibernate.Driver.SqlClientDriver< /nhibernateDriver2nd>
< nhibernateDialect2nd>NHibernate.Dialect.MsSql2000Dialect< /nhibernateDialect2nd>
< !-- Email. -->
< smtpHost>localhost< /smtpHost> < !-- required-->
< smtpPort>< /smtpPort>
< smtpUsername>< /smtpUsername>
< smtpPassword>< /smtpPassword>
< emailEncoding>utf-8< /emailEncoding> < !-- iso-8859-1, , us-ascii, etc. -->
< tempDir>< /tempDir>
< /properties>
< /configuration>
2: change Config/facilities.config to: (check < assembly>CUY.Modules.Test< /assembly> and < factory id="nhibernate.factory2" alias="seconddb" > )
Code:
< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
< facilities>
< facility type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"
id="nhibernate" isWeb="true" useReflectionOptimizer="false">
< factory id="nhibernate.factory">
< settings>
< item key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider< /item>
< item key="hibernate.connection.driver_class">#{nhibernateDriver}< /item>
< item key="hibernate.dialect">#{nhibernateDialect}< /item>
< item key="hibernate.connection.connection_string">#{connectionString}< /item>
< item key="hibernate.cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache< /item>
< item key="hibernate.cache.use_query_cache">true< /item>
< item key="relativeExpiration">30< /item>
< /settings>
< assemblies>
< assembly>Cuyahoga.Core< /assembly>
< /assemblies>
< /factory>
< !—for second database-->
< factory id="nhibernate.factory2" alias="seconddb">
< settings>
< item key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider< /item>
< item key="hibernate.connection.driver_class">#{connectionString2nd}< /item>
< item key="hibernate.dialect">#{connectionString2nd}< /item>
< item key="hibernate.connection.connection_string">#{connectionString2nd}< /item>
< item key="hibernate.cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache< /item>
< item key="hibernate.cache.use_query_cache">true< /item>
< item key="relativeExpiration">30< /item>
< /settings>
< assemblies>
< !—map files from CUY.Modules.Test -->
< assembly>CUY.Modules.Test< /assembly>
< /assemblies>
< /factory>
< /facility>
< facility type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement"
id="autotransaction" />
< /facilities>
< /configuration>
3- use the 2nd database in CUY.Modules.Test module by : this._sessionManager.OpenSession( alias name )
Code:
public IList GetAll()
{
ISession session = this._sessionManager.OpenSession("seconddb");
ICriteria crit = session.CreateCriteria(typeof(test1));
return crit.List();
}