代码改变世界

NHibernate Step By Step(1)-配置文件(Configuration)

  Clingingboy  阅读(1734)  评论(2编辑  收藏  举报

属于orm的配置文件

介绍一些常用的方法,太生僻的就不写了

一.全局配置

这是程序的入口点,一般情况两种

常用配置方法

1.在app.config或者web.config文件配置如下节点

(1)配置节点

<configSections>
    <section name="hibernate-configuration"
     type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
    requirePermission="false" /> <!-- Important under Medium Trust -->
</configSections>


(2)配置具体参数

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <bytecode-provider type="null"/><!-- Important under Medium Trust -->
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
        <property name="connection.connection_string">
            Server=(local);initial catalog=nhibernate;Integrated Security=true
        </property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="current_session_context_class">managed_web</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    </session-factory>
</hibernate-configuration>


2.将文件配置到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.Test project 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-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="NHibernate.Test">
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">
      Data Source=CLINGINGBOY-PC\SQLEXPRESS;Initial Catalog=NHTEST;Integrated Security=True
    </property>
        <property name="adonet.batch_size">10</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="use_outer_join">true</property>
        <property name="command_timeout">10</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    </session-factory>
</hibernate-configuration>



读取配置文件方法

(1)默认读取配置文件(推荐方法)
即上面定义了文件文件后,默认采用Configure无参方法读取,若配置了config文件,则优先读取,否则从hibernate.cfg.xml文件中读取

Configuration cfg = new Configuration();
cfg.Configure();


(2)自定义读取配置文件
下面的情况虽提供了灵活性,但不推荐使用,增加了复杂度.

方法一:根据特定文件读取

Configuration cfg = new Configuration();
cfg.Configure("TestEnbeddedConfig.cfg.xml");


方法二:根据程序集嵌入的资源文件读取(注意名字的命名空间)

 

Configuration cfg = new Configuration();
cfg.Configure(this.GetType().Assembly, "NHibernate.Test.TestEnbeddedConfig.cfg.xml");

方法三:根据XML读取器来读取

 

            string xml =
                @"<?xml version='1.0' encoding='utf-8' ?>
<hibernate-configuration xmlns='urn:nhibernate-configuration-2.2'>
    <session-factory name='NHibernate.Test'>
  <event type='flush'>
    <listener class='NHibernate.Event.Default.DefaultFlushEventListener, NHibernate'/>
  </event> 
    </session-factory>
</hibernate-configuration>";

            XmlDocument cfgXml = new XmlDocument();
            cfgXml.LoadXml(xml);

            Configuration cfg = new Configuration();
            XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null);
            cfg.Configure(xtr);


动态修改配置文件
除了默认从配置文件读取参数外,还可以在程序中修改和配置配置参数
Environment类包含了配置文件的全部参数,所以想了解具体参数的可以参考这个类

(1)检测配置文件参数是否配置正确

Assert.IsTrue(cfg.Properties.ContainsKey(Environment.ShowSql));
Assert.IsTrue(cfg.Properties.ContainsKey(Environment.UseQueryCache));
Assert.IsFalse(cfg.Properties.ContainsKey(Environment.PrepareSql), 
    "Our default conf should not include override the possible Dialect default configuration.");
Assert.IsTrue(cfg.Properties.ContainsKey(Environment.Isolation));
Assert.AreEqual("true 1, false 0, yes 1, no 0", cfg.Properties[Environment.QuerySubstitutions]);
Assert.AreEqual("Data Source=CLINGINGBOY-PC\\SQLEXPRESS;Initial Catalog=nhibernate;Integrated Security=True",
                cfg.Properties[Environment.ConnectionString]);


(2)手动配置参数

Configuration cfg = new Configuration();
IDictionary props = new Hashtable();

props[Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider";
props[Environment.Dialect] = "NHibernate.Dialect.MsSql2000Dialect";
props[Environment.ConnectionDriver] = "NHibernate.Driver.SqlClientDriver";
props[Environment.ConnectionString] =
    "Data Source=CLINGINGBOY-PC\\SQLEXPRESS;Initial Catalog=nhibernate;Integrated Security=True";

foreach (DictionaryEntry de in props)
{
    cfg.SetProperty(de.Key.ToString(), de.Value.ToString());
}


二.配置映射文件

添加单个类的映射文件

方法一:根据类名字
cfg.AddClass(typeof(Simple));

方法二:根据映射文件名

cfg.AddResource("NHibernate.DomainModel.Simple.hbm.xml", typeof(Simple).Assembly);


方法三:根据程序集来添加(推荐)

public Configuration AddAssembly(Assembly assembly)


方法四:直接通过配置文件来配置(推荐)

还有其他的方法,如根据字符串,外部文件等来配置,一般用不到,就不介绍了。用到再参考api吧.

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2006-08-02 asp.net控件开发基础(3)
点击右上角即可分享
微信分享提示