看到博客园有很多人用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);
}
}
}
}
运行这个测试,可以看到数据已添加进去。