The contents of this section
Introduced
Code generation Tools
Conclusion
Introduced
We spent a lot of space to introduce the relevant nhibernate knowledge, has always been with everyone to write code, first create the database schema, and then write the persistence class and mapping file, and finally write data manipulation methods, test methods. This is a typical database-driven development (Dbdd,database-driven developent) technology, but I didn't do it, I first wrote the persistence class and the mapping file, and then secretly used the Schemaexport tool to generate the database, According to the above steps to write the article, about the Schemaexport tool is the next thing, this article is about using the database schema to generate the persistence class and the mapping file with the code generation tool.
The so-called database-driven model is that the object model changes as the database architecture changes, so why do we still use this technology? There are two reasons: your database is the legacy of the system used, the database already has a lot of useful data, can not replace the database will be used; Your database structure is basically determined by requirements analysis, do not make any changes, in the early stage of the system design has been built in the database.
Let's use a picture to roughly show the typical database-driven development model. Take the database schema as the core.
Using the database-driven model process is when the database schema is modified, the mapping file and the persistence class are regenerated through the Code generation tool. We just need to modify the application.
Here's an extension of the question: Does the database need to be designed before the project starts?
If we are designing a database, we have a series of problems: we must consider the database table structure and table relationships that have been designed in the object-oriented domain, and consider when writing the mapping file, the inheritance, polymorphism and so on object-oriented are not used at all. So I do not recommend the project to start designing the database, what do you think? So using the code generation tool is the next worst.
Code generation Tools
As the name suggests, the code generation tool has been used, there are commercial codesmith open source mygeneration and so on, this article we describe how to use the open source Mygeneration code generation tool to generate mapping files and persistent classes based on the database schema. If you do not have Mygeneration installed on your machine, please download it here, then install Mygeneration, after installation, open mygeneration, configure "Default settings", and open the Template browser window. If these steps are not clear, please turn to NHibernate (15): Explore the use of stored procedures in the nhibernate in the article, the above steps and methods are described in detail.
Again: If your operating system is Windows Vista, right-click "Run as Administrator" mygeneration.
Here we go:
STEP1: Click on the third "online Update" button in the Template browser window: To download the template online.