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 lot of time on the knowledge of related nhibernate, has always been with everyone to write code manually, first create a database schema, then write persisted classes and mapping files, and finally write data manipulation methods, test methods. This is a typical database-driven development (Dbdd,database-driven developent) technology, but I do not do this, I first write a persistent class and mapping files, and then secretly using 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 in the object model as the database schema changes, so why do we use this technology? There are two reasons: your database is the legacy of the system used, there is already a large number of useful data in the database, can not replace the database will be used; Your database architecture is basically determined by requirement analysis, do not make any changes, the database has been built in the early stages of 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 is modified, the mapping file and the persistence class are regenerated by 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, then we have a series of problems: we must consider the pre-designed database table structure and the table relationship in the object-oriented domain, and consider the inheritance, polymorphism and other features in object-oriented. So I don't recommend designing a database at project start, 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, commercial codesmith open source mygeneration, etc., 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. If you have not installed Mygeneration on the machine, please download it here, then install Mygeneration, after installation, open mygeneration, configure "Default settings", open the "Template Browser" window, These steps, if not clear, turn to NHibernate Tour (15): Explore nhibernate using Stored procedures (above) in this article, the above steps and methods are described in detail.

Again: If your operating system is Windows Vista, right-click the "Run as Administrator" mygeneration.

In this article we proceed:

STEP1: Click on the third "online Update" button in the "Template Browser" window: To download the template online.

STEP2: The Online Template Gallery window appears, with a variety of templates available in the Online Template gallery to locate the NHibernate node:

STEP3: Expand the NHibernate node, find the "NHibernate sharp 1.4 [sharpm64]" template and right-click on "Save". This template is automatically saved to the local templates folder.

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

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

STEP6: Click on the "CustomerId" column, display the table on the right, and display the properties of the "CustomerId" column in the status bar: type int, primary key. You can also explore yourself:

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

STEP8: Click "Action" tab, we set the assembly name and namespace name, here I set Gendomainmodel, set the build project, because this template does not support VS2008, so choose to build the IDE version of VS2005. Sets the output path. Click the OK button.

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

STEP10: Open With VS2008, VS2008 automatically upgrade gendomainmodel.csproj solution files, the solution project files are as follows:

STEP11: This can be used, you can use this class library to add existing project functionality to our project as a solid persistence layer, we use this entity persistence layer to write the data Access layer method for database crud operations, Of course, before using to understand the project's class library architecture, and because the template itself is not very perfect (for example, the template does not support version control mapping, does not support many-to-many direct mapping), so we also need to modify the persistence class and mapping according to the actual situation, obviously added some burden.

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

Conclusion

The use of the Code generation tool itself is simple, the next step is to press, this section is a multi-figure show how to quickly use the code generation tool, there are many templates in mygeneration to generate NHibernate persistence class and mapping file, Each template has its own shortcomings and advantages, to generate a different structure, which is to find out ~ ~ But I found that their own handwritten code is overbearing! Clear thinking, simple code!

I don't understand this question yet: Who is the code generation tool for? Novice? Veteran? The only benefit is fast? Don't write code. But do you know the schema it generates? The idea of it?

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

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.