Esframewor tips (2) -- use nhib.pdf in the plug-in

Source: Internet
Author: User
Let's discuss this scenario. You use the esframework-based 4-layer architecture for application development. You analyze your needs and classify them into several major parts, consider using a function plug-in for each piece. Among these plug-ins, a plug-in needs to access a database, and only the plug-in needs to access the database, according to the plug-in's "autonomous" nature, you don't want to "escalate" the data access in this plug-in to the application (FS), but make it "only" in this plug-in. In this way, it is good for both FS and plug-ins-Fs does not need to access the database itself (except for log records), plug-ins are "autonomous" and plug-ins are independent of each other, the entire system structure is very clear and simple.

Well, we decided to use nhib in the plug-in that requires data access to implement the data layer, but the problem arises-when we add a default app for the application. during the config configuration file, when the application starts, Nhibernate will automatically parse the configuration of this file. Unlike the application, we can also add an app for the plug-in. config configuration file, but when FS loads this plug-in, Nhibernate will not touch the app corresponding to this plug-in. in this way, the configuration cannot be completed automatically by nhib.pdf. Therefore, we must solve this problem manually.
Fortunately, the setproperties () method of nhib.pdf. cfg. configuration allows us to add the configuration content to nhib.pdf. Since the app. config of the plug-in does not work, we should simply use a custom configuration file to add the nhib.pdf configuration: <? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Esframeworke>
<Databasetype> Oracle </databasetype>
</Esframeworke>

<Nhib.pdf>
<Add key = "hibernate. Connection. provider" value = "nhib.pdf. Connection. driverconnectionprovider"/>
<Add key = "hibernate. Connection. driver_class" value = "nhib.pdf. 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 = "nhib.pdf. dialect. oracle9dialect"/>
</Nhib.pdf>
</Configuration>

After the configuration file is added, We need to manually parse it. Using XPath, we can easily obtain the Nhibernate configuration section in the configuration file:

Private Static idictionary getnhibernagecfgs ()
{
String XPath = string. Format ("/configuration/nhib.pdf/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;
}

When the plug-in is loaded, the plug-in reads the configuration and initializes the sessionfactory of Nhibernate. But how does one get into the loading time of the plug-in? You must remember that the onloading method in the iaddin interface of the plug-in -- that is, it adds code similar to the following:

Idictionary DIC = getnhibernagecfgs ();
Configuration Config = new configuration ();
Config. Setproperties(DIC );
Config = config. addassembly ("businessmanageraddin ");
Isessionfactory factory = config. buildsessionfactory ();

This article only solves a simple problem of Nhibernate configuration in DLL (plug-in is also DLL). However, this article aims to illustrate the idea that, solve the problem in the smallest scope (if possible), rather than spreading the problem to a larger scope. The Nhibernate configuration and initialization in the plug-in mentioned above can be fully performed in FS. However, doing so not only makes FS difficult to understand (because FS does not use sessionfactory of this configuration at all ), in addition, FS also needs to pass sessionfactory reference to the plug-in some way, which violates the plug-in "autonomy" principle, making FS no longer simple, and the relationship with the plug-in will become increasingly complex.

Appendix (2006.06.07): How to Use codesmith to assist in Automatic Generation of nhib.pdf object classes and. HBM. xml files
After the re-installation today, I re-installed codesmith and forgot how to ask codesmith to assist nhib.pdf. After a while, I found out how to fix it. Now I will record it for future reference.
(1) codesmith uses v2.5
(2) place the entire nhibernatetemplate folder under c: \ Program Files \ codesmith \ v2.5 \ samples.
(3) provide Oracle Support. Use vs2003 to open the oraclenativeschemaprovider project. The project must reference schemaexplorer. dll, which can be found under c: \ Program Files \ codesmith \ v2.5. After re-compilation, copy the compiled schemaexplorer. oraclenativeschemaprovider. dll and schemaexplorer. oraclenativeschemaprovider. dll. config file c: \ Program Files \ codesmith \ v2.5 and re-open codesmith.
(4) nhib.pdf configuration example:

<Configsections>
<Section name = "Nhibernate" type = "system. configuration. namevaluesectionhandler, system, version = 1.0.5000.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
</Configsections> <nhibctions>
<Add key = "hibernate. Connection. provider" value = "nhib.pdf. Connection. driverconnectionprovider"/>
<Add key = "hibernate. Connection. driver_class" value = "nhib.pdf. Driver. oracleclientdriver"/>
<Add key = "hibernate. Connection. connection_string" value = "User ID = xtgl; Data Source = orcl; Password = gsgl"/>
<Add key = "hibernate. Connection. Isolation" value = "readcommitted"/>
<Add key = "hibernate. dialect" value = "nhib.pdf. dialect. oracle9dialect"/>
</Nhib.pdf>

(5) Use sessionquicker to operate the Nhibernate data layer.

Go to: esframework reusable Communication Framework (sequence)

 

 

 

 

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.