NHibernate Dynamically loading resource files

Source: Internet
Author: User
Tags sql using

Recent projects have also used the ORM framework--nhibernate that was previously done.

The goal this time to achieve:

1. Simple SQL using the NHibernate session of the Crud method implementation

2. Native SQL implementation for complex SQL

3. The database may have multiple, and may be different database types, such as a system with the ORACLE,B system MSSQL

(1th is simple, you can refer to other blogs; This section focuses on 2nd 3rd, about how to configure the next record for multiple databases)

Complex SQL implementations, and for different databases, it is necessary to load different resource files.

For resource files, please refer to http://blog.csdn.net/config_man/article/details/38038249

Then we need to filter the resource files for different databases.

The idea is as follows:

Oracle database Complex SQL statement file: XXX.Oracle.hbm.xml, XXX. Oracle.hbm.xml

MSSQL database Complex SQL statement file: XXX.SQLServer.hbm.xml, XXX. SQLServer.hbm.xml

MySQL database Complex SQL statement file: XXX.MySQL.hbm.xml, XXX. SQLServer.hbm.xml

Use the configuration file to get the database type, and then genjiu the file name suffix to load what resource file.

The code is as follows:

1                     foreach(stringResourceinchmappingassembly.getmanifestresourcenames ())2                     {3                         //database type for intercepting resource files4                         intLastIndexOf = resource. LastIndexOf (". Hbm.xml");5                         stringDatabaseType = resource. Substring (0, lastIndexOf);6DatabaseType = databasetype.substring (Databasetype.lastindexof (".")+1);7 8                         if(Getdatabasetype (). ToUpper (). Equals ("SQL Server"))9                         {Ten                             //if the resource file is not a SQL Server type, the next loop One                             if(!databasetype.toupper (). Equals ("SQL Server")) A                             { -                                 Continue; -                             } the                         } -                         Else if(Getdatabasetype (). ToUpper (). Equals ("ORACLE")) -                         { -                             //if the resource file is not of type Oracle, the next loop +                             if(!databasetype.toupper (). Equals ("ORACLE")) -                             { +                                 Continue; A                             } at                         } -                         Else if(Getdatabasetype (). ToUpper (). Equals ("MYSQL")) -                         { -                             //if the resource file is not a MySQL type, the next loop -                             if(!databasetype.toupper (). Equals ("MYSQL")) -                             { in                                 Continue; -                             } to                         } +                         Else -                         { the                             //not more than three types of databases, do not load resource files *                             Continue; $                         }Panax Notoginseng                         using(Stream stream =Mappingassembly.getmanifestresourcestream (Resource)) -                         { the                             using(StreamReader reader =NewStreamReader (stream)) +                             { A _configuration. Addxmlstring (reader. ReadToEnd ()); the                             } +                         } -}

which

Mappingassembly is the assembly type,
_configuration is the NHibernate.Cfg.Configuration type
Getdatabasetype () Get the database type

NHibernate Dynamically loading resource files

Related Article

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.