NHibernate3快速上手教程FluentNHibernate配置与DBHelper

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

很多学习NHibernate的新手很容易卡在配置文件这一关,正所谓万事开头难,上手后再配合官方文档就比较容易了。

网上关于配置文件的资料非常多,但由于版本的问题,许多老的教程中都没有明确指出类库的版本号,

另外许多人抱怨配置比较麻烦,本教程结合FluentNHibernate简化配置,快速上手。

下载类库、版本要求:

NHibernate 3 及以上

Iesi.Collections 1.0.1.0

FluentNHibernate 1.4.0.0

.NET Framework 3及以上

NH3已经将日志与动态代理的类库集成了,所以只需要引入前两个配置文件。

总体思路:

1、通过配置文件hibernate.cfg.xml配置数据库连接;

2、配置文件注入到FluentNHibernate;

3、并添加实体模型的映射程序集到FluentNHibernate;

hibernate.cfg.xml:

数据库Oracle10g配置文件,看到那段被注释的英文了吗?

大致意思就是其他数据库可以参考NH包里面的数据库名称对应的配置文件,然后把文件名改成hibernate.cfg.xml,放在类库的根目录。

别忘了将配置文件复制到输出目录

<?xml version="1.0" encoding="utf-8"?><!--This template was written to work with NHibernate.Test.

Copy the template to your NHibernate.TestPRoject folder and rename it in hibernate.cfg.xml and change it

for your own use before compile tests in VisualStudio.--><!--This is the System.Data.OracleClient.dll provider for Oracle from MS--><hibernate-configurationxmlns="urn:nhibernate-configuration-2.2"><session-factoryname="MyManager"><propertyname="connection.driver_class">NHibernate.Driver.OracleClientDriver</property><propertyname="connection.connection_string">User ID=user;PassWord=pwd;Data Source=oracle</property><propertyname="show_sql">true</property><propertyname="dialect">NHibernate.Dialect.Oracle10gDialect</property><propertyname="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property></session-factory></hibernate-configuration>

顺手建立一个DBHelper类,或者叫SessionBuilder,直接复制一步到位。

/*

* by:13yan

*/

publicclassSessionBuilder

{privatestaticISessionFactory sessionFactory =null;privatestaticobject_lock =newobject();#region初始化 生成SessionFactory,并配置上下文策略publicstaticvoidInstance(stringcurrentSessionContextClass)

{lock(_lock)

{

Configuration cfg=newConfiguration()

.Configure()

.SetProperty("current_session_context_class","web");

sessionFactory=Fluently.Configure(cfg)

.Mappings(m=>m.FluentMappings

.AddFromAssembly(typeof(SessionBuilder).Assembly))

.BuildSessionFactory();

}

}#endregion#regionSession在当前上下文的操作privatestaticvoidBindContext()

{lock(_lock)

{if(!CurrentSessionContext.HasBind(sessionFactory))

{

CurrentSessionContext.Bind(sessionFactory.OpenSession());

}

}

}privatestaticvoidUnBindContext()

{lock(_lock)

{if(CurrentSessionContext.HasBind(sessionFactory))

{

CurrentSessionContext.Unbind(sessionFactory);

}

}

}publicstaticvoidCloseCurrentSession()

{

UnBindContext();

}publicstaticISession GetCurrentSession()

{

BindContext();returnsessionFactory.GetCurrentSession();

}#endregion#region关闭SessionFactory(一般在应用程序结束时操作)publicstaticvoidCloseSessionFactory()

{if(!sessionFactory.IsClosed)

{

sessionFactory.Close();

}

}#endregion#region打开一个新的SessionpublicstaticISession OpenSession()

{lock(_lock)

{returnsessionFactory.OpenSession();

}

}#endregion}

使用这个类需要在应用程序启动时初始化,调用代码:SessionBuilder.Instance("web");,一般添加在Global.asax

publicclassGlobal : System.Web.Httpapplication{voidApplication_Start(objectsender, EventArgs e)

{//在应用程序启动时运行的代码SessionBuilder.Instance("web");

}

}

使用时可调用SessionBuilder.GetCurrentSession()获取上下文中的Session,以便实现自动打开关闭Session,

也可以SessionBuilder.OpenSession()得到一个普通的Session。

代码映射:

publicclassDepartment

{publicvirtualintID {get;set; }publicvirtualstringName {get;set; }

}publicclassDepartmentMap:ClassMap<Department>{publicDepartmentMap()

{

Table("SYSDEPT");

Id(p=> p.ID).GeneratedBy.Sequence("SEQ_SYSDEPT");

Map(p=>p.Name);

}

}

这个使用单实体的例子非常简单,相信差不多够上手了,

更加复杂的配置有一定的学习曲线,推荐边学边用,以前用低端一点的ORM不也单实体在用么,

NH的好处就在于简单起来可以很简单地用,完全上手后又拥有其他一般ORM无法满足的功能。

映射的重点:

实体类与数据库之间需要有一个映射,所以我们建立一个DepartmentMap映射类,它需要继承FluentNHibernate.Mapping.ClassMap<T>泛型类。

Table("SYSDEPT") 对应数据库表名;

Id映射主键,这里使用sequence,其他还有许多办法,比如GUID、自增长等。

Map映射,属性名与数据库字段相同就不需要另外声明了。

测试一下,看看我们是否成功了。

ISession session =SessionBuilder.GetCurrentSession();

session.BeginTransaction();

Department obj= session.Load<Department>(102);

obj.Name="新部门";

session.SaveOrUpdate(obj);

session.Transaction.Commit();

如果到这里已经能够获取到session了,那么恭喜你成功了,现在我们已经可以边看官方文档边实战了。

如果你看不懂这段代码,没关系,可以边看官方文档边练习了。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航