Use of the [NHibernate] code generator

Source: Internet
Author: User

Directory

Write in front

Documentation and series articles

Use of code generators

Summarize

Write in front

The previous article introduced the relevant knowledge of nhibernate, are the code of their own hands, sometimes appear special trouble, such as you have to write persistent classes, mapping files and so on, for example is relatively simple, less fields, if a data table has dozens of fields, you are bored of the hand to die, But in order to learn the grammar of NHibernate, the beginner's process is to suggest that you do, so knock on each process, and the meaning of the node in the mapping file has a general impression. In the example above, you will also find that the steps in this way are to create the database first, then write the persisted class, map the file, and finally write the code of the operational data, which is a typical database-driven development (Dbdd,database-driven developent) technology. Can this step be reversed? Just like the code first in EF, encode and then build the database. Of course, you can use the Schemaexport tool to generate the database. Here is the first to talk about the use of the Mygeneration code generator used.

What is a database-driven model?

The object model changes as the database schema changes. So why do we need this model?

A: The database is the legacy of the system used, and the database has a large amount of data, can not be arbitrarily changed.

Second: Your database architecture is basically determined by the requirements analysis, do not make any changes, in the early stages of system design has built the database.

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.

Reference: http://www.cnblogs.com/lyj/archive/2008/11/10/1330542.html

Documentation and series articles

[Nhibernate] Architecture

[NHibernate] Isessionfactory Configuration

[NHibernate] Persistence class (persistent Classes)

[NHibernate] O/R Mapping Basics

[NHibernate] Collection Class (collections) mappings

[NHibernate] Association mappings

[NHibernate] Parent/child

[NHibernate] Cache (nhibernate.caches)

[NHibernate] NHibernate.Tool.hbm2net

[NHibernate] Nullables

[NHibernate] NHibernate How to map image fields in SQL Server

[NHibernate] Basic configuration and testing

[NHibernate] HQL Query

[NHibernate] Conditional query criteria

[NHibernate] additions and deletions to change the operation

[NHibernate] Transactions

[NHibernate] concurrency control

dependent objects of [NHibernate] components

[NHibernate] One-to-many relationship (cascade Delete, cascade Add)

[NHibernate] One-to-many relationship (associative query)

[NHibernate] Many-to-many relationships (associative queries)

[NHibernate] Lazy loading

[NHibernate] Load now

[NHibernate] View processing

[NHibernate] N+1 Select query problem analysis

[NHibernate] Use of stored procedures (i)

[NHibernate] Use of stored procedures (ii)

[NHibernate] Use of stored procedures (iii)

Use of code generators

An open source code generator Mygeneration has been described in the previous article, so this article continues to show you how to use the open source Mygeneration code generation tool to generate mapping files and persisted classes based on the database schema. (assuming you already have Mygeneration installed), because the code generator is similar to the procedure, here is a reference to Li Yongjing's article in the diagram to explain, and do not consider the differences between the version, know the steps to use it.

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

2, the "Online Template Library" window appears, in the "Online Template Gallery" provides a variety of templates to find the NHibernate node:

3, expand the NHibernate node, find "NHibernate sharp 1.4 [sharpm64]" template, right click on "Save". This template is automatically saved to the local templates folder.

4, click the "Template Browser" window of the first "Refresh" button, this template is visible in the "template browser", expand the NHibernate node, right-click "Execute" NHibernate Sharp1.4 template.

5, 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.

6, click on the "CustomerId" column, the right side shows the table, and the status bar shows the "CustomerId" column of the properties: int type, primary key. You can also explore yourself:

7, click the "View" tab, set CustomerID as the primary key.

8, click on the "Action" tab, we specifically 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.

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

10, open with VS2008, VS2008 automatic upgrade gendomainmodel.csproj solution file, solution project file as follows

11, 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 to implement the database CRUD operations, of course, before using to understand the project's Class library architecture, In addition, 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"/>.

Summarize

About the code generator part of the said here, after all, is a tool, in the project I use a soft code generator, the tool is an auxiliary role, less tap code, which uses the handy with which!

Reference: http://www.cnblogs.com/lyj/archive/2008/11/10/1330542.html

Use of the [NHibernate] code generator

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.