Dashboard of the donet data persistence layer framework-nhib.pdf

Source: Internet
Author: User

Nhib.pdf is. NET environment object/Relational Database ing tool. the term object/relational mapping (ORM) represents a technology used to map the Objects represented by the object model to the SQL-based relational model data structure.

The above may be very abstract. I will explain why it saves time and effort to use nhib.pdf.
If nhib.pdf is not used and a piece of data is inserted into the database, we may need to write an SQL statement to understand the structure of the data table, and then write it like this:
Insert into Table Name ([Field 1], [Field 2]) values ('"& page parameter 1 &"', '"& page parameter 2 &"')
The correctness of the statement must be considered during writing. This is a simple SQL statement. If it is more complicated, it is very troublesome for programmers.

With nhib.pdf, we can ignore this because nhib.pdf encapsulates database addition, deletion, query, and modification. We only need to provide object attributes to it, the rest is handled by nhib.pdf, which greatly reduces the burden on programmers.

Let's take a look at the following steps:
1. Download The Nhibernate class library online, because the DLL file of Nhibernate must be referenced during the call. You can download it on its official homepage.
Http://www.nhibernate.org/.the main core of dllis as follows:
Nhibctions. dll, log4net. dll, iesi. Collections. dll, Castle. dynamicproxy. dll

2. Create a database logging and a simple user table. The fields are as follows:
Create Table users (
Logonid varchar (20) primary key,
Name varchar (40 ),
Password varchar (20 ),
Emailaddress varchar (40 ),
Lastlogon datetime
)

3. open vs2005 and create a website project nhibernatedemo. Here we use a three-tier structure, model layer, data access layer (DAL), and business logic layer (BLL ). add the above DLL to the reference. otherwise, an error is reported during compilation.
Create a user model:
Using system;
Namespace nhibernatewebdemo. Model
{
Public class user
{
Public user ()
{
}
Private string ID;
Private string username;
Private string password;
Private string emailaddress;
Private datetime lastlogon;
Public String ID
{
Get {return ID ;}
Set {id = value ;}
}

Public String Username
{
Get {return username ;}
Set {username = value ;}
}

Public String Password
{
Get {return password ;}
Set {Password = value ;}
}

Public String emailaddress
{
Get {return emailaddress ;}
Set {emailaddress = value ;}
}

Public datetime lastlogon
{
Get {return lastlogon ;}
Set {lastlogon = value ;}
}
}
}

4. Construct an XML file that allows Nhibernate to know how to complete ORM ing
This XML must be handwritten and can be generated using professional tools. Here I recommend codesmith professional 4.1
<? XML version = "1.0" encoding = "UTF-8"?>
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2">
<Class name = "nhibernatewebdemo. model. User, nhibernatewebdemo. Model" table = "users" lazy = "false">

<ID name = "ID" column = "logonid" type = "string" length = "20">
<Generator class = "assigned"/>
</ID>

<Property name = "username" column = "name" type = "string" length = "40"/>
<Property name = "password" type = "string" length = "20"/>
<Property name = "emailaddress" type = "string" length = "40"/>
<Property name = "lastlogon" type = "datetime"/>
</Class>
</Hibernate-mapping>

5. Create a data access layer. This layer is complex. First, we need to provide a Nhibernate data abstraction factory. The role of this layer is to instantiate the Nhibernate object as needed, so that we can easily call methods in Nhibernate and provide some code:
Public static isession opensession (string assemblyname) 'opens a session, just as it opens a database connection first.
{
If (sessions = NULL)
{
Lock (padlock)
{
If (sessions = NULL)
{
Buildsessionfactory (assemblyname );
}
}
}
Return sessions. opensession ();
}

Private Static void buildsessionfactory (string assemblyname) 'creates a session factory and initializes the configuration.
{
CFG = new configuration ();
Cfg. addassembly (assemblyname );
Sessions = cfg. buildsessionfactory ();
}
6. The following describes how to add, delete, query, and modify the content.
If you add data, you can write the following code:
Public void addentity (Object entity)
{
Isession session = sessionfactory. opensession (_ assemblyname); 'Call the methods in the abstract factory
Itransaction transaction = session. begintransaction (); 'transaction rollback
Try
{
Session. Save (entity); 'here is the Save method, which is very simple. It saves data and does not need to write SQL statements. It is very strong.
Transaction. Commit ();
}
Catch (exception ex)
{
Transaction. rollback ();
Throw ex;
}
Finally
{
Session. Close ();
}
}
7. Call this method at the business logic layer.
Using system;
Using system. collections;
Using nhibernatewebdemo. Dal;
Using nhibernatewebdemo. model;
Namespace nhibernatewebdemo. BLL
{
Public class userbll
{
Public void adduser (User user)
{
Userdal Dal = new userdal ();
Dal. adduser (User );
}
}
}
This example is a simple example. We can see that nhib.pdf reduces the number of repetitive work done by programmers. you do not need to consider SQL statements to execute various data operations. However, nhib.pdf is not very good at multi-Table Association. Third-party plug-ins need to be used for further research.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ice241018/archive/2009/04/22/4101791.aspx

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.