NHibernate源代码浅读 1

NHibernate源代码浅读 1

曾经了解过Hibernate, 印象很深,是个很不错得O/R Mapping FW. 在 _ http://nhibernate.sourceforge.net/ _ 上有个从Java移植过来得.NET版本--NHibernate(以下称NH),不过目前还处于PreAlpha Build 2阶段.

出于兴趣以及学习.NET得目的, 花了两天仅看了很小一部分代码:一来源代码注释并不丰富,二来对于Hibernate/NHibernate的使用也很不熟悉,三来有些知识点还不熟悉.

准备工作如下:
1. NHibernate PreAlpha Build 2
2. NUnit
3. NHibernate配置文件 monitoring.dll.config 如下:

_

 1<configuration>
 2<configsections>
 3<section name="nhibernate" type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"></section>
 4</configsections>
 5<nhibernate>
 6<add key="hibernate.show_sql" value="false"></add><!-- 设置是否输出SQL语句到Console -->
 7<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"></add>
 8<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"></add><!--设置使用SQL Server的方言,毕竟不同DB的SQL有或多或少的区别 -->
 9<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"></add> <!-- 设置使用的DB驱动-->
10<add key="hibernate.connection.connection_string" value="Server=zephyr;initial catalog= argus;UserID=sa;Password=zephyr;Min Pool Size=2"></add> <!-- 设置连接串 -->
11</nhibernate>
12</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对应的最简单的映射文件

_

 1<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
 2<!-- 指明BO的全名和所在Assembly的名字以及在数据库中对应的表名-->
 3<class name="Argus.Monitoring.Monitor,monitoring" table="record">
 4<id name="DBID" type="Int32">
 5<generator class="identity"></generator> <!-- 在数据库表中 ID列设成自动加一的主键-->
 6</id>
 7<!--若不设column属性,则默认BO中属性名称和表中字段名一致,若不指明type,则通过反射BO的属性来得到列的数据类型-->
 8<property name="Computer" type="String(50)"></property>
 9<property name="Category" type="String(50)"></property>
10<property column="counter" name="Name" type="String(50)"></property>
11<property name="Instance" type="String(50)"></property>
12<!--此处BO的Value属性被映射到data列-->
13<property column="data" name="Value" type="Double"></property>
14<property name="MonitorType" type="String(50)"></property>
15</class>
16</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 ( );

}

}

}

(嘿嘿, 电影Down刚完, 休息一下了 明天将随着Test Case的每一步,看看NH中到底发生了什么)

Published At
Categories with Web编程
Tagged with
comments powered by Disqus