博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

第一个NHibernate程序

Posted on 2008-10-28 18:47  a-peng  阅读(696)  评论(0编辑  收藏  举报

在这里先谢谢园里的永京:www.cnblogs.com/lyj 写了很好的一系列关于NHibernate的文章。

(一)、准备工作
1、下载NHibernate最新版
主页:http://sourceforge.net/projects/nhibernate/

2、下载NUnit
主页:http://www.NUnit.org

3、下载TestDriven.NET(Visual Studio中的一个插件,非常方便,可以使用右键进行测试)
主页:http://www.testdriven.net/

(二)、第一个NHibernate程序(环境VS2005,MS SQL 2000)
1、数据库
新建数据库Test

新建数据表Animals
字段 AnimalID int(4) 主键,递增
字段 AnimalType varchar(50)  不为空

已有数据
image 

2、解决方案说明
 image
解决方案如上图所示。


ORM (Object对象  Mapper映射文件  Relational关系数据库)
image
配置文件AnimalInfo.hbm.xml 将 实体类AnimalInfo 与 数据表Animals 映射起来

Model为实体层
Entities中存放实体类,Mappings中存放实体类对应的映射文件。
DAL为数据库访问层
DAL.Test为数据库访问层的测试

3、Model实体层下代码

AnimalInfo.cs代码如下:

using System;

namespace Model.Entities
{
    
public class AnimalInfo
    
{
        
private Int32 m_animalID;
        
private String m_animalType;

        
public Int32 AnimalID
        
{
            
get return m_animalID; }
            
set { m_animalID = value; }
        }


        
public String AnimalType
        
{
            
get return m_animalType; }
            
set { m_animalType = value; }
        }

    }

}
定义了一个实体类:AnimalInfo

AnimalInfo.hbm.xml代码如下:(NHibernate的实体类映射文件都是以.hbm.xml为扩展名hbm意思为hibernate mapping)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<class name ="Model.Entities.AnimalInfo,Model" table="Animals" lazy="false" >
    
<id name="AnimalID" column="AnimalID" type="Int32">
      
<generator class ="native" />
    
</id>
    
<property name="AnimalType" column ="AnimalType" type="String" length="50" not-null="true"/>
  
</class>
</hibernate-mapping>

配置文件属性必须设为嵌入资源。(否则会出现错误)

image

配置文件中的含义就不详细说明了,文档中都有介绍,所以小菜给出文档的地址。
英文版:http://nhforge.org/doc/nh/en/
中文版:http://www.cnblogs.com/renrenqq/archive/2005/11/24/283757.html (园里一朋友翻译的)

4、DAL数据库访问层下代码
SessionManager.cs代码如下:

using System;

using NHibernate;
using NHibernate.Cfg;

namespace DAL
{
    
public class SessionManager
    
{
        
private static Configuration m_configuration;
        
private static ISessionFactory m_sessionFactory;
        
private static object m_lockHelper = new object();

        
private SessionManager()
        
{
        }


        
public static ISession GetSession()
        
{
            
if (m_sessionFactory == null)
            
{
                
lock (m_lockHelper)
                
{
                    
if (m_sessionFactory == null)
                    
{
                        m_configuration 
= new Configuration().Configure();
                        m_sessionFactory 
= m_configuration.BuildSessionFactory();
                    }

                }

            }

            
return m_sessionFactory.OpenSession();
        }

    }

}
使用单件模式构造SessionFactory

Animals.cs代码如下:
using System;

using Model.Entities;
using NHibernate;

namespace DAL
{
    
public class Animal
    
{
        
private ISession m_session;

        
public Animal(ISession session)
        
{
            m_session 
= session;
        }


        
public AnimalInfo GetAnimalByID(Int32 animalID)
        
{
            
return m_session.Get<AnimalInfo>(animalID);
        }

    }

}

5、DAL.Test数据库访问层测试
using System;

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

namespace DAL.Test
{
    [TestFixture]
    
public class AnimalFixture
    
{
        
private ISession m_session;
        
private Animal m_animal;

        [SetUp]
        
public void SetUp()
        
{
            m_session 
= SessionManager.GetSession();
            m_animal 
= new Animal(m_session);
        }

        
        [Test]
        
public void GetAnimalByIDTest()
        
{
            AnimalInfo animalInfo 
= m_animal.GetAnimalByID(1);
            Int32 animalID 
= animalInfo.AnimalID;
            Assert.AreEqual(
1, animalID);
        }

    }

}


hibernate.cfg.xml代码如下:(NHibernate的配置文件)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  
<session-factory name="NHibernate.Test">
    
<!-- properties -->
    
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    
<property name="connection.connection_string">Server=localhost;initial catalog=test;Integrated Security=SSPI</property>
    
<property name="show_sql">true</property>
    
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    
<property name="use_outer_join">true</property>
    
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    
<!-- mapping files -->
    
<mapping assembly="Model" />
  
</session-factory>
</hibernate-configuration>
设置show_sql为true测试时可以显示运行的sql语句。

配置文件属性复制到输出目录设置为始终复制。(否则提示错误)
image 

还有一个问题需要说明一下:
在NHibernate中可以找到NHibernate.dll,log4net.dll,Iesi.Collections.dll等
NHibernate.dll是核心组件,它依赖于后面两个组件,所以都需要引用。(否则提示错误)


6、测试
安装了TestDriven.NET

选中DAL.Test项目图标,右键。
如下图所示:
image 
我们可以直接选择Run Test直接运行测试,也可以选择NUnit 2.4运行测试,非常方便,这就是TestDriven.NET带来的功能。

选择Run Test
在Visual Studio 2005中显示
image
选择NUnit2.4
image
最爱的绿条。

可见,TestDriven.NET丰常方便。

我们指定具体的测试方法:GetAnimalByIDTest执行Run Test
image 

在Visual Studio 2005中显示了对应的Sql语句。
image 
代码下载:https://files.cnblogs.com/a-peng/ORM_Chapter01.rar