首页 > 编程语言 >二、第一个NHibernate程序

二、第一个NHibernate程序

时间:2022-12-08 11:05:54浏览次数:33  
标签:Customer 第一个 DAL 程序 Test using NHibernate public


一、准备

要运行第一个NHibernate程序需要两个额外的DLL和两个xsd文件,他们可以在从官网上下载的文件里找到,DLL分别是NHibernate.dll和NHibernate.ByteCode.Castle.dll,xsd文件分别是nhibernate-configuration.xsd和nhibernate-mapping.xsd,我们可以先将其复制出来放到一个单独的叫NHibernateNeedFile的文件中去。

除了DLL和XSD文件外我们还需要创建一个数据库,用“NHibernateSample”命名吧!然后向其中新增一个表

这里是整个数据库的脚本,但是现在只需要用到Customer表,其它的以后会用到

 

二、新建一个解决方案

可以将这个解决方案的名称命名为“MyNHibernateSample”

 

三、分别创建ModelDALDAL.Test三个程序集

结果如下:

 

二、第一个NHibernate程序_数据库

 

四、编写实体类(也叫持久化类)

向Model中新增一个Entities文件夹,然后在该文件夹中新增一个Customer类,代码如下:

namespace Model.Entities
{
//注意,实体类必须为public,否则无法访问
public class Customer
{
//在NHibernate的实体类中,所有的公共方法、属性和事件都必须使用virtual修饰
public virtual int CustomerId { get; set; }
public virtual string Firstname { get; set; }
public virtual string Lastname { get; set; }
}
}

 

五、编写映射文件

NHibernate需要知道怎样去加载和存储持久化类的对象。而这正是NHibernate映射文件的作用。映射文件包含了对象/关系映射所需的元数据。元数据包含持久化类的声明和属性到数据库的映射。映射文件告诉NHibernate它应该访问数据库里面的哪个表及使用表里面的哪些字段。

添加智能提示功能:要为Microsoft Visual Studio 2008添加编写NHibernate配置文件智能提示的功能。我们需将上面的NHibernateNeedFile文件夹里的nhibernate-configuration.xsd和nhibernate-mapping.xsd两个文件复制到X:/Program Files/Microsoft Visual Studio 9.0/Xml/Schemas目录。

然后,我们向Model中新增一个Mappings文件夹,然后在该文件夹中新增一个Customer.hbm.xml的文件,代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model.Entities">
<class name="Model.Entities.Customer,Model" table="Customer">
<id name="CustomerId" column="CustomerId" type="Int32" unsaved-value="0">
<generator class="native"></generator>
</id>

<property name="Firstname" column="Firstname" type="string" length="50" not-null="false"></property>
<property name="Lastname" column="Lastname" type="string" length="50" not-null="false"></property>
</class>
</hibernate-mapping>

(注意:一、xmlns="urn:nhibernate-mapping-2.2"是NHibernate的版本号,如果使用的不是NHibernate-2.0.1.GA版本,请修改为正确的版本号;二、需要将该文件的“生成操作”属性修改为“嵌入的资源”)

 

六、编写数据访问辅助类

要使用NHibernate,首先,我们要从ISessionFactory中获取一个ISession(NHibernate的工作单元)。ISessionFactory可以创建并打开新的Session。一个Session代表一个单线程的单元操作。 ISessionFactory是线程安全的,很多线程可以同时访问它。ISession不是线程安全的,它代表与数据库之间的一次操作。ISession通过ISessionFactory打开,在所有的工作完成后,需要关闭。 ISessionFactory通常是个线程安全的全局对象,只需要被实例化一次。我们可以使用GoF23中的单例(Singleton)模式在程序中创建ISessionFactory。

我们可以编写一个辅助类来处理这件事情,但是先要把Model和NHIbernate的引用添加进去,然后在DAL中新建一个名为“NHibernateHelper”的类,代码如下:

using                     //ISessionFactory和ISession需要用到
using //Configuration需要用到

namespace DAL
{
public class NHibernateHelper
{
private static ISessionFactory

public
{
sessionFactory = (new Configuration()).Configure().BuildSessionFactory();
}

public ISession
{
return
}
}
}

七、编写数据操作类

同样在DAL中新增一个NHibernateSample类,用来编写相应的CURD操作,代码如下:

using NHibernate;
using Model.Entities;

namespace DAL
{
public class NHibernateSample
{
private ISession

public NHibernateSample(ISession
{
session = _session;
}

public void Save(Customer
{
session.Save(customer);
session.Flush();
}

public Customer GetCustomerById(int
{
return session.Get<Customer>(customerId);
}
}
}

八、配置NHibernate

在DAL.Test中新增一个hibernate.cfg.xml文件,它是NHibernate的配置文件,代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test"><!--请注意此处的配置-->
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property><!--请注意此处的配置,选择对于的驱动-->
<property name="connection.connection_string">Server=.;uid=sa;pwd=;Database=NHibernateSample</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property><!--请注意此处的配置,选择对应的数据库-->
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>

    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property><!--请注意此处的配置,这里配置了IoC框架动态代理方式,分别为:Castle框架、LinFu框架、Spring.Net框架-->

<mapping assembly="Model"/><!--请注意此处的配置,是实体类所在的程序集的名称-->
</session-factory>
</hibernate-configuration>

(注意:此文件的“复制到输出目录”属性必须修改为“始终复制”)

 

九、编写测试代码

需要添加DAL、Model、NHibernate、NUnit.Framework、NHibernate.ByteCode.Castle.dll、Castle.Core.dll、Castle.DynamicProxy2.dll、Iesi.Collections.dll和log4net.dll的引用。

(注意:这里使用了TestDriven.Net测试工具,请先学习了TestDriven.Net之后再继续)

然后在DAL.Test中新增NHibernateSampleTest.cs文件,然后编写如下测试代码:

using NUnit.Framework;
using Model.Entities;
using DAL;
using NHibernate;

namespace DAL.Test
{
[TestFixture]
public class NHibernateSampleTest
{
private NHibernateSample
private NHibernateHelper sessionHelper;
private ISession

[SetUp]
public void
{
sessionHelper = new NHibernateHelper ();
session = sessionHelper.GetSession();
sample = new NHibernateSample(session);
}

[Test]
public void
{
Customer
}

[Test]
public void
{
Customer customer = new Customer();
customer.Firstname = "soldier";
customer.Lastname = "luo";
sample.Save(customer);
}

[TearDown]
public void
{
session.Close();
}
}
}

结果如下:

------ Test started: Assembly: DAL.Test.dll ------

 

NHibernate: SELECT customer0_.CustomerId as CustomerId0_0_, customer0_.Firstname as Firstname0_0_, customer0_.Lastname as Lastname0_0_ FROM Customer customer0_ WHERE customer0_.CustomerId=@p0;@p0 = 2

 

1 passed, 0 failed, 0 skipped, took 4.88 seconds (NUnit 2.5.1).

 

 

 

 

 

 

标签:Customer,第一个,DAL,程序,Test,using,NHibernate,public
From: https://blog.51cto.com/u_15906220/5920731

相关文章