【问题标题】:How to write custom nHibernate dialect?如何编写自定义 nHibernate 方言?
【发布时间】:2010-02-27 19:29:47
【问题描述】:

我将 NHibernate 与旧版 rdbms 规则引擎一起使用。我正在使用 GenericDialect 但生成的某些 sql 不起作用。如果我需要为此规则引擎编写自定义方言,如何开始?

【问题讨论】:

    标签: nhibernate


    【解决方案1】:

    这是一个示例方言:

    
    using System;
    using System.Collections.Generic;
    using System.Web;
    
    /// 
    /// This class ensures that the tables created in our db are handling unicode properly.
    /// 
    public class NHibernateMySQL5InnoDBDialect : NHibernate.Dialect.MySQL5Dialect
    {
        public override String TableTypeString { get { return " ENGINE=InnoDB DEFAULT CHARSET=utf8"; } }
    }
    

    它所在的程序集引用了 NHibernate.dll

    hibernate.cfg.dll(注意我这里没有设置'connection.connection_string'属性,这是我的设置,通常你会在这里有连接字符串):

    <?xml version="1.0" encoding="utf-8"?>
    <!-- This is the ByteFX.Data.dll provider for MySql -->
    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
      <session-factory name="NHibernate.Test">
        <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
        <property name="dialect">NHibernateMySQL5InnoDBDialect, Assembly1</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      </session-factory>
    </hibernate-configuration>
    

    在某些设置中,方言行会是

    <property name="dialect">Assembly1.NHibernateMySQL5InnoDBDialect, Assembly1</property>
    

    以及创建 ISessionFactory 的代码:

    
    NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
    cfg.Configure();
    cfg.Properties["connection.connection_string"] = ConnectionStringForDatabase();
    cfg.AddDirectory(PathToMappingsIfYouUseNonStandardDirectory);//not needed if using embedded resources
    return cfg.BuildSessionFactory();
    

    【讨论】:

      【解决方案2】:

      我将从获取 nhibernate 源代码开始,2.1.x 分支是here。现有的Dialects都在src/NHibernate/Dialect下。

      复制一份并开始破解。基类Dialect 有很多扩展点。

      【讨论】:

        猜你喜欢
        • 2010-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多