如何编写自定义nHibernate方言?

时间:2010-02-27 19:29:48

标签: nhibernate

我正在使用 NHibernate 和传统的rdbms规则引擎。我正在使用 GenericDialect 但是生成的一些sql无法正常工作。如果我需要为此规则引擎编写自定义方言,该如何开始?

2 个答案:

答案 0 :(得分:7)

这是一个示例方言:


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();

答案 1 :(得分:3)

我首先抓住nhibernate源,2.1.x分支是here。现有的方言都在src / NHibernate / Dialect下。

复制一个并开始黑客攻击。基类Dialect有许多扩展点。