Esframework Use Tips (2)-Use NHibernate in Plug-ins

Source: Internet
Author: User
Tags config xpath

Let's talk about this scenario where you use the 4-tier architecture based on Esframework for application development, you analyze the needs of users, and sort them into chunks, and consider each piece using a single functional plug-in. In these several plug-ins, there is a plug-in need to access a database, and only the plug-in needs to access the database, according to the "autonomous" nature of the plug-in, you do not want the data access in this plug-in to "spread" to the application (FS), but let it "just" in this plug-in, so, Whether it is for the FS or plug-ins are good--fs themselves do not need access to the database (except logging), Plug-ins "autonomous", and Plug-ins are independent, the entire system structure is very clear and simple.

Well, we decided to use NHibernate to implement the data tier in the plugin that needs data access, But here's the problem-when we add a default app.config profile for the application, NHibernate automatically resolves the configuration of the file when the application starts, and unlike the application, we can add a app.config profile for the plugin, but FS is loading This plugin, NHibernate will not touch this plugin corresponding to the app.config, so that NHibernate can not automatically complete the configuration. So, we have to solve this problem by hand.

Fortunately, the NHibernate.Cfg.Configuration setproperties () method allows us to add configuration content to the NHibernate. Now that the app.config of the plugin doesn't work, we simply use a custom profile, which joins the NHibernate configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<ESFrameworke>
<DataBaseType>Oracle</DataBaseType>
</ESFrameworke>

<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
<add key="hibernate.connection.connection_string" value="user id=jjaj;data source=ORCL;password=gsaj" />
<add key="hibernate.connection.isolation" value="ReadCommitted"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.Oracle9Dialect" />
</nhibernate>
</configuration>

After the configuration file is added, we need to parse it ourselves manually, and using XPath makes it easy to get the NHibernate configuration part of the configuration file:

private static IDictionary GetNHibernageCfgs()
{
string xPath = string.Format("/configuration/nhibernate/add") ;

XmlNodeList list = XmlDoc.SelectNodes(xPath) ;
IDictionary dic = new Hashtable() ;
foreach(XmlNode node in list)
{
dic.Add(node.Attributes.Item(0).InnerText ,node.Attributes.Item(1).InnerText) ;
}

return dic ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.