iexpression 条件表达式描述,用于处理查询过程中的条件;从些接口实现的条件表达式有:=、>、<>、like、in等;组件还有很多对象进行内部处理的:类的映射信息,主要和忝相就应的sql语句和命令对象;缓存类用于缓存操作命令对象、命令对持久化接口等。
类和数据库的关系映射 实体类和数据库表的关联采用了xml文件描述,相应对nhibernate业说比较简单. 以下是描述employees类和employees表对映象关系。 类文件
using system;
namespace northwind.entitys
{
/// <summary>
/// employees
/// </summary>
public class employees
{
public employees()
{
//
// todo: 在此处添加构造函数逻辑
//
}
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;
}
}
………………..
……………….. 映射关系的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>
组件的使用 当类和映射文件建立以后,就通过组件对类的操作来实现数据操作。为了实现动态配置,组件配置的方式和nhibernate是一样的,只是配置节有所不同。
<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);
}
组件为了提高效率,增加了缓存机制。可以通过配置文件制定对插入数据、修改数据的命令对象进行缓存的数量。当组件需要调用对象的插入命令时就会从缓存中找,当找不到的情况下才创建相关命令对象,这样会减少创建所带来的性能问题;同样缓存会占用相应的内存资源。组件现在还决少查询命令对象、数据集、实体对象的缓存。实体对象缓存比较复杂,中间还存在着和数据库信息同步、频繁锁的问题;还没有想到较好的解决方法,暂时没有打算把实体缓存集成到组件中。
新闻热点
疑难解答
图片精选