NHibernate Tour (18): A preliminary discussion of the Code generation tool using

Source: Internet
Author: User

The content of this section

    • Introduced
    • Code generation Tools
    • Conclusion
Introduced

We have spent a great deal of space on the knowledge of the relevant nhibernate. It always takes everyone to write code manually, first create the database schema. Then write persisted classes and mapping files, and finally write data manipulation methods. Test method.

This is a typical database-driven development (Dbdd,database-driven developent) technology. But I did not do this, I first write the persistence class and mapping files, and then secretly use the Schemaexport tool to generate the database. Follow the above steps to write the article, about the Schemaexport tool is the next thing, this article uses the database schema to generate persistence classes and mapping files with the code generation tool.

The so-called database-driven model refers to the change of the object model as the database schema changes. So why do we still use this technology? There are two reasons: your database is the legacy of the system used, there is already a large number of practical data in the database. Not being able to replace the database will be used. Your database architecture is basically determined in terms of requirements analysis, and do not make any changes whatsoever. The database has been built at the beginning of the system design.

Let's use a picture to roughly show a typical database-driven development model. Take the database schema as the core.

Using a database-driven model process is when the database schema changes, the mapping file and the persistence class are generated once again through the Code generation tool.

We just have to change the application to do it.

Here's an extension of the question: Does the database need to be designed before the project starts?

Assuming that we are designing a database, we have a series of questions: we must consider the pre-designed database table structure and table relationships in the object-oriented domain to design the persisted object, as well as when writing the mapping file. In object-oriented inheritance, polymorphism and other features are not available at all. So I don't recommend designing a database at the start of a project. What do you think? So using code generation tools is the bottom line.

Code generation Tools

As the name implies, the code generation tool has been used, there are commercial codesmith open source mygeneration and so on, this article describes how to use the open source Mygeneration code generation tool to generate mapping files and persisted classes based on the database schema.

Suppose you haven't installed Mygeneration on your machine yet. Please download here and install Mygeneration. After installation, open mygeneration, configure default settings, open the template browser form. If these steps are not clear, please turn to NHibernate Tour (15): Explore nhibernate using Stored procedures (above) in the article. The above steps and methods are described in detail.

Again: Assuming that your operating system is Windows Vista, right-click the "Execute as Administrator" mygeneration.

In this article we proceed:

STEP1: Click on the third "online Update" button in the "Template browser" form to download the template online.

STEP2: The online Template gallery form appears. A variety of templates are available in the online template gallery. Locate the NHibernate node:

STEP3: Expand the NHibernate node, find the "NHibernate sharp 1.4 [sharpm64]" template and right-click on "Save". This template will be saved to the local template directory on its own initiative.

STEP4: Click the first "Refresh" button in the "Template Browser" form, this template will be visible in the "template browser", expand the NHibernate node, right-click on the "Run" NHibernate Sharp1.4 template.

STEP5: This is the NHibernate Ohm interface form, and the right side displays the table, view, and action label. On the Table tab interface. On the right there are no mappings, mappings, and so on buttons, and different colors in the table represent different properties of the field.

STEP6: Click on the "CustomerId" column. The table is displayed on the right and the properties of the "CustomerId" column are displayed in the status bar: type int, primary key. Also be able to explore for yourself:

STEP7: Click on the "View" tab and set CustomerID as the primary key.

STEP8: Click "Actions" tab, we set the assembly name and namespace name in detail, here I set Gendomainmodel, set build project. Because this template does not support VS2008. So choose the Build IDE version number to VS2005. Sets the output path. Click OKButton.

STEP9: When the directory is opened, the Code generation tool generates corresponding persisted classes and mapping files based on the database schema.

STEP10: Open with VS2008, VS2008 own initiative to upgrade Gendomainmodel.csproj solution file. Solution project files such as the following:

STEP11: You can use it now. Being able to add this class library to our project as an entity persistence layer by adding existing project functionality, we use this entity persistence layer to write data access layer methods to implement CRUD operations on the database. Of course, before using the project to understand the class library architecture. In addition, because the template itself is not very good (such as the template does not support version number control mapping, does not support many-to-many direct mapping), so we also need to change the persistence class and mapping according to the actual situation, obviously added some burden.

Note Do not forget to change in Hibernate.cfg.xml <mapping assembly="domainmodel"/ > for <mapping assembly="gendomainmodel"/>.

Conclusion

The use of the Code generation tool itself is very easy, is the next step to press. This section is a multi-figure demonstration of how to use code generation tools at high speed, and there are a lot of templates in mygeneration to generate nhibernate persistence classes and mapping files. Each template has its own shortcomings and strengths. Generate different structure, this is to find out the ~ ~ but I found that my own handwriting code is overbearing! Clear thinking. The code is simple!

This question is unclear: who is the code generation tool for? Novice? Veteran? The only advantage is fast? Don't write code.

But do you know the schema it generates? The idea of it?

NHibernate Tour (18): A preliminary discussion of the Code generation tool using

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.