在Nhibernate.Burrow中使用Fluent NHibernate
在web开发中使用Nhibernate.Burrow可以在数据库session和事务管理上节省不少力气, 最近又开始用Nhibernate了,结果发现一个不错的东西:Fluent NHibernate,它非常Cool! 我一直不喜欢写hbm映射xml,改用NHibernate.Mapping.Attributes感觉还不错,但遇到Fluent NHibernate之后就立刻把NHibernate.Mapping.Attributes抛到一边去了~~~
现在想在项目中同时使用Burrow和Fluent,让他们协作还是需要做一点工作的,参考我以前使用NHibernate.Mapping.Attributes的方式,只需要做如下修改:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <configSections>
4 <section name="NHibernate.Burrow" type="NHibernate.Burrow.Configuration.NHibernateBurrowCfgSection,NHibernate.Burrow" restartOnExternalChanges="true" />
5 </configSections>
6 <NHibernate.Burrow customConfigurator="cmscs.bll.BurrowConfigurator,cmscs.bll"
7 conversationCleanupFrequency="5"
8 conversationTimeOut="30"
9 manualTransactionManagement="false">
10 <persistenceUnits>
11 <add name="cmscs1" nh-config-file="database.cmscs1.xml" />
12 <add name="cmscs2" nh-config-file="database.cmscs2.xml" />
13 <add name="cmscs3" nh-config-file="database.cmscs3.xml" />
14 </persistenceUnits>
15 </NHibernate.Burrow>
16 </configuration>
2 <configuration>
3 <configSections>
4 <section name="NHibernate.Burrow" type="NHibernate.Burrow.Configuration.NHibernateBurrowCfgSection,NHibernate.Burrow" restartOnExternalChanges="true" />
5 </configSections>
6 <NHibernate.Burrow customConfigurator="cmscs.bll.BurrowConfigurator,cmscs.bll"
7 conversationCleanupFrequency="5"
8 conversationTimeOut="30"
9 manualTransactionManagement="false">
10 <persistenceUnits>
11 <add name="cmscs1" nh-config-file="database.cmscs1.xml" />
12 <add name="cmscs2" nh-config-file="database.cmscs2.xml" />
13 <add name="cmscs3" nh-config-file="database.cmscs3.xml" />
14 </persistenceUnits>
15 </NHibernate.Burrow>
16 </configuration>
这里面只需要添加一个customConfigurator="cmscs.bll.BurrowConfigurator,cmscs.bll" ,customConfigurator 会通知Burrow在初始化的时候实例化指定的对象来配置Burrow和Nhibernate.
cmscs.bll.BurrowConfigurator必须要实现NHibernate.Burrow.IConfigurator接口.
1 using NHibernate.Burrow;
2 using NHibernate.Cfg;
3 using FluentNHibernate;
4 using cmscs.entity;
5
6 namespace cmscs.bll
7 {
8 /// <summary>
9 /// 自定义配置burrow
10 /// </summary>
11 public class BurrowConfigurator : IConfigurator
12 {
13 private static bool _isSet;
14
15 #region IConfigurator 成员
16
17 public void Config(IBurrowConfig val)
18 {
19 //可以在这里重新设置Burrow配置参数;
20 //val.ConversationTimeOut = 60;
21 //val.ConversationCleanupFrequency = 5;
22 //val.ManualTransactionManagement = false;
23 }
24
25 /// <summary>
26 /// 配置nhibernate
27 /// </summary>
28 /// <param name="puCfg"></param>
29 /// <param name="nhCfg"></param>
30 public void Config(IPersistenceUnitCfg puCfg, Configuration nhCfg)
31 {
32 //由于Burrow会在每个数据库连接上调用此方法,并且我只想在第一个数据库连接上做映射关系配置,所以做了个检测
33 if (_isSet)
34 return;
35 _isSet = true;
36
37 //这里直接使用FluentNHibernate处理实体类映射,AddMappingsFromAssembly是FluentNHibernate提供的扩展方法.
38 //BaseEntity<>与FluentNHibernate映射类在同一个程序集中.
39 nhCfg.AddMappingsFromAssembly(typeof(BaseEntity<>).Assembly);
40
41 //如果使用NHibernate.Mapping.Attributes处理实体类映射的话可以这样操作
42 //HbmSerializer.Default.Validate = true;
43 //var stream = HbmSerializer.Default.Serialize(System.Reflection.Assembly.Load(EntityAssemblyName));
44 //nhCfg.AddInputStream(stream);
45
46 #endregion
47 }
48 }
2 using NHibernate.Cfg;
3 using FluentNHibernate;
4 using cmscs.entity;
5
6 namespace cmscs.bll
7 {
8 /// <summary>
9 /// 自定义配置burrow
10 /// </summary>
11 public class BurrowConfigurator : IConfigurator
12 {
13 private static bool _isSet;
14
15 #region IConfigurator 成员
16
17 public void Config(IBurrowConfig val)
18 {
19 //可以在这里重新设置Burrow配置参数;
20 //val.ConversationTimeOut = 60;
21 //val.ConversationCleanupFrequency = 5;
22 //val.ManualTransactionManagement = false;
23 }
24
25 /// <summary>
26 /// 配置nhibernate
27 /// </summary>
28 /// <param name="puCfg"></param>
29 /// <param name="nhCfg"></param>
30 public void Config(IPersistenceUnitCfg puCfg, Configuration nhCfg)
31 {
32 //由于Burrow会在每个数据库连接上调用此方法,并且我只想在第一个数据库连接上做映射关系配置,所以做了个检测
33 if (_isSet)
34 return;
35 _isSet = true;
36
37 //这里直接使用FluentNHibernate处理实体类映射,AddMappingsFromAssembly是FluentNHibernate提供的扩展方法.
38 //BaseEntity<>与FluentNHibernate映射类在同一个程序集中.
39 nhCfg.AddMappingsFromAssembly(typeof(BaseEntity<>).Assembly);
40
41 //如果使用NHibernate.Mapping.Attributes处理实体类映射的话可以这样操作
42 //HbmSerializer.Default.Validate = true;
43 //var stream = HbmSerializer.Default.Serialize(System.Reflection.Assembly.Load(EntityAssemblyName));
44 //nhCfg.AddInputStream(stream);
45
46 #endregion
47 }
48 }
好了,最后如需根据映射关系导出Schema脚本的话,无需再请Fluent NHibernate可以这样做.
1 var bf = new BurrowFramework();
2 var nhcfg = bf.BurrowEnvironment.GetNHConfig(DefaultConnectionName);
3 var export = new SchemaExport(nhcfg);
4 try
5 {
6 export.SetOutputFile(fileName);
7 export.Create(false, false);
8 }
9 catch (Exception ex)
10 {
11 throw new Exception("无法输出创建数据脚本。", ex);
12 }
2 var nhcfg = bf.BurrowEnvironment.GetNHConfig(DefaultConnectionName);
3 var export = new SchemaExport(nhcfg);
4 try
5 {
6 export.SetOutputFile(fileName);
7 export.Create(false, false);
8 }
9 catch (Exception ex)
10 {
11 throw new Exception("无法输出创建数据脚本。", ex);
12 }