NHibernate abc

NHibernate abc

0.prepare
download
www.hibernet.org
关键是NHibernate.dll

基本使用顺序:
addReference => cfg 配置数据库连接 => bo/hbm => 创建SessionFactory => Session => 使用

1.在console中使用
<1>将HashCodeProvider.dll,Iesi.Collections.dll,log4net.dll,NHibernate.dll四个文件copy到project的lib中
在project中添加对这四个dll的reference

<2>newItem中增加App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
        
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,
            Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
 />
        
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    
</configSections>

    
<nhibernate>
        
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
        
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
        
<add key="hibernate.connection.connection_string" value="Server=rssvr;initial catalog=testdb;User
               ID=rmsdbadmin;Password=123456"
 />
    
</nhibernate>

    
<log4net></log4net>
</configuration>


这里省略log4net的配置,这里的配置还是非常容易理解的。和Hibernate也几乎完全一样。

当然这里要在sql_server中补上一个schema

<3>在domain-driven的情况下,理应先创建bo,再创建hbm
这个工作可以NQA代劳。
具体的内容就不介绍了,还是看书和代码来的详细,这里只是粗犷的介绍一下流程。

<4>
static void Main(string[] args) {
    Configuration cfg 
= new Configuration();
    cfg.AddAssembly(
"testHibernate");

    ISessionFactory factory 
= cfg.BuildSessionFactory();
    ISession session 
= factory.OpenSession();
    ITransaction transaction 
= session.BeginTransaction();

    Link link 
= new Link("hibernate""http://www.hibernate.org");

    
// Tell NHibernate that this object should be saved
    session.Save(link);

    
// commit all of the changes to the DB and close the ISession
    transaction.Commit();
    session.Close();
}


带入assembly进行配置,依次创建SessionFactory,Session,Transaction
再是CRUD最后Commit,Close

就基本上跑把NHibernate跑起来了。

2.在Test项目中运行NHibernate进行测试
比如说有一个ClassLibrary中有相应的bo和hbm
进而在相应的unittest进行对这些bo和nhibernate进行测试
则需要在ClassLibrary的设置中将AppConfig copy到test项目的debug目录下
这个和log4net是一样的,做了log4net这步就带过了
在Post-build event command line中写:

copy "$(ProjectDir)app.config" "$(SolutionDir)home.test\bin\Debug\home.test.dll.config"

这里的目录视具体情况而定
[TestClass]
public class LinkDAOTest {
    
private LinkDAOHibernate linkdao;
    
private Configuration cfg;
    
private ISession session;
    
private ITransaction tx;
    
    [TestInitialize()]
    
public void setUp() {
        cfg 
= new Configuration();
        cfg.AddAssembly(
"home.domain");
        session 
= cfg.BuildSessionFactory().OpenSession();
        tx 
= session.BeginTransaction();
       
        linkdao 
= new LinkDAOHibernate(session);
    }

    [TestCleanup()]
    
public void tearDown() {
        tx.Commit();
        session.Close();
    }

    [TestMethod]
    
public void testSave() {
        Link l 
= new Link("hibernate""http://www.hibernate.org");
        linkdao.makePersistent(l);
    }
}


3.在ASP.NET2.0中使用
在web.config中添加
<configSections>
    
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler,
        System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
 />
</configSections>

<nhibernate>
    
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
    
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
    
<add key="hibernate.connection.connection_string"
        value
="Server=rssvr;initial catalog=testdb;User ID=rmsdbadmin;Password=123456" />
</nhibernate>

这样就基本上把整个NHibernate的start都整理了一下
当然还有很多深入的问题要去研究
Session,Transaction,HQL,Criteria等
架构在稍后总结

在连接发生异常或是错误的时候还是需要换回ado.net来测试,这样能更方便的发现问题所在

String str = @"Server=rssvr;initial catalog=pldb;User ID=rmsdbadmin;Password=123456";
SqlConnection con 
= new SqlConnection(str);
con.Open();

这里要谢谢沈宇扬帮我debug,呵呵
posted @ 2006-02-28 19:55 nonocast 阅读(465) 评论(1) 编辑 收藏