The three-tier architecture based on nhib.pdf is divided into the following four modules:
Model layer:Create the class user to be persisted, create the persistent ing file user. HBM. XML, and set the configuration file.
Data access layerWrite a common class of the data access layer, including: Session creation (sessionfactory) and entity operation (entitycontrol) Write a data access layer (userdal)
Business logic layer (userbll)
User Interface (Web)
Advantages of the three-tier architecture based on nhib.pdf:
1. You do not need to manually write insert, update, and delete SQL scripts.
2. high maintainability and reusability
Disadvantages of the three-tier architecture based on nhib.pdf:
1. As a framework, the scalability is not strong enough (nhib.pdf is not needed)
2. The nhib.pdf query is difficult to meet the actual query requirements (Multi-table and partial attribute query) 3. Multi-data sources (SQL Server, Oracle, XML) cannot be used)
Entitycontrol code snippet:
// Sessionfactory. CS
Using nhib.pdf;
Using model;
Namespace dal
{
Class entitycontrol
{
Private Static entitycontrol entity;
Private string _ assemblyname;
Static readonly object padlock = new object ();
Public static entitycontrol createentitycontrol (string assemblyname)
{
If (entity = NULL)
{
Lock (padlock)
{
If (entity = NULL)
{
Entity = new entitycontrol ();
Entity. _ assemblyname = assemblyname;
}
}
}
Return entity;
}
Public void addentity (Object entity)
{
Isession session = sessionfactory. opensession (_ assemblyname );
Itransaction transaction = session. begintransaction ();
Try
{
Session. Save (entity );
Transaction. Commit ();
}
Catch (exception ex)
{
Transaction. rollback ();
Throw ex;
}
Finally
{
Session. Close ();
}
}
Public void updateentity (Object entity, object key)
{
Isession session = sessionfactory. opensession (_ assemblyname );
Itransaction transaction = session. begintransaction ();
Try
{
Session. Update (entity, key );
Transaction. Commit ();
}
Catch (exception ex)
{
Transaction. rollback ();
Throw ex;
}
Finally
{
Session. Close ();
}
}
Public void deleteentity (Object entity)
{
Isession session = sessionfactory. opensession (_ assemblyname );
Itransaction transaction = session. begintransaction ();
Try
{
Session. Delete (entity );
Transaction. Commit ();
}
Catch (exception ex)
{
Transaction. rollback ();
Throw ex;
}
Finally
{
Session. Close ();
}
}
}
}
Session creation code snippet:
// Sessionfactory. CS
Using nhib.pdf;
Using nhib.pdf. cfg;
Using nhib.pdf. tool. hbm2ddl;
Namespace dal
{
Public class sessionfactory
{
Public sessionfactory ()
{
}
Private Static isessionfactory sessions;
Private Static configuration CFG;
Static readonly object padlock = new object ();
Public static isession opensession (string assemblyname)
{
If (sessions = NULL)
{
Lock (padlock)
{
If (sessions = NULL)
{
Buildsessionfactory (assemblyname );
}
}
}
Return sessions. opensession ();
}
Private Static void buildsessionfactory (string assemblyname)
{
CFG = new configuration ();
Cfg. addassembly (assemblyname );
Sessions = cfg. buildsessionfactory ();
}
}
}
Sample Code of the data access layer (userdal:
// Userdal. CS
Using model;
Namespace dal
{
Public class userdal
{
Private entitycontrol control;
Public userdal ()
{
Control = entitycontrol. createentitycontrol ("model ");
}
Public void adduser (User user)
{
Control. addentity (User );
}
Public void updateuser (User user, string ID)
{
Control. updateentity (user, user. ID );
}
Public void deleteuser (User user)
{
Control. deleteentity (User );
}
}
}
Configuration File Settings:
// Web. config
<Nhib.pdf>
<Add key = "hibernate. Connection. provider" value = "nhib.pdf. Connection. driverconnectionprovider"/>
<Add key = "hibernate. dialect" value = "nhib.pdf. dialect. mssql2000dialect"/>
<Add key = "hibernate. Connection. driver_class" value = "nhib.pdf. Driver. sqlclientdriver"/>
<Add key = "hibernate. Connection. connection_string" value = "Server =.; uid = sa; Pwd = saadmin; database = login"/>
</Nhib.pdf>