转自:http://www.cnblogs.com/mack/archive/2004/09/01/38636.aspx
出于兴趣以及学习.NET得目的, 花了两天仅看了很小一部分代码:一来源代码注释并不丰富,二来对于Hibernate/NHibernate的使用也很不熟悉,三来有些知识点还不熟悉.
准备工作如下:
1. NHibernate
2. NUnit
3. NHibernate配置文件 monitoring.dll.config 如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
configSections
>
<
section
name
="nhibernate"
type
= "System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</
configSections
>
<
nhibernate
>
<
add
key
="hibernate.show_sql"
value
="false"
/>
<!--
设置是否输出SQL语句到Console
-->
<
add
key
="hibernate.connection.provider"
value
="NHibernate.Connection.DriverConnectionProvider"
/>
<
add
key
="hibernate.dialect"
value
="NHibernate.Dialect.MsSql2000Dialect"
/>
<!--
设置使用SQL Server的方言,毕竟不同DB的SQL有或多或少的区别
-->
<
add
key
="hibernate.connection.driver_class"
value
="NHibernate.Driver.SqlClientDriver"
/>
<!--
设置使用的DB驱动
-->
<
add
key
= "hibernate.connection.connection_string"
value
="Server=zephyr;initial catalog= argus;UserID=sa;Password=zephyr;Min Pool Size=2"
/>
<!--
设置连接串
-->
</
nhibernate
>
</
configuration
>
可以看出以上是一个标准得Config文件,一般由System.Configuration.ConfigurationSettings.GetConfig方法来读取.
蓝色部分才是真正配置NH的地方, 例子中我配置它使用SQL Server, 那些Key/Value的含义很好明白.
值得注意得是,配置文件得文件名很重要,通常对于一个EXE得Assembly来说,是AssemblyName.Config,不过对于Dll Assembly来,对应的配置文件为AssemblyName.dll.config 例如:
MyAssy.exe -> MyAssy.config
MyAssy.dll -> MyAssy.dll.config
我打算在我的monitoring.dll,一个用来监视性能东东中使用NH来持久化数据. 该类库包含了一个TestCase,由NUnit来调用
4.将要被持久化的对象, 即Business Object(BO)
using
System;
namespace
Argus.Monitoring

{
public class Monitor

{
//dbID & DBID 是NH必须要求的主键
private int dbID;

public int DBID
{set
{dbID=value;}get
{return dbID;}}

public string MonitorType
{get
{return "dummy monitor";}}

public double Value
{get
{return 12.34;}

public string Category
{get
{return "this is category";}}

public string Name
{get
{return "this is name";}}

public string Instance
{get
{return "this is instance";}}

public string Computer
{get
{return "this is computer";}}
}
}
这是一个被极度简化的类,省略了Member Method,甚至Property的set方法,因为我打算先试试Insert功能,然后再尝试Load功能
5. 写一个该BO对应的最简单的映射文件
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-mapping
xmlns
="urn:nhibernate-mapping-2.0"
>
<!--
指明BO的全名和所在Assembly的名字以及在数据库中对应的表名
-->
<
class
name
="Argus.Monitoring.Monitor,monitoring"
table
= "record"
>
<
id
name
="DBID"
type
="Int32"
>
<
generator
class
="identity"
/>
<!--
在数据库表中 ID列设成自动加一的主键
-->
</
id
>
<!--
若不设column属性,则默认BO中属性名称和表中字段名一致,若不指明type,则通过反射BO的属性来得到列的数据类型
-->
<
property
name
="Computer"
type
="String(50)"
/>
<
property
name
="Category"
type
="String(50)"
/>
<
property
name
="Name"
column
="counter"
type
="String(50)"
/>
<
property
name
="Instance"
type
="String(50)"
/>
<!--
此处BO的Value属性被映射到data列
-->
<
property
name
="Value"
column
="data"
type
="Double"
/>
<
property
name
="MonitorType"
type
="String(50)"
/>
</
class
>
</
hibernate-mapping
>
6. 最后一步 (好累啊~~~), TestCase:
using
System;

using
NUnit.Framework;

using
NHibernate;

using
Argus.Monitoring;


namespace
Test.Monitoring


{


[TestFixture]

public class MonitoringTest


{

[Test] public void NHibernateTest ()


{

Argus.Monitoring.Monitor m=new Argus.Monitoring.Monitor ();

NHibernate.Cfg.Configuration cfg=new NHibernate.Cfg.Configuration ();

cfg.AddXmlFile ("Argus.Monitoring.Monitor.hbm.xml");

ISession session= cfg.BuildSessionFactory ().OpenSession();

session.Save (m);

session.Close ();

}

}

}