Amitabh
Amitabh

Reputation: 61287

How to write custom nHibernate dialect?

I am using NHibernate with a legacy rdbms rule engine. I am using GenericDialect but some of the sql generated are not working. If I need to write custom Dialect for this rule engine how do start?

Upvotes: 0

Views: 3691

Answers (2)

tmaj
tmaj

Reputation: 35105

This is an example dialect:


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"; } }
}

The assembly it's in has a reference to NHibernate.dll

hibernate.cfg.dll (note that I don't have 'connection.connection_string' property set here, this is my setup specific and usually you would have connection string here) :

<?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>

In some setups the dialect line would be

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

And the code creating an 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();

Upvotes: 7

Lachlan Roche
Lachlan Roche

Reputation: 25956

I would start by grabbing the nhibernate source, the 2.1.x branch is here. The existing Dialects are all under src/NHibernate/Dialect.

Copy one and start hacking. The base class Dialect has many extension points.

Upvotes: 3

Related Questions