看到博客園有很多人用Spring.Net和NHibernate這兩個框架,所以自己也想學習一下,這是我寫的一個關於NHibernate和Spring.Net結合起來的小例子,比較簡單,只實現了一個簡單的增加信息的功能。不知道結合的合不合理,希望大家給予批評。
總體思路為:
1、編寫實體類Person和映射文件Person.hbm.xml
2、使用NHibernate生成表T_Person
3、編寫接口IPersonDao,用PersonDao類實現該接口
4、使用Spring.Net實現增加信息的功能
5、測試工具是用的Resharper里自帶的測試工具。
該例子的框架如下:

實現步驟:
1、新建解決方案SpringNet_Lesson1,添加lib文件夾,里面有
這幾個dll。
2、添加Domain類庫項目,並編寫Person類和映射文件,代碼如下:
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Domain
{
public class Person
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
Person.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain">
<class name="Person" table="T_Person" lazy="true" >
<id name="Id" type="Guid" column="PersonID">
<generator class="assigned"/>
</id>
<property name="Name" type="string">
<column name="Name" length="50"/>
</property>
</class>
</hibernate-mapping>
將Person.hbm.xml文件的屬性設置為嵌入的資源,

3、添加類庫項目Dao,編寫IPersonDao接口和PersonDao類:
IPersonDao.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
namespace Dao
{
public interface IPersonDao
{
object Save(Person person);
}
}
PersonDao.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using NHibernate;
using NHibernate.Cfg;
namespace Dao
{
public class PersonDao : IPersonDao
{
private ISessionFactory sessionFactory;
public PersonDao()
{
var cfg = new Configuration().Configure("Config/hibernate.cfg.xml");
sessionFactory = cfg.BuildSessionFactory();
}
public object Save(Person person)
{
using (ISession session = sessionFactory.OpenSession())
{
var id = session.Save(person);
session.Flush();
return id;
}
}
}
}
4、添加類庫Test,用於測試。
在類庫下有一個Config文件夾,里面有這么一個文件hibernate.cfg.xml,包含了配置數據庫的一些相關信息
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="Domain">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
server=(local);database=NHibernateDemo;uid=sa;pwd=123456;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">60</property>
<property name="hbm2ddl.auto">update</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="Domain"/>
</session-factory>
</hibernate-configuration>
添加app.config文件,在這個文件里包含了需要注入的對象,這里只注入了PersonDao:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<description>一個簡單的控制反轉例子</description>
<object id="PersonDao" type="Dao.PersonDao, Dao" />
</objects>
</spring>
</configuration>
再編寫PersonInit.cs類,用於生成表結構
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
namespace Test
{
[TestFixture]
public class PersonInit
{
/// <summary>
/// In order to generate table T_Person in database NHibernateDemo.
/// </summary>
[Test]
public void Init()
{
var cfg = new Configuration().Configure("Config/hibernate.cfg.xml");
using (ISessionFactory sessionFactory = cfg.BuildSessionFactory())
{
}
}
}
}
生成的表結構T_Person為:

接着編寫DomainTest.cs類,用於添加數據
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dao;
using Domain;
using NHibernate;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
namespace Test
{
[TestFixture]
class DomainTest
{
private IPersonDao personDao;
//[SetUp]
//public void Init()
//{
// personDao = DaoFactory.DataAccess.CreatePersonDao();
//}
[Test]
public void Save()
{
var person = new Person
{
Id = Guid.NewGuid(),
Name = "李四"
};
IApplicationContext context = ContextRegistry.GetContext();
personDao = context.GetObject("PersonDao") as IPersonDao;
if (personDao != null)
{
var id = personDao.Save(person);
Assert.NotNull(id);
}
}
}
}
運行這個測試,可以看到數據已添加進去。

