iexpression 條件表達(dá)式描述,用于處理查詢過程中的條件;從些接口實(shí)現(xiàn)的條件表達(dá)式有:=、>、<>、like、in等;組件還有很多對象進(jìn)行內(nèi)部處理的:類的映射信息,主要和忝相就應(yīng)的sql語句和命令對象;緩存類用于緩存操作命令對象、命令對持久化接口等。
類和數(shù)據(jù)庫的關(guān)系映射 實(shí)體類和數(shù)據(jù)庫表的關(guān)聯(lián)采用了xml文件描述,相應(yīng)對nhibernate業(yè)說比較簡單. 以下是描述employees類和employees表對映象關(guān)系。 類文件
using system;
namespace northwind.entitys
{
/// <summary>
/// employees
/// </summary>
public class employees
{
public employees()
{
//
// todo: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public const string f_employeeid="employeeid";
private int32 memployeeid;
/// <summary>
/// [int identity]
/// </summary>
public int32 employeeid
{
get
{
return memployeeid;
}
set
{
memployeeid = value;
}
}
public const string f_lastname="lastname";
private string mlastname;
/// <summary>
/// [nvarchar]
/// </summary>
public string lastname
{
get
{
return mlastname;
}
set
{
mlastname = value;
}
}
public const string f_firstname="firstname";
private string mfirstname;
/// <summary>
/// [nvarchar]
/// </summary>
public string firstname
{
get
{
return mfirstname;
}
set
{
mfirstname = value;
}
}
………………..
……………….. 映射關(guān)系的xml文件
<?xml version="1.0" encoding="utf-8" ?>
<class name="northwind.entitys.employees,northwind.entitys" table="employees">
<id name="employeeid" column="employeeid" value="select @@identity ,false"/>
<property name="lastname" column="lastname"/>
<property name="firstname" column="firstname"/>
<property name="title" column="title"/>
<property name="titleofcourtesy" column="titleofcourtesy"/>
<property name="birthdate" column="birthdate"/>
<property name="hiredate" column="hiredate"/>
<property name="address" column="address"/>
<property name="city" column="city"/>
<property name="region" column="region"/>
<property name="postalcode" column="postalcode"/>
<property name="country" column="country"/>
<property name="homephone" column="homephone"/>
<property name="extension" column="extension"/>
<property name="photo" column="photo"/>
<property name="notes" column="notes"/>
<property name="reportsto" column="reportsto"/>
<property name="photopath" column="photopath"/>
</class>
組件的使用 當(dāng)類和映射文件建立以后,就通過組件對類的操作來實(shí)現(xiàn)數(shù)據(jù)操作。為了實(shí)現(xiàn)動態(tài)配置,組件配置的方式和nhibernate是一樣的,只是配置節(jié)有所不同。
<configsections>
<section name="dataconfig" type="hfsoft.data.dataconfigsectionhandler, hfsoft.data, version=0.9.1.0, culture=neutral, publickeytoken=null" />
</configsections>
<dataconfig>
<drivertype value="hfsoft.data.sqldriver, hfsoft.data, version=0.9.1.0, culture=neutral, publickeytoken=null"/>
<connectionstring value="data source=.;initial catalog=northwind;user id=sa;pwd=;"/>
<mappingassemblys>
<assembly value="northwind.entitys"/>
</mappingassemblys>
<cachecommands default="3"/>
</dataconfig>
添加一條雇員信息
hfsoft.data.mappingcontainer mapcontainer = hfsoft.data.mappingcontainer.configcontainer;
using(hfsoft.data.idatasession session = mapcontainer.opensession())
{
session.open();
northwind.entitys.employees emp = new northwind.entitys.employees();
emp.reportsto = 3;
emp.firstname ="fan";
emp.lastname = "henry";
emp.photo = new byte[0];
emp.hiredate = datetime.parse("
emp.birthdate = datetime.parse("
emp.city ="廣州";
emp.country ="中國";
session.save(emp);
messagebox.show(emp.employeeid.tostring());
}
獲取所有雇員信息
using(hfsoft.data.idatasession session = mapcontainer.opensession())
{
session.open();
system.collections.ilist myds = session.list(typeof(northwind.entitys.employees),null);
this.datagrid1.datasource = myds;
}
多條件查詢(雇員編號大于等于3并且小于等于10或者姓包含fan)
hfsoft.data.expression expression = new hfsoft.data.expression();
expression.add(new hfsoft.data.rteqexpression(northwind.entitys.employees.f_employeeid,3));
expression.add(new hfsoft.data.leeqexpression(northwind.entitys.employees.f_employeeid,10));
expression.add(hfsoft.data.uinttype.or,new hfsoft.data.likeexpression(northwind.entitys.employees.f_firstname,"fan%"));
using(hfsoft.data.idatasession session = mapcontainer.opensession())
{
session.open();
system.collections.ilist myds = session.list(typeof(northwind.entitys.employees),expression);
this.datagrid1.datasource = myds;
}
獲取雇員信息并修改
using(hfsoft.data.idatasession session = mapcontainer.opensession())
{
session.open();
northwind.entitys.employees emp = (northwind.entitys.employees)session.load(typeof(northwind.entitys.employees),41);
emp.firstname ="kfc";
session.update(emp);
}
組件為了提高效率,增加了緩存機(jī)制??梢酝ㄟ^配置文件制定對插入數(shù)據(jù)、修改數(shù)據(jù)的命令對象進(jìn)行緩存的數(shù)量。當(dāng)組件需要調(diào)用對象的插入命令時就會從緩存中找,當(dāng)找不到的情況下才創(chuàng)建相關(guān)命令對象,這樣會減少創(chuàng)建所帶來的性能問題;同樣緩存會占用相應(yīng)的內(nèi)存資源。組件現(xiàn)在還決少查詢命令對象、數(shù)據(jù)集、實(shí)體對象的緩存。實(shí)體對象緩存比較復(fù)雜,中間還存在著和數(shù)據(jù)庫信息同步、頻繁鎖的問題;還沒有想到較好的解決方法,暫時沒有打算把實(shí)體緩存集成到組件中。
新聞熱點(diǎn)
疑難解答
圖片精選