I have been using nhib.pdf for nearly a year. I have sent an example of a recent project. If you want to learn about Nh, you can download it for reference.
It also includes the mode of operations Ser and Radius, ing compound primary key, guid primary key
The Code contains three items:
1. NhibernateService, which encapsulates NhFactory and NHSession. You can easily add other databases. Currently, two MySql databases are connected, one SqlServe
Add a new database:
Add a class: simply inherit SessionFactory, as shown below:
Class SerSessionFactory: SessionFactory
{
Public static readonly SessionFactory _ sessionFactory = new SerSessionFactory ();
Protected SerSessionFactory ()
: Base ("Ser. cfg. xml ")
{
}
}
Add a configuration file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Hibernate-configuration xmlns = "urn: nhibernate-configuration-2.0">
<Session-factory>
<Property name = "connection. provider"> nhib.pdf. Connection. DriverConnectionProvider </property>
<Property name = "connection. driver_class"> nhib.pdf. Driver. MySqlDataDriver </property>
<Property name = "connection. connection_string"> Server = 203.193.42.37; initial catalog = radius; uid = **; pwd = ** </property>
<Property name = "show_ SQL"> false </property>
<Property name = "dialect"> nhib.pdf. Dialect. MySQLDialect </property>
<! -- <Property name = "use_outer_join"> true </property>
<Property name = "query. substitutions"> true 1, false 0, yes 'y', no 'n' </property> -->
<Mapping assembly = "LoginBase"/>
</Session-factory>
</Hibernate-configuration>
Then you can use Repository to operate the database.
Repository encapsulates the NhSession and can operate on all the configured databases. It mainly references the implementation of Cuyahoga. For a Web project, see: http://bluewater.cnblogs.com/archive/2006/05/29/412254.html
2. LoginBase provides a class library for LoginService, and uses the library operator Ser, Radius, and Sqlserver provided by NhibernateService. Because LoginServie requires high flexibility
This project applies a large number of configuration files. Including login. config, errMessage. config, log4net. config, ser. cfg. xml, login. cfg. xml, radius. cfg. xml
By using serialization to convert configuration files and classes, the code is much simpler, and the flexibility is not as good as reading in XMl directly. Summary:
1. Use xmlspyto generate xsdks and use xsd.exe to generate classes.
2. serialize and convert some xml and classes by yourself.
3. write xml by yourself. The class can be read and converted to the class through xmlreader, which is the most flexible. Many functions that cannot be implemented by serialization, such as HashTable. See my http://bluewater.cnblogs.com/archive/2006/05/25/408844.html
This project adopts the second method. The use of xmlattribute can bring some flexibility.
Exception and error handling:
This project Is not complete in this regard. It Is mainly confused about when to use Exception, when to use error codes, and when to use a detection function such as Is.
Use error codes: 1. The efficiency will be better, but if the customer does not have a return code for Swich, many errors will occur.
2. It is difficult to find the cause of the error, because some returned values are hidden during the call process, and the error code itself carries less information. Logs can be added.
3. Some programs use return codes to increase complexity.
In the past, almost every project had to return code and encountered a lot of problems.
The Director also said many advantages of error codes and some applications, but I still think it is not necessary to use exceptions.
Exception Handling structure:
Define a class containing Exception information: The LoginException class does not inherit any. net Exception. Because some clients cannot detect SoapException, it is not convenient to analyze <fault>.
Define the return value class: UserInfo inherits LoginException. Inheritance brings convenience in encoding, although it seems unreasonable. The client can obtain the error message and return value through UserINfo and decide whether the return value is valid.
This project should not return UserInfo in LoginBase, which is not reasonable. I think UserInfo should be returned in LoginService.
I hope to talk about the error handling method. I still feel confused. It is difficult to find a perfect solution.
Is ** and other test functions also provide a lot of convenience.
3. Simple Service calling of LoginService
4. LoginTest, there is nothing, and there are only a few test cases. This project uses vs's own unit test, which is much worse than Nunit.
:
Http://www.cnblogs.com/files/bluewater/login.rar
Please correct the incorrect code