Learn together and use the nhib.pdf development example.

Source: Internet
Author: User
Turn: initial development of a three-tier Application Based on nhib.pdf

This document does not describe how to use nhib.pdf in detail, but uses a simple example to demonstrate the three-tier structure application based on nhib.pdf.ProgramDevelopment process. DDL has been written in Chinese for the relevant documentation of nhib.pdf, but since the English documentation is not complete, it is not complete in Chinese. Bodhi talked about the difficulties in learning nhibana in an essay "The Road to nhibana learning". I also hope that you can share your experiences and experiences in using nhibana and share them with you. In addition, I was just getting started with nhigoal. If you have any errors, please give us some advice.

Step 1: Prepare a data table

Here, we use the simplest example: there is a user table, which contains the following fields: ID, name, password, email address, and last logon time.

Create Table users (

Logonid varchar (20) primary key,

Name varchar (40 ),

Password varchar (20 ),

Emailaddress varchar (40 ),

Lastlogon datetime

)

Step 2: Create a class to be persisted

Create a nhibernatewebdemo. Model Project in. NET and add the user entity class.

// User. CS

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 ;}

}

}

}

Step 3: Create a persistent ing File

The file is named user. HBM. xml and put in the same directory as user. CS. Set the file generation operation attribute to "embedded resource. In addition, the number is used as the primary key, which is input by the user. Therefore, assigned is used in the ing file.

<? XML version = "1.0" encoding = "UTF-8"?>

<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.0">

<Class name = "nhibernatewebdemo. model. User, nhibernatewebdemo. Model" table = "users">

<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>

 


Edit post management top
Report post
Copy a post
Add as essence
Single sticker shielding
Post comment
Use Items

Mark

Level: idle
Article: 58
Credit: 568
Charm: 422
Stock: 0
Deposit: 0
Total funds: 8341
Login: 4
Registration: 2007-01-31
Status: [Normal]

· SMS · friends · MATERIALS · search · email address · Reference · reply to 2nd floor

Step 4: configure the configuration file

In the configuration file, we need to tell nhib.pdf what the database is and how to connect to the database.

<Configsections>

<Section name = "nhibction" type =" system. configuration. namevaluesectionhandler, system, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089 "/>

</Configsections>

<Nhib.pdf>

<Add key = "hibernate. Connection. provider"

Value = "nhib.pdf. Connection. driverconnectionprovider"/>

<Add key = "hibernate. dialect"

Value = "nhibect. dialect. mssql2000dialect"/>

<Add key = "hibernate. Connection. driver_class"

Value = "nhib.pdf. Driver. sqlclientdriver"/>

<Add key = "hibernate. Connection. connection_string"

Value = "Server =.; uid = sa; Pwd = sa; database = test"/>

</Nhib.pdf>

Step 5: Write Public classes at the data access layer

Here, two common classes are written for session creation and entity Operations respectively. In these two classes, the single-piece mode is used to limit the creation of sessions. In order to be unrelated to a specific application, the Assembly name is passed to the opensession () method as a parameter. You can put these two classes separately under a project named common, and put them in the Dal layer first. These two classes are just a form of personal writing. You can write them on your own.

// Sessionfactory. CS

Using system;

Using system. reflection;

Using system. Data;

Using nhib.pdf;

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.