ORM Weapon: NHibernate (i) Introduction

Source: Internet
Author: User

Brief introduction:

ORM (Object relational Mapping) Object Relational mapping is a technique to solve the mismatch between object-oriented and relational database.


Why is ORM present?

    Object-oriented features : We usually use the development language Java,. NET is object-oriented. Objects are encapsulated, and objects have properties and methods inside them.

    relational database features: The organizes our data as a table. The characteristics of the table are expressed in columns. One record in the table = an object, and the columns in the table represent the properties in the object. Between the tables is established a certain connection.

    How do I extract a record from a table and encapsulate it as an object? the previous practice is to query the records through the corresponding SQL statements, and then take each piece of data in the record out to separate, to instantiate an object, and give each property of the object, so as to achieve the purpose of encapsulating the object. This makes it possible to convert a piece of data into an object.

    put an object through a crud in the database? It is necessary to make DML statements to the database, to spell various SQL statements, to convert one form to another, and to implement the user manual code. If the project table has many structures, it is easy to make errors by repeatedly converting objects and libraries. And programmers are more suited to the encapsulation of objects than to the library's frequent switches and DML to the database, so is there any better way? That is the advent of ORM. ORM is a bridge between database and object, which describes the transformation relationship between two different forms.

working principle:

ORM automatically persists objects in a program to a relational database by using metadata that describes the mapping between the object and the database.

The ORM automatically completes the following operations: 1) The object is placed in a crud table, and finally in the library. 2) The database is transformed into an object by a DML statement, and finally comes back.  

. NET three-tier architecture, the topmost presentation layer (U-layer), the middle of the business layer (layer B), and the following data access layer (layer D). In previous projects, the D layer is usually used? DAO, DAL, is responsible for all the database dealing with the place, simple crud, through the manual encapsulation DBHelper to achieve. and the database variety, if the product to do is perfect, you need to support different kinds of database driver, and write different D-layer. The interface can be implemented by defining an interface, which is implemented by different data sources. But then the D-Layer code would be very large. A lot of different kinds of libraries, there will be a large D-layer code. So how do you save such a operation? The ORM goal is to persist and act as a layer of dealing with the database. And for different varieties of the database, will be automatically converted into corresponding statements.

  

NHibernate The code is complex, and here we just provide basic usage.

Five-part song

      1. Create a table . To convert an object to a table in a database, you naturally have to have a table for one. Therefore, the first thing to do in the database is to create a. NET class persisted table.
      2. create the Class . Create a. To be persisted. NET class. Object.
      3. Create a mapping file . Describes the relationship between objects and libraries and tells NH how to persist the properties of these classes.
      4. Create a NH configuration file that tells NH how to connect to a database and how to connect to different kinds of databases.
      5. use the API provided by NH. Maintains relationships between objects and libraries, crud on objects, and DML against databases.


Summarize

NHibernate is the framework for implementing ORM, developed by the Java Open Source project hibernate. It provides a durable class for object-oriented development (which is what we usually call D-layer), which solves the problem of transforming objects and databases. and provides the API, realizes the object and the different kind of database mutual transformation question, and has the universality.

The study of NHibernate is only a brief introduction, the series of blog will continue to update, please look forward to O (∩_∩) o~


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.