The beauty of the charm of nhibernate frame

Source: Internet
Author: User
Tags map class

NHibernate belongs to one of the ORM frameworks, before we get to know nhibernate what is ORM?

the ORM Framework is designed to map class objects and relationships. In fact, it is simply a Mapping to map our entity classes and databases, creating a connection that is equivalent to adding an intermediate layer between the application and the database.  This way we only have to manipulate the object directly in the program, without caring about the tables, columns, and fields in the database.


This is like we eat, if we choose to eat at home, we need to go to the oil and salt rice dishes, self-fried, fried after their own clean up. But if we choose to eat out, we just have to go to that à la carte dinner, clap your butt and leave on the OK. Specific this dish how to do, do not good delicious we have no way. in both of these ways, eating at home is equivalent to the traditional operation of not using ORM. We need to know what to do with care. And the restaurant is the equivalent of ORM Framework mapping, for us to deal with a variety of complex operations, just to the most important part of the meal to us to do the line. The order of the dish is the ORM configuration, you just need to tell the waiter what you eat, she will be according to your needs to prepare for the dish, after the dish is good to send up on the line.


In the past, we wrote a variety of SQL statements in the D layer, and there are many approximate common patterns. Write a SQL (add-in) statement, then save the Sql,command object, parameters, and then execute the SqlCommand object. For each object, we need to repeat the code. How to avoid duplication, mapping will help us solve, it will be the SQL statements are encapsulated, we only need to configure it, we do not have to write the D-layer code, mapping is responsible for saving, deleting and reading objects.


NHibernate fully develops the core theory of ORM framework, which derives from Java's hiberanate technology. The mapping of objects and relationships is established through XML configuration. Why do we need these mappings? What good is nhibernate doing this?


Benefits: First, NHibernate use only need to manipulate the object, abandoned the previous database-centric ideas, belong to the complete object-oriented; second, there are better portability, support a variety of databases, need to replace the database or table, only need to modify the configuration file, flexibility is greatly enhanced. Finally, a persisted class is provided, which is only temporarily stored in the session, and the data is persisted in the database.


NHibernate to achieve the specific steps:


1. Creating entity Objects (UserInfo)

<pre name= "code" class= "CSharp" > public class UserInfo    {public          virtual int userid{get; set;}          Public virtual string username{get; set;}          Public virtual string userpwd{get; set;}      }  


2. Create a mapping file related to the object class UserInfo.hbm.xml

<?xml version= "1.0" encoding= "Utf-8"? >


3. Create the Consolidation profile: Hibernate.cfg.xml


  <pre name= "code" class= "HTML" > <?xml version= "1.0" encoding= "Utf-8"?> 

4. Implementing the Add User test

<pre name= "code" class= "CSharp" >private void btnAdd_Click (object sender, EventArgs e)          {              Configuration CFG = new Configuration (). Configure ("Hibernate.cfg.xml");              Isessionfactory factory = cfg. Buildsessionfactory ();              ISession session = Factory. Opensession ();              ITransaction transaction = session. BeginTransaction ();              UserInfo UserInfo = new UserInfo ();              Userinfo.userid = 1;              Userinfo.username = "NH";              USERINFO.USERPWD = "abc";               Session. Save (userInfo);  Perform user add Operation               transaction.commit (); Executes a transaction              session. Close ();  Close transaction            MessageBox.Show ("user added ok!");            


Summary:nhibernate lets us get rid of the repetitive and tedious writing of SQL statements. the automatic generation of SQL statements allows us to invest in the implementation of the business logic. NET technology has more charm waiting for us to dig, and we are exposed to only the tip of the iceberg.



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.