Nhibernate Extension: fluentnhibernate basic tutorial

Source: Internet
Author: User

Http://www.cyqdata.com/nhibernate/article-detail-2523

 

After a large project and a small project, I used FLUENT nhib.pdf in the devtext project. Today I will introduce the fluent nhib.pdf, and what are the benefits and convenience of using it!

The following is an introduction:

I. Introduction to fluent nhib.pdf

Fluent nhib provides a method that eliminates the need to write the standard nhing file (. HBM. XML) of Nhibernate. Instead, you can use C # To write all your ing files. This facilitates ourCodeRefactoring provides easy coding and streamlined project code.

It also includes the following tools:

Fluent Nhibernate is an extension of Nhibernate's core code and fully compatible with nhibernate2.x.

 

Ii. What are the advantages of using FLUENT nhib.pdf?

Not to mention Nhibernate. Everyone knows that it is a good ORM tool and its mapping is defined in XML format. Each class has a Mapping File mapped to the corresponding table of the database. Fluent nhib.pdf cancels these XML files.

Why should we replace XML files?

A. XML is not compiled in real time. When an error occurs in your xml configuration file, you can only see the error at runtime.

B. XML is very cumbersome. It is true that the XML node is very simple in the configuration file of nhib.pdf, but it still cannot conceal the complexity of the XML file.

C. Repeated attribute settings in the ing file. For example, in XML, we need to set that each string type field cannot be blank, the length is greater than 1000, and the int type must have a default value of-1, in this way, you will find a lot of repetitive work in the final xml configuration file.

How can fluent nhib.pdf overcome these defects?

Fluent nhib.pdf converts these configurations into files into C # code, so that your mapping can be completed directly at compilation.

 

The following is a comparison between the traditional hbm xml Mapping File and fluent nhib.pdf.

 

Fluent nhib.pdf: http://github.com/jagregory/fluent-nhibernate

 

3. A simple example of fluent nhib.pdf

The previous section briefly explains why fluent nhib.pdf is used. Now let's simulate a scenario to see how to use fluent nhib.pdf in the project.

In our scenario, there are multiple-to-many relationships between products, such as "employee" and "store and product.

 

The object class is as follows:

Employee:

Product:

Store:

 

If we use nhib.pdf, we can only configure the painful XML file. However, we do not need to write the configuration file when we use fluent nhib.pdf. We only need to write C # code in a simple way.

To be honest, this is a special mapping method in ctp4.

 

Let's take a look at the Mapping File of employee:

Obviously, the map method is equivalent to the property of the XML configuration file, and the reference method is equivalent to just-to-one.

 

The product mapping is as follows:

 

The hasmanytoet is equivalent to the upload-to-transfer in nhibernaet.

 

4. The next step is to create the database and sessionfactory:

Create a database:

Sessionfacotry:

The database connection here is directly input to server, DB, username, PWD, etc. fluentnhibernate also supports other database connection forms:

 

Finally, add a record:

 

RunProgramThe tables in the database are automatically created and the data is successfully added.

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.