Content Summary
Preparatory work
Development process
Program Development
First, the preparatory work
1.1 Development environment
Development tools: VS2008 above, I use VS2010
Database: Any relational database, I am using SQL Server 2005 Express
1.2 Test environment
NUnit 2.5.7
Second, the development process
The development process for the NHibernate program is:
(1). Writing domain classes and mapping files
(2). Use the NHibernate tool to generate the corresponding database structure
(3). Writing DAO (Database Access Object)
(4). Use NUnit to test the increase, deletion, and search methods of DAO (data Access Object)
Third, the development of procedures
3.1 Build the domain project, as shown in 3.1.1.
Figure 3.1.1
Writing class Files Product.cs
Product
Writing the mapping file Product.hbm.xml
Product.hbm.xml
Then, set the property " build as" of the mapping File "Product.hbm.xml" to " Embedded Resource ", as shown in 3.1.2.
Figure 3.1.2
3.2 Create a project named "Nhibernatetest", 3.2.1
Figure 3.2.1
Referencing the assembly "Antlr3.Runtime.dll", "Iesi.Collections.dll", "NHibernate.dll", "Remotion.Data.Linq.dll", " Nunit.framework.dll ", 3.2.2 shown
Figure 3.2.2
Then the music domain project, copy and paste the NHibernate configuration template into the project, shown in 3.2.3
Figure 3.2.3
Modify the properties of the file to " always copy "
Hibernate.cfg.xml
Create a "NHibernateInit.cs" class file to initialize the table structure of the database
NHibernateInit.cs
Copy the Assembly "LinFu.DynamicProxy.dll" and "NHibernate.ByteCode.LinFu.dll" of the Proxyfactory class into the project and modify how it is generated, as shown in 3.2.4
Figure 3.2.4
Set the startup action for the project properties, start external program , and then select the path to the NUnit application. As shown in 3.2.5.
Figure 3.2.5
Open SQL Server Management Studio Express, create a database named "Nhibernatedemo", 3.2.6
Figure 3.2.6
With NUnit enabled, select the assembly with the name "NHibernateTest.dll". As shown in 3.2.7. Next, click the "Run" button to run NUnit.
Figure 3.2.7
At this point, we open the database again, we will find that NHibernate has established a "t_product" table for us, 3.2.8 is shown.
Figure 3.2.8
3.3 Write a DAO (Database Access Object) and build a project called "DAO". As shown in 3.3.1.
Figure 3.3.1
Refer to the assembly required by the project, and then write the Iproductdao interface and PROductdao class
Productdao
Then write the test class "Productdaotest" in the Test Project "Nhibernatetest".
productdaotest
Finally run NUnit to test the project. The effect is shown in 3.3.2.
Figure 3.3.2
Well, a nhibernate complete project is done. From which we can find that this application project does not write an SQL statement, it can achieve the data increase, delete, the, check.
This simplifies our project development. O (∩_∩) o~
NHibernate from getting Started to Mastering series (3)--The first NHibernate application