I applied the subsequent version of nhibbench 0.5 in a small project and saw no major changes. I will discuss it with version 0.5 here.
- Project Experience
- Personal feeling
- Some questions
Project Experience
1. Set NHibernate in a unified manner through the Factory class
Using System;
Using freedom. data;
Using nhib.pdf. Cfg;
Using nhib.pdf. Dialect;
Namespace freedom. dal
{
/// <Summary>
/// Summary of Factory.
/// </Summary>
Public class Factory
{
Private static NHibernate. Cfg. Configuration cfg;
Private static nhib.pdf. ISessionFactory Isession;
Public Factory ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Initialization
/// </Summary>
Public static void BuildSessionFactory ()
{
Cfg = new Configuration ();
Cfg. AddAssembly ("freedom ");
Isession = cfg. BuildSessionFactory ();
}
/// <Summary>
/// Open the session
/// </Summary>
/// <Param name = "datatype"> </param>
Public static NHibernate. ISession SesionOpen ()
{
If (Isession = null)
{
BuildSessionFactory ();
}
Return Isession. OpenSession ();
}
}
}
2. Use the EntityControl class to unify all operations.
Using System;
Using System. Collections;
Using nhib.pdf;
Using nhib.pdf. Cfg;
Using nhib.pdf. Hql;
Using nhib.pdf. Expression;
Namespace freedom. dal
{
/// <Summary>
/// Abstract description of EntityControl.
/// </Summary>
Public class EntityControl
{
Public EntityControl ()
{
//
// TODO: add the constructor logic here
//
}
// Add an object
Public bool addEntity (Object entity)
{
Bool isok = true;
ISession s = Factory. SesionOpen ();
ITransaction t = s. BeginTransaction ();
Try
{
S. Save (entity );
T. Commit ();
}
Catch (Exception e)
{
T. Rollback ();
Isok = false;
Throw e;
}
Finally
{
S. Close ();
}
Return isok;
}
// Update an object
Public bool updateEnity (Object entity, Object key)
{
Bool isok = true;
ISession s = Factory. SesionOpen ();
ITransaction t = s. BeginTransaction ();
Try
{
S. Update (entity, key );
T. Commit ();
}
Catch (Exception e)
{
T. Rollback ();
Throw e;
Isok = false;
}
Finally
{
S. Close ();
}
Return isok;
}
// Delete an object
Public void DelEntity (object entity)
{
ISession s = Factory. SesionOpen ();
ITransaction t = s. BeginTransaction ();
Try
{
S. Delete (entity );
T. Commit ();
}
Catch (Exception e)
{
T. Rollback ();
Throw e;
}
Finally
{
S. Close ();
}
}
Public string getdeptid (string dwdm)
{
IList lst;
String ifquery = "(";
String
Query = "select d. deptid from department as d where dwdm like
'"+ Dwdm +" % '";
ISession s = Factory. SesionOpen ();
Lst = s. Find (query );
Ifquery = "deptid in (";
For (int I = 0; I <lst. Count; I ++)
{
Ifquery + = lst [I]. ToString ();
If (I <lst. Count-1)
{
Ifquery + = ",";
}
}
Ifquery + = ")";
S. Close ();
Return ifquery;
}
Public IList GetEntities (String query)
{
IList lst;
ISession s = Factory. SesionOpen ();
Lst = s. Find (query );
S. Close ();
Return lst;
}
Public IList
FindEntities (System. Type thetype, string propertyName, string
Pvalue)
{
ISession s = Factory. SesionOpen ();
Expression ex = Expression. Eq (propertyName, Pvalue );
IList retList = s. CreateCriteria (thetype). Add (ex). List ();
S. Close ();
Return retList;
}
Public Object GetEntity (Type theType, Object id)
{
Object obj;
ISession s = Factory. SesionOpen ();
Obj = s. Load (theType, id );
S. Close ();
Return obj;
}
Public IList GetEntities (System. Type thetype)
{
ISession s = Factory. SesionOpen ();
IList objectList = s. CreateCriteria (thetype). List ();
Return objectList;
}
}
}
3. Data Table object ing Class
Here I want to raise a question, because the Person table is related to the Department table. Here I do not use the one-to-one class or multiple-to-one of nhib.pdf, it is implemented through a custom method. I don't know, okay?
Using System;
Using freedom. dal;
Namespace freedom. data
{
Public class Person
{
Public Person ()
{
}
Private System. String _ mz;
Public System. String mz
{
Get {return this. _ mz ;}
Set {this. _ mz = value ;}
}
Private System. String _ address;
Public System. String address
{
Get {return _ address ;}
Set {_ address = value ;}
}
Private System. DateTime _ birthday;
Public System. DateTime birthday
{
Get {return _ birthday ;}
Set {_ birthday = value ;}
}
Public System. String sex
{
Get {return _ sex ;}
Set {_ sex = value ;}
}
Private System. String _ notes;
Public System. String notes
{
Get {return _ notes ;}
Set {_ notes = value ;}
}
Private System. String _ tell;
Public System. String tell
{
Get {return _ tell ;}
Set {_ tell = value ;}
}
Private System. String _ name;
Public System. String name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Private System. String _ hfzk;
Public System. String hfzk
{
Get {return _ hfzk ;}
Set {_ hfzk = value ;}
}
Private System. String _ cardid;
Public System. String cardid
{
Get {return _ cardid ;}
Set {_ cardid = value ;}
}
Private System. Int32 _ deptid;
Public System. Int32 deptid
{
Set {this. _ deptid = value ;}
Get {return this. _ deptid ;}
}
Private department _ depart;
Public department depart
{
Get
{
If (this. _ depart! = Null)
{
Return this. _ depart;
}
Else
{
If (_ deptid! = 0)
{
This. _ depart =
(Department) (new
EntityControl (). GetEntity (typeof (department), this. _ deptid ));
}
Else
{
This. _ depart
= Null;
}
Return this. _ depart;
}
}
}
Public string dname
{
Get
{
If (this. depart! = Null)
{
Return this. depart. dname;
}
Else
{
Return String. Empty;
}
}
}
}
}
Personal feelings and questions:
Does nhib.pdf have frequent access to the database and SQL statements are not very concise (known through the event probe)? Is it a little inappropriate for a system that requires an operation of tens of thousands of records at a time?